Linux Command - find and xargs

xargs can be used to process the files return from find command.

Xargs Options

Option Description
-0 Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special
--replace[=replace-str] Replace occurrences of replace-str in the initial-arguments with names read from standard input.

Examples

Example to remove .core files in /tmp directory

1
find /tmp -name "*.core" -type f -print | xargs rm -vf

However, the above command cannot remove filenames with space in it. A better way is to use null character as separator.

1
find /tmp -name "*.core" -type f -print0 | xargs -0 rm -vf

Example to copy .log files to test directory

the filename is provided as the first parameter of cp.

1
find . -name "*.log" -type f -print0 | xargs --replace={}  -0  cp -f  {} test

Reference