I found myself today needing to move some files around but unfortunately they had spaces in them and would not move as expected.
A quick solution soon found using the infamous 'FIND' command and some examples.
the most basic kind of search is as follows:
find . -name "*.avi" -print
It quite simply finds all files the have .avi on the end.
However sometimes you need a bit more, say you want to see the file sizes for these files:
find . -name "*.avi" -exec ls -l {} ;
Now we should get the file attributes for each file found, note the {} expand during the execution of the script and are replaced by the filenames found.
Lets say we want to move these files into the same directory, but some files we don't want to move, we could use the following command for this:
find / -name *.avi -a ! -name *Clip* -a ! -name *clock* -exec cp -v {} /usb01/ ;
The command in this example says find in directory "/" files with .avi AND (-a) NOT (!) Clip or clock, then execute the copy command (cp) filename into /usb01/
The next part is to remove original files, this could all be done on the same command line but I like to check things have worked before committing to a delete.
find / -name *.avi -a ! -name *Clip* -a ! -name *clock* -exec rm -v {} ;
496 find / -name "*.avi" -exec file {} ;
I also cam across the following command that search the path ie the directory name as well, I have not used it yet but thought I would add it.
You can match the entire filename with -path instead of -name.
find . -path '*/139.P/*smooth' -exec mv "{}" destination ;
Hope this explains some of the mysteries of the find utility.