Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 44 down vote accepted

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.

share|improve this answer
that would work too, thanks! (to bad i can only accept one answer...) – gilad hoch Nov 8 '12 at 13:54
no problem, being accepted isn't everything. ;) – Evandro Silva Nov 8 '12 at 13:58

You could use

mv -t DESTINATION file1 file2 file3



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

share|improve this answer
Based off upvotes, why is this answer determined better? The accepted answer looks simpler. – Goldname yesterday

You can use wildcards.

Example: To move all files having extension .doc

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

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


To answer the comment.

mv *.ext *.xml *.txt /path/to/dest/folder/

share|improve this answer
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 8 '12 at 13:25
@giladhoch How about the edited one? – Achu Nov 8 '12 at 13:36
@gliadhoch If you are so comfortable using grep, you can see my answer above/below. – ignite Nov 8 '12 at 13:47
won't work since there are other .xml files (for instance) i do'nt want to move. – gilad hoch Nov 8 '12 at 13:50

In case you want to move a set of irrelevant 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 move command

share|improve this answer

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.

share|improve this answer
ls is not recommended for this kind of use. If you want to list files, especially with a grep behind, use find . -name \*IDENTIFIER\*. – NorTicUs Nov 8 '12 at 13:48
This answer was just to demonstrate how you can use the output of previous command in mv. As ls|grep was mentioned in the question, I just copied it. – ignite Nov 8 '12 at 13:50
thanks! that did the trick. – gilad hoch Nov 8 '12 at 13:55
As stated by @NorTicUs, the use of ls is ill advised. Also, files with spaces could cause a problem. – Paddy Landau Nov 13 '12 at 14:27

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.

share|improve this answer
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 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

You can use the output from ls to input into 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.

share|improve this answer

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:

share|improve this answer

Using this command you can move multiple files.

mv SourceFilenames ~DestinationPath

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.