212

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

0

15 Answers 15

285

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`
6
  • 7
    works on linux, not sure if it will work on mac Dec 10, 2016 at 20:25
  • 6
    works on mac and git bash via git for windwows. Feb 23, 2017 at 12:55
  • 24
    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, 2017 at 15:29
  • 4
    -1 for second approach as it will fall for files including new lines Sep 7, 2017 at 15:33
  • 9
129

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.

0
34

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

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.

0
13

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)

2
  • 1
    just eliminates the need to specify full path to dir for each file
    – Sruli
    Dec 1, 2017 at 11:22
  • +1 very intuitive, exactly what's needed! Tested to work on Ubuntu 20.04.
    – mirekphd
    Jun 15 at 11:20
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
0
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.

4
  • 10
    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, 2012 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, 2012 at 13:50
  • 4
    As stated by @NorTicUs, the use of ls is ill advised. Also, files with spaces could cause a problem. Nov 13, 2012 at 14:27
  • 2
    don't parse ls
    – Zanna
    Sep 7, 2017 at 15:15
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.

1
  • 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, 2021 at 16:15
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.

4
  • 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, 2012 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, 2012 at 23:33
  • 1
    getting illegal option -i on mac os
    – james-see
    Sep 28, 2020 at 14:38
  • On MacOS 11.4 this works ls -1 | grep IDENTIFIER | xargs -I % mv % /path/to/dest/folder/
    – shinwan
    Dec 24, 2021 at 11:00
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

3

You can use find and -exec like this:

find . -name "Identifier" -exec echo mv {} <destination_path> \;
2
  • maybe "*identifier*"?
    – Zanna
    Aug 24, 2021 at 16:11
  • find . -name "Identifier" -exec mv {} <destination_path> \;
    – alireza
    Jan 30 at 12:38
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.

1
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
1
  • 1
    this is just the normal mv syntax... the shell is not doing anything
    – Zanna
    Aug 24, 2021 at 15:20
0

Run this below command it will move all files

mv * DestinationPath

1
  • 3
    This is already included in the other answers. Sep 13 at 5:48

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.