How to pipe the results of 'find' to mv in Linux

Solution 1:

find ./ -name '*article*' -exec mv {}  ../backup  \;

OR

find ./ -name '*article*' | xargs -I '{}' mv {} ../backup

Solution 2:

xargs is commonly used for this, and mv on Linux has a -t option to facilitate that.

find ./ -name '*article*' | xargs mv -t ../backup

If your find supports -exec ... \+ you could equivalently do

find ./ -name '*article*' -exec mv -t ../backup {}  \+

The -t option is a GNU extension, so it is not portable to systems which do not have GNU coreutils (though every proper Linux I have seen has that, with the possible exception of Busybox). For complete POSIX portability, it's of course possible to roll your own replacement, maybe something like

find ./ -name '*article*' -exec sh -c 'mv "$@" "$0"' ../backup {} \+

where we shamelessly abuse the convenient fact that the first argument after sh -c 'commands' ends up as the "script name" parameter in $0 so that we don't even need to shift it.

Solution 3:

I found this really useful having thousands of files in one folder:

ls -U | head -10000 | egrep '\.png$' | xargs -I '{}' mv {} ./png

To move all pngs in first 10000 files to subfolder png