How to move multiple files at once to a specific destination directory?

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)?

Asked By: gilad hoch

||

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/
Answered By: Achu

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.

Answered By: Evandro Silva

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.

Answered By: ignite

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.

Answered By: Fuad Saud

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.

Answered By: AggieBill

Using this command you can move multiple files:

mv SourceFilenames ~DestinationPath
Answered By: Rajanand Ilangovan

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`
Answered By: tuomaz

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.

Answered By: Ismail AL-taharwa

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
Answered By: Blake Frederick
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, file2file50000 to target-directory.

Answered By: user734124

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)

Answered By: Sruli

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

Answered By: Sabrina

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
Answered By: nehem

You can use find and -exec like this:

find . -name "Identifier" -exec echo mv {} <destination_path> ;
Answered By: Mano Sriram

Run this below command it will move all files

mv * DestinationPath
Answered By: Rohit Brijwasi
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.