169

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

14 Answers 14

110

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.

| improve this answer | |
223

You could use

mv -t DESTINATION file1 file2 file3

and

mv -t DESTINATION `ls|grep IDENTIFIER`

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

| improve this answer | |
  • 7
    works on linux, not sure if it will work on mac – Alexander Mills Dec 10 '16 at 20:25
  • 4
    works on mac and git bash via git for windwows. – Brandt Solovij Feb 23 '17 at 12:55
  • 16
    Does not work on Mac (10.11.16 El Capitan). But you can simply put the target folder at the back, i.e. mv file1 file2 ... destination – Jonathan H Jul 23 '17 at 15:29
  • 3
    -1 for second approach as it will fall for files including new lines – αғsнιη Sep 7 '17 at 15:33
  • 5
29

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.

Edit

To answer the comment.

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

| improve this answer | |
  • 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 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
27

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

| improve this answer | |
  • This works as long as /destination/ already exists (tested on Mac OS X and Alpine Linux) (Note: this is a correction to my previous comment, which I deleted) – Matt Browne Oct 24 '19 at 19:19
12

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

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)

| improve this answer | |
  • 1
    This doesn't really add something to the existing answers, don't you think? – dessert Dec 1 '17 at 9:46
  • 1
    just eliminates the need to specify full path to dir for each file – Sruli Dec 1 '17 at 11:22
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.

| improve this answer | |
  • 9
    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
  • 4
    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
  • 1
    don't parse ls – Zanna Sep 7 '17 at 15:15
5

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.

| 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
    +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
4
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.

| improve this answer | |
3

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.

| improve this answer | |
2

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

| improve this answer | |
1

Using this command you can move multiple files.

mv SourceFilenames ~DestinationPath

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

You can use find and -exec like this:

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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