How can I move files with xargs on Linux?

I am trying this and it's not working:

ls file_* | xargs mv {} temp/

Any ideas?

1

5 Answers

On OS X:

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

On Linux:

ls file_* | xargs -i {} mv {} temp/
8

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

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

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.

4

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

ls *.java | xargs -J mv {} someDirectory

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like