简体   繁体   中英

linux find: move files by xargs

How should this be fixed? I am following a tutorial but I receive this error:

$ find ~/Desktop -name “*.jpg” -o -name “*.gif” -o -name “*.png” -print0 | xargs -0 mv –target-directory ~/Pictures
mv: cannot stat `–target-directory': No such file or directory

*I am interested on how to perform this command using xargs!

You don't need to use xargs , find can execute commands on the matches:

find ~/Desktop -name “*.jpg” -o -name “*.gif” -o -name “*.png” -exec mv \{\} ~/Pictures \;

You can give a command after -exec and before the escaped semicolon \\; . The \\{\\} is replaced with the matching file name.

From man find :

-exec command ;

Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of ';' is encountered. The string '{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a '\\') or quoted to protect them from expansion by the shell. See the EXAMPLES section for examples of the use of the -exec option. The specified command is run once for each matched file. The command is executed in the starting directory. There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.

Notice that the semicolon and {} must be escaped.

Using find and exec

$ find ~/Desktop -name "*.jpg" -exec mv '{}' /tmp/target/ \; -or -name "*.gif" -exec mv '{}' /tmp/target/ \; -or -name "*.png" -exec mv '{}' /tmp/target/ \;

Using xargs

$ find ~/Desktop -name "*.jpg" -or -name "*.gif" -or -name "*.png"  | xargs -I SRCFILE mv SRCFILE /tmp/target/

我相信-target-directory应该是--target-directory-t

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM