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
You could use:
mv -t DESTINATION file1 file2 file3The 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 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.
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.extand some justIDENTIFIER-XYZ.extall having different extensions, mostlyxmlorproperties.
mv *.ext *.xml *.txt /path/to/dest/folder/ 4 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/{file1,file2,*.ext1,*.ext2} /path/to/destination/(tested in Ubuntu 16.04)
2I use tuomaz's technique, but slightly modified:
mv file1 file2 file3 -t DESTINATIONI 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 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.
4find -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.
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.
Easiest way is like this
mv {file1,file2,file3} DESTINATIONor directory
mv {directory1,directory2,directory3} DESTINATIONor both files and directories
mv {file1,file2,file3,directory1,directory2,directory3} DESTINATIONHope this helps
You can use find and -exec like this:
find . -name "Identifier" -exec echo mv {} <destination_path> \; 2 You can use the output of ls as input to the mv commnad:
mv $(ls | grep IDENTIFIER) /path/to/dest/dirThe command between $() returns a list of the file names matching your search, and that can be provided as a parameter for the mv command.
Using this command you can move multiple files:
mv SourceFilenames ~DestinationPath 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