Moving files with find + xargs: target is not a directory?

Assuming you have GNU (find, xargs, & mv), change your command to this:

$ find /foot/bar/ -name '*.csv' -print0 | xargs -0 mv -t some_dir

excerpt from mv man page

   -t, --target-directory=DIRECTORY
          move all SOURCE arguments into DIRECTORY

The above xargs ... will construct the command so that calls to move will be like this:

 $ mv 1.csv 2.csv 3.csv ... -t some_dir

Don't need xargs

You can skip this approach by just having find do all the work itself:

$ find /foot/bar/ -name '*.csv' -exec mv -t some_dir {} +

Why do you need the mv -t ...?

This has to do with the way that xargs is constructing the set of files to pass to the command it's going to run each time, (i.e. mv ...).

When you run the mv command manually yourself you control how many filenames are passed to it and so you don't need to worry about needing the -t my_dir since you always will put the destination directory last.

References

  • mv GNU docs
  • [one-liner]: Copying & Moving Files efficiently with xargs
  • Why does find -exec mv {} ./target/ + not work?

Tags:

Find

Xargs