To find all files under the current directory with the string "FOO" in them and copy them to directory MEOW
, use:
grep --null -lr FOO . | xargs -0 cp -t MEOW
Unlike find
, grep
looks in the contents of a file. The -l
option to grep
tells it to list file names only. The -r
option tells it to look recursively in subdirectories. Because file names can have spaces and other odd characters in them, we give the --null
option to grep
so that, in the list it produces, the file names are safely separated by null characters. xargs -0
reads from that list of null-separated file names and provides them as argument to the command cp -t MEOW
which copies them to the target directory MEOW
.