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)?
15 Answers 15
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`
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
If you want to move ABC-IDENTIFIER-XYZ.ext or IDENTIFIER-XYZ.xml , you can use:
* 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.
Fore example, to move all files having a .doc extension:
This will move all doc file under the current directory to the specific destination.
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/
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 .
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.
If the files are in the same dir you can use
mv /path/to/source/dir/ /path/to/destination/
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 `ls|grep IDENTIFIER` /path/to/dest/folder
However, ls is not recommended for this kind of use. Use find command instead.
ls is not recommended for this kind of use. If you want to list files, especially with a grep behind, use find . -name \*IDENTIFIER\* .
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.
find -type f -name "[range]" -exec mv <> target-directory ';'
this command will move file names with any pattern/range to target-directory.
find -type f -name "file12488" -exec mv <> target-directory ';'
it will move files with names like file1 , file2 . file50000 to target-directory .
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.
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.
How to move all files and folders via mv command [duplicate]
Good point. If you are using bash, then you can run shopt -s dotglob and then «*» will match hidden files, too.
What happens if there are folders and files with the same name in the destination folder? Are they overwritten?
. it seems folders with the same name are not overwritten. mv: cannot move ‘/a/js’ to ‘/b/js’: Directory not empty
I know I accepted the first answer many years ago, and it’s stupid for me now to change it. But actually, I’ve been always using this method.
This works for me in Bash (I think this depends on your shell quite a bit. )
$ mv source/* /destination/folder/here
When I try I get mv: overwrite ‘destination/.’? mv: overwrite ‘destination/..’? , but adding -n to mv stops it from trying to overwrite
@Putnik — that’s a good gotcha! what os/distro ? ( I was working on OSX when I was messing around with this. )
This works for me in Bash 4.2.46, it moves all files and folders including hidden files and folders to another directory
Notice that .[^.]* means all hidden files except . and ..
I’d say it’s a bit boring, but really bullet-proof (GNU) way is:
cd /SourceDir && find ./ -maxdepth 1 -mindepth 1 -exec mv -t /Target/Dir <> +
P. S. Now you can possibly see why lots of people do prefer Midnight Commander, though.
If you only want to do a cut and paste-like action there is a simple way that worked for me:
$mv /media/dir_source $HOME/Documents/
It will move the folder named dir_source located in /media to the directory $HOME/Documents/
yet another way just for the heck of it (because I love convoluted ways to do things I guess)
cd /source for f in $(\ls -QA); do eval mv $f /destination/$f; done
the -Q and the -A are not POSIX, however the -A is fairly prevalent, and to not use the -Q you need to change the IFS (which then means you don’t need the eval but need to quote the variable)
IFS=" " && for f in $(ls -A); do mv "$f" /destination/"$f"; done