[Linux Basic] xargs Command
xargs is a command on Unix and most Unix-like operating systems used to build and execute command lines from standard input. It is passed to the command parameter of a filter. Normally, xargs reads data from the pipe or in stdin. But it can also read data from the output file. The default is the echo command xargs, this means that piped to xargs input will contain line breaks and gaps, but by xargs processing line breaks and blank spaces will be replaced.
xargs is a powerful command that can capture the output of a command, and then passed to another command, the following are some of the effective use xargs practical examples.
- Find all *.conf the end of the file list on /etc/ directory
# find /etc -name “*.conf” | xargs ls –l -
Copy all image files to an external hard drive
# ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory - When you try to delete too many files with rm, you may get an error message: /bin/rm Argument list too long. To avoid this problem with xargs
#find ~ -name ‘*.log’ -print0 | xargs -0 rm -f - Find all jpg files, and compress it
# find / -name *.jpg -type f -print | xargs tar -cvzf picture.tar.gz