232

I got a bunch of files in some directory (along with many other files) that I want to move.

Luckily, all the files I want to move contain a certain identifier in their names, so I can ls | grep IDENTIFIER to get the exact list of files to move.

But, how can I execute mv file /path/to/dest/folder/ at once, and not one by one (there's a lot of files to move)?

muru
  • 197,895
  • 55
  • 485
  • 740
gilad hoch
  • 2,445
  • 2
  • 21
  • 12

15 Answers15

317

You could use:

mv -t DESTINATION file1 file2 file3

The following also works, but I'm not sure if mv is invoked multiple times or not, as grep will output a new line for each match:

mv -t DESTINATION  `ls|grep IDENTIFIER`
tuomaz
  • 3,203
  • 2
  • 12
  • 3
135

If you want to move ABC-IDENTIFIER-XYZ.ext or IDENTIFIER-XYZ.xml, you can use:

mv *IDENTIFIER* ~/YourPath/

* is a wildcard for zero or more characters, this means zero or more characters, followed by IDENTIFIER, followed by zero or more characters.

This will move all the files that contain the IDENTIFIER you specified.

36

You can use wildcards.

Fore example, to move all files having a .doc extension:

mv *.doc /path/to/dest/folder/

This will move all doc file under the current directory to the specific destination.

Edit

To answer the comment:

but the list of files to move is not determined by extension. some of the files are named: ABC-IDENTIFIER-XYZ.ext and some just IDENTIFIER-XYZ.ext all having different extensions, mostly xml or properties.

mv *.ext  *.xml *.txt /path/to/dest/folder/
Achu
  • 21,237
  • 2
    but the list of files to move is not determined by extension. some of the files are named: ABC-IDENTIFIER-XYZ.ext and some just IDENTIFIER-XYZ.ext all having different extensions, mostly xml or properties. – gilad hoch Nov 08 '12 at 13:25
  • @giladhoch How about the edited one? – Achu Nov 08 '12 at 13:36
  • @gliadhoch If you are so comfortable using grep, you can see my answer above/below. – ignite Nov 08 '12 at 13:47
  • won't work since there are other .xml files (for instance) i do'nt want to move. – gilad hoch Nov 08 '12 at 13:50
34

If you want to move a set of arbitrary files (no common pattern in the names and types) you can do as Mr. Rajanand said: first go to the directory that contains the files you want to move

mv file1.ext1 file2.ext2 file3.ext3 /destination/

In case the files are scattered in different directories, you only need to specify the path for each file in the mv command.

16

If the files are in the same dir you can use

mv /path/to/source/dir/{file1,file2,*.ext1,*.ext2} /path/to/destination/

(tested in Ubuntu 16.04)

Sruli
  • 355
  • 3
  • 8
14

I use tuomaz's technique, but slightly modified:

mv file1 file2 file3 -t DESTINATION

I find this easier to remember and harder to screw up since it uses the same ordering as the vanilla mv operation:

mv file1 DESTINATION
Blake Frederick
  • 241
  • 2
  • 4
6

Use this command:

mv `ls|grep IDENTIFIER` /path/to/dest/folder  

However, ls is not recommended for this kind of use. Use find command instead.

ignite
  • 8,936
5
find -type f -name "[range]" -exec mv {} target-directory ';'

this command will move file names with any pattern/range to target-directory.

eg.

find -type f -name "file[1-50000]" -exec mv {} target-directory ';'

it will move files with names like file1, file2 ... file50000 to target-directory.

Zanna
  • 70,465
  • given the example in the question, I just want to put a note here on character classes - [range] (literally) will match r or a or n or g or e, so [IDENTIFIER] (whatever OP's identifier is) would probably not do the thing expected. Better to run find without -exec first to see what files will be operated on. – Zanna Aug 24 '21 at 16:15
4

Easiest way is like this

mv {file1,file2,file3} DESTINATION

or directory

mv {directory1,directory2,directory3} DESTINATION

or both files and directories

mv {file1,file2,file3,directory1,directory2,directory3} DESTINATION

Hope this helps

Sabrina
  • 2,003
  • 5
  • 24
  • 28
4

If you have so many files to move you can actually have too many for the mv command (or other commands like rm). I suggest using xargs to move each file individually in a loop like fashion. One way to get around that is to do:

ls -1 | grep IDENTIFIER | xargs -i mv {} /path/to/dest/folder/

The ls -1 (minus one) ensures that there is only one filename on each line. If you have hidden aliases for the ls command you can have multiple filenames on a single line and inadvertently move a file you did not intend to move.

  • This is also useful when your IDENTIFIER is not easily turned into a wildcard, or you want to use grep with a more complex regex. – AggieBill Nov 14 '12 at 10:11
  • 1
    +1 for xargs. find is almost always better and safer than ls. find . IDENTIFIER -exec mv {} /path/to/dest/folder ; (untested code) The . is for the current working directory. The ; is to end the command to be executed. Depending on what you're doing, you might have to add a -maxdepth 1 to keep it from recursing into subdirectories. – Joe Nov 14 '12 at 23:33
  • 1
    getting illegal option -i on mac os – james-see Sep 28 '20 at 14:38
  • On MacOS 11.4 this works ls -1 | grep IDENTIFIER | xargs -I % mv % /path/to/dest/folder/ – shinwan Dec 24 '21 at 11:00
3

You can use find and -exec like this:

find . -name "Identifier" -exec echo mv {} <destination_path> \;
2

You can use the output of ls as input to the mv commnad:

mv $(ls | grep IDENTIFIER) /path/to/dest/dir

The command between $() returns a list of the file names matching your search, and that can be provided as a parameter for the mv command.

Fuad Saud
  • 180
1

Using this command you can move multiple files:

mv SourceFilenames ~DestinationPath
0

If you are using fish shell most things are fortunate. So it simple goes like this, just key in the destination as the last file.

mv file1 file2 file3 DESTINATION
nehem
  • 113
0

Run this below command it will move all files

mv * DestinationPath
Tejas Lotlikar
  • 2,945
  • 5
  • 17
  • 26