Skip to content

Linux xarg basic commands

Xargs is a filter for passing arguments to commands and a tool for combining multiple commands. Xargs can convert pipe or standard input (stdin) data into command-line arguments, and can also read data from file output. Xargs can also convert single-line or multi-line text input to other formats, such as multi-line to single-line and single-line to multi-line. The default command of xargs is echo, which means that the input passed to xargs through the pipeline will contain newlines and blanks, but through xargs processing, newlines and blanks will be replaced by spaces. Xargs is a powerful command that captures the output of one command and passes it to another.

Usage syntax

Usage: xargs [OPTION]... COMMAND INITIAL-ARGS...
Run COMMAND with arguments INITIAL-ARGS and more arguments read from input.

Usage examples

  • Convert the standard input on the left side of the pipeline to the command line parameter hello world and pass it to the second echo command
echo "hello world"| xargs echo

Outputs:

hello world
  • Read input from the specified file and reformat it for outputFor example, test.txt has the following content.
# test.txt 
hello
,
good
morning
!

Convert multi-line input to single-line output.

cat test.txt | xargs

Outputs:

hello , good morning !

The -d option can customize a delimiter.

cat test.txt |xargs -d","

Outputs:

hello

good
morning
!

  • Read one line of input from the specified file and execute the command
    For example, test.txt has the following content.
  www.google.com
  8.8.8.8
  youtube.com

The xargs command uses the -a option, followed by the file name, to read the content from the file.

Use the -L 1 option, which tells xargs to read one line at a time. If this option is omitted, xargs will pass all content to the command.

  xargs -a test.txt -t -L 1 ping -c 1

Outputs:

  ping -c 1 www.google.com 
  PING www.google.com (216.58.203.68) 56(84) bytes of data.
  64 bytes from hkg07s48-in-f4.1e100.net (216.58.203.68): icmp_seq=1 ttl=120 time=19.6 ms

  --- www.google.com ping statistics ---
  1 packets transmitted, 1 received, 0% packet loss, time 0ms
  rtt min/avg/max/mdev = 19.629/19.629/19.629/0.000 ms
  ping -c 1 8.8.8.8 
  PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
  64 bytes from 8.8.8.8: icmp_seq=1 ttl=120 time=19.7 ms

  --- 8.8.8.8 ping statistics ---
  1 packets transmitted, 1 received, 0% packet loss, time 0ms
  rtt min/avg/max/mdev = 19.787/19.787/19.787/0.000 ms
  ping -c 1 youtube.com 
  PING youtube.com (172.217.27.46) 56(84) bytes of data.
  64 bytes from sin11s03-in-f14.1e100.net (172.217.27.46): icmp_seq=1 ttl=120 time=15.5 ms

  --- youtube.com ping statistics ---
  1 packets transmitted, 1 received, 0% packet loss, time 0ms
  rtt min/avg/max/mdev = 15.567/15.567/15.567/0.000 ms

  • Run multiple commands after xargs

To run multiple commands with xargs, use the -i or -I option. Customize a passing parameter symbol after -i or -I, such as %, all matching items will be replaced with the parameters passed to xargs.

  echo "file1 file2 file3"| xargs -I % sh -c 'touch %; ls -l %'

The function of the above command can also be implemented with the following command.

echo "file1 file2 file3"| xargs -i sh -c 'touch {}; ls -l {}'

Outputs:

  -rw-r--r-- 1 root root 0 Oct  8 10:53 file1
  -rw-r--r-- 1 root root 0 Oct  8 10:53 file2
  -rw-r--r-- 1 root root 0 Oct  8 10:53 file3
Leave a Reply