Low Orbit Flux Logo 2 F

Linux Command - xargs

The xargs command is used to send batches of arguments to a command. The command is run for each batch.

Problem - Why this functionality is needed:

Behavior:

NOTE - xargs won’t handle filenames with spaces correctly. The ‘-0’ option can be used to split on nulls instead. This will require the input to be split by nulls. ( The GNU find command does this with ‘-print0’).

Generated a sequence of numbers and pass it to xargs as a test:



seq 1 100 | xargs

echo the names of all listed files:



ls /data1 | xargs

Remove all files listed:



find /data1 | xargs rm

grep for a string inside all files found:



find /data1 | xargs grep "Error"

grep for a string inside all files found while also handling filenames with spaces by using the “-print0” and “-0” options of the find and xargs commands:



find /data1 -print0 | xargs -0 grep "Error"

gzip all files listed:



ls *.txt | xargs gzip

gzip all files found:



find /data1 | xargs gzip

gzip all files found and do as many in parallel as possible ( 0 ):



find /data1 | xargs -P 0 gzip

Using a replace string to control arg placement:



seq 1 5 | xargs -I STRING echo "start:" STRING "end"

Use a file instead of standard input:



xargs -a my_list.txt rm

Show system limits:



echo /dev/null | xargs --show-limits

-0 split on null
-a file # read from file
-I replace-str
-n max_args
-P max processes to run in parallel ( default 1 ), set to 0 for max possible
-p interactive, prompt before running each command
-r don’t run if no args ( GNU only )
–show-limits shows system limits