How can I move files with xargs on Linux?

24

6

I am trying this and it's not working:

ls file_* | xargs mv {} temp/

Any ideas?

user1953864

Posted 2013-01-08T04:03:32.873

Reputation:

Anyone with a multi file move version of this? (xargs -n10 for example) – Gert van den Berg – 2016-04-14T09:42:00.963

Answers

31

On OS X:

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

On Linux:

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

Amadan

Posted 2013-01-08T04:03:32.873

Reputation: 1 465

1I notice that using "%" in place of "{}" also works - what does % mean and what does {} mean? Example: ls file_* | xargs -I% mv % temp/ – dmonopoly – 2016-06-16T03:19:45.190

3@dmonopoly: They don't mean anything. Whatever the parameter to -i is, it is getting replaced. ls file_* | xargs -iFOO mv FOO temp/ works exactly the same. – Amadan – 2016-06-16T03:40:17.483

-i is deprecated according to the manual, use -I (uppercase i) instead. – Matthias Braun – 2018-06-21T11:47:39.010

whats happen with -i option – None – 2013-01-08T04:19:30.543

1On Linux, at least, the / at the end is optional.  You can include it if you want, but it’s not necessary. – Scott – 2013-01-08T04:20:56.520

@Scott: I was staying as close to original, just changing to make it work. – Amadan – 2013-01-08T04:21:32.870

4@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 – 2013-01-08T04:23:15.883

15You might need to say -i{}, without a space.  Or say -I {}. – Scott – 2013-01-08T04:52:20.490

8

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.

David-SkyMesh

Posted 2013-01-08T04:03:32.873

Reputation: 221

2Note 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 – 2013-01-08T04:20:34.743

1Yes, true. Add -maxdepth 0 to prevent this. – None – 2013-01-08T04:22:01.437

"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 – 2013-01-08T04:25:34.683

Edited (added -maxdepth 0) – None – 2013-01-08T07:13:44.023

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

Ratheesh

Posted 2013-01-08T04:03:32.873

Reputation: 81

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

mdk

Posted 2013-01-08T04:03:32.873

Reputation: 41

0

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.

Basile Starynkevitch

Posted 2013-01-08T04:03:32.873

Reputation: 1 022