I need to find and archive files with a certain file name e.g. ABC_000.jpg
find ~/my-documents/ -iname "ABC_***.JPG" -type f -exec cp {} ~/my-documents/archive/ \;
however I can not seem to find a way to limit the find function to find only 3 integers as there are files that are named ABC_CBA.jpg that I do not want included
Try this find:
find ~/my-documents/ -iname "ABC_[0-9][0-9][0-9].JPG" -type f -exec cp '{}' ~/my-documents/archive/ \;
EDIT: Or using regex:
find -E ~/my-documents/ -iregex ".*ABC_[0-9]{3}\.JPG" -type f -exec cp '{}' ~/my-documents/archive/ \;
Related
I am trying to rename files in current directory by prepending timestamp value using find command like below
find . -type f -exec mv {} $(date +%Y-%m-%d)_{} \;
But getting 'Can't move, no such file or directory error', but appending works well with below command, not sure what is the difference between two.
find . -type f -exec mv {} {}.$(date +%Y-%m-%d) \;
Try this
find . -type f -exec mv {} $(date +%Y-%m-%d)$(basename {}) \;
I am honestly nowhere near to be a decent bash scripter, but I made a little research and found a command that seems to be useful
find /path/to/files* -mtime +1 -exec rm {} \;
The question is if this line will remove directories? Because I want to only remove files that are images (actually in a *.jpeg format)
No, rm without the -r flag does not remove directories.
It looks like you want to add some more filters:
-type f to match only files
-name '*.jpeg' to match only files ending with .jpeg
Lastly, instead of -exec rm {} \;, you could use the much simpler -delete.
Putting it together, this looks more appropriate for you:
find /path/to/files* -mtime +1 -type f -name '*.jpeg' -delete
Then narrow your search results to *.jpeg files:
find /path/to/files* -mtime +1 -type f -name "*.jpeg" -exec rm {} \;
It's always better to remove the exec parameter to do a dry run before delete:
find /path/to/files* -mtime +1 -type f -name "*.jpeg"
Each line will be passed to rm command, and nothing more.
I'm trying to accomplish this task
1) Find directory A (DIR_A) and copy all files in the directory(including its sub-directory, if any) into a new directory called DIR_B
2) In directory (DIR_B),replace the word apple with orange
I executed the following code and for some reason, it copies all the files but it fails on the second task (replace apple with orange). I would appreciate help on this. Below is my code
find DIR_A -iname FILEA -type f -exec cp {} DIR_B \;|find DIR_B/ -iname \*.* -type f -exec sed -i "s|apple|orange|g" {} \;
Rather than trying to piping the output from one find into the other, why not just run them sequentially? I'm not sure that find reads from its stdin.
find DIR_A -iname FILEA -type f -exec cp {} DIR_B \; ; find DIR_B/ -iname \*.* -type f -exec sed -i "s|apple|orange|g" {} \;
I've replaced your pipe with a semi-colon.
Try this :
Sed Syntax :
sed 's/old/new/g'
find DIR_A -iname FILEA -type f -exec cp {} DIR_B \;|find DIR_B/ -iname \*.* -type f -exec sed -i "s/apple/orange/g" {} \;
I need to find files i directory and zip them under the same name.
i am trying the following
find . -name "ABC_*.txt" -mtime +30 -exec sh -c zip '{}' '{}' \;"
But something is wrong.
basically if find command finds 3 files say:
./ABC_1.txt
./ABC_2.txt
./ABC_3.txt
I will need 3 zip files:
./ABC_1.txt.zip
./ABC_2.txt.zip
./ABC_3.txt.zip
thanks in advance.
Try this:
find . -name "ABC_*.txt" -mtime +30 -exec zip "{}.zip" "{}" \;
You are likely overwriting your original file and will need to supply an extension to your ZIP.
You can use execdir option:
find . -name "ABC_*.txt" -mtime +30 -execdir sh -c 'zip "$1.zip" "$1"' - '{}' \;
In Unix, what is the single cmd that lets me search and locate a file recursively and then retrieve the file instead of just the path of the file?
What do you mean by retrieve?
You can simply use -exec argument to find.
$ find /path/to/search -type f -name '*.txt' -exec cat {} \;
$ find /path/to/search -type f -name 'pattern' -exec cp {} /path/to/new \;
The second one should work.
cat `find /wherever/you/want/to/start/from -name name_of_file`
Note those quotes are backquotes (`).