tags:

views:

134

answers:

5

I wanted to move all the files having certain patterns of the current directory to a folder.

Example- I wanted to move all the files starting with nz to foobar directory. I tried it using mv but didn't work out well.

+1  A: 

This will do it, though if you have any directories beginning with nz it will move those too.

for files in nz*
do
mv $files foobar
done

Edit: As shown above this totally over the top. However, for more complex pattern matches you might do something like:

for files in `ls | grep [regexp]`
do
mv $files foobar
done
RobS
+5  A: 

mv nz* foobar should do it.

Joey
+1  A: 

Try to use "mmv", which is installed on most Linux distros.

Oliver Michels
+2  A: 

mv nz* foobar/

Dikla
+1  A: 
find . | grep "your_pattern" | xargs mv destination_directory

Does the following:

  • Finds all files in the current directory
  • Filters them according to your pattern
  • Moves all resulting files to the destination directory
Benedikt Eger
Don't grep filenames. Especially not with find(1): it has -name. Also don't use xargs without -0. Especially not with find(1): it has -exec.
lhunath
Except for the fact that find can search for names what could go wrong with grepping filenames?
Benedikt Eger