aatifh
aatifh

Reputation: 2337

Moving files to a directory

I want to move all files matching a certain pattern in the current directory to another directory.

For example, how would I move all the files starting with nz to a directory called foobar? I tried using mv for that, but it didn't work out well.

Upvotes: 9

Views: 17772

Answers (6)

Jonathan Sanchez
Jonathan Sanchez

Reputation: 9364

mv nz* foobar/

  • mv - will move or rename file
  • nz - will get all the items that start with the "nz"
  • foobar/ - is the directory where all items will go into

Upvotes: 1

B.E.
B.E.

Reputation: 5080

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

Upvotes: 10

RobS
RobS

Reputation: 3857

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

Upvotes: 1

Dikla
Dikla

Reputation: 3491

mv nz* foobar/

Upvotes: 6

Oliver Michels
Oliver Michels

Reputation: 2917

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

Upvotes: 1

Joey
Joey

Reputation: 354536

mv nz* foobar should do it.

Upvotes: 8

Related Questions