Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying this and its not working

ls file_* |xargs mv {} temp/

Any ideas

share|improve this question

migrated from stackoverflow.com Jan 8 '13 at 7:45

This question came from our site for professional and enthusiast programmers.

5 Answers 5

up vote 8 down vote accepted

On OS X:

ls file_* | xargs -J {} mv {} temp/

On Linux:

ls file_* | xargs -i {} mv {} temp/
share|improve this answer
    
whats happen with -i option –  user1953864 Jan 8 '13 at 4:19
    
On Linux, at least, the / at the end is optional. You can include it if you want, but it’s not necessary. –  Scott Jan 8 '13 at 4:20
    
@Scott: I was staying as close to original, just changing to make it work. –  Amadan Jan 8 '13 at 4:21
1  
@user1953864: -i (or -J) specify a token that will be replaced with the incoming arguments, instead of them just being tacked onto the end. man xargs –  Amadan Jan 8 '13 at 4:23
4  
You might need to say -i{}, without a space. Or say -I {}. –  Scott Jan 8 '13 at 4:52

find . -name "file_*" -maxdepth 0 -exec mv {} temp/ \;

find is better than ls where there might be more files than the number of program arguments allowed by your shell.

share|improve this answer
2  
Note that the question suggests a desire to process only the file_* files in the current directory, while find (without additional options) will search the entire directory tree under the current directory. –  Scott Jan 8 '13 at 4:20
    
Yes, true. Add -maxdepth 0 to prevent this. –  David-SkyMesh Jan 8 '13 at 4:22
    
"better" is subjective. More powerful, more complex, and slower; and while mv doesn't care if you process files together or individually, some other uses might. –  Amadan Jan 8 '13 at 4:25
    
Edited (added -maxdepth 0) –  David-SkyMesh Jan 8 '13 at 7:13

Use -t "specify target directoty" at mv, it should work moving files* to destination directory /temp

ex:- #ls -l file* | xargs mv -t /temp

share|improve this answer

as suggested by @user1953864: {-i, -J} specify a token that will be replaced with the incoming arguments.

For example ls:

something.java  exampleModel.java  NewsQueryImpl.java  NewsQueryInterface.java  readme someDirectory/

Then to move all java files into the someDirectory folder with xargs would be as follows:

On Linux

ls *.java | xargs -i mv {} someDirectory/

On Mac

ls *.java | xargs -J mv {} someDirectory
share|improve this answer

Another solution might be:

 for f in file_* ; do
   mv $f temp/$f
 done

The disadvantage is that it forks a new mv process for each file.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.