I am trying this and it's not working:
ls file_* | xargs mv {} temp/
Any ideas?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityOn OS X:
ls file_* | xargs -J {} mv {} temp/
On Linux:
ls file_* | xargs -i {} mv {} temp/
/
at the end is optional. You can include it if you want, but it’s not necessary.
Jan 8, 2013 at 4:20
-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
-i{}
, without a space. Or say -I {}
.
Jan 8, 2013 at 4:52
-i
is, it is getting replaced. ls file_* | xargs -iFOO mv FOO temp/
works exactly the same.
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.
file_*
files in the current directory, while find
(without additional options) will search the entire directory tree under the current directory.
Jan 8, 2013 at 4:20
-maxdepth 0
to prevent this.
mv
doesn't care if you process files together or individually, some other uses might.
-maxdepth 0
)
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.
xargs -n10
for example)