Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

8 Answers 8

up vote 21 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
1  
no problem, being accepted isn't everything. ;) –  Evandro Silva Nov 8 '12 at 13:58

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/

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

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
6  
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
3  
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

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

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

Using this command you can move multiple files.

mv SourceFilenames ~DestinationPath

share|improve this answer

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.

share|improve this answer

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

Your Answer

 
discard

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.