0

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!

pouyan
  • 307
  • 1
  • 5
Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
  • If you're trying to move all image files from your desktop to an specific directory just use `-exec mv \{\} ~/Pictures \;` at the end of find (without pipe and xargs and print0) – pouyan Jan 31 '16 at 07:03
  • You are using the [tag:linux] tag, but are you actually using Linux (and GNU `xargs` and `mv`)? The BSD versions (which are also on [Mac OS X](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/mv.1.html)) have different options. – e0k Jan 31 '16 at 07:39

3 Answers3

2

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.

e0k
  • 6,961
  • 2
  • 23
  • 30
2

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/
noorul
  • 1,283
  • 1
  • 8
  • 18
1

I believe -target-directory should be --target-directory, or just -t.

lanceg
  • 86
  • 6
  • Not sure what is missing: `find ~/Desktop -name “*.jpg” -o -name “*.gif” -o -name “*.png” -print0 | xargs -0 mv -t ~/Pictures mv: missing file operand Try `mv --help' for more information.` – Mona Jalal Jan 31 '16 at 07:05
  • For what its worth, I think the quotes for the `-name` is causing the `find` not to return anything. Try removing them – lanceg Jan 31 '16 at 07:12
  • The quotes prevent wildcard expansion in the shell. You want to give the asterisk to `find`. – e0k Jan 31 '16 at 07:19
  • Actually, I don't think that `mv` command will work, since the arguments are reversed (See: http://stackoverflow.com/questions/13899746/use-xargs-to-mv-the-find-directory-into-another-directory). Try using `xargs -0 -J % mv % ~/Pictures` instead. – lanceg Jan 31 '16 at 07:25