Join our mailing list Subscribe Us

How to: xargs linux command


xargs is a extended arguments. xargs connects two commands, evaluate output of one command and apply this generated output to second command.


Syntax of Linux xargs?

To use the xargs command in the terminal, use the following syntax:

$ First_command | xargs [Options] [Second_command]

Prompt

This causes the second command to be executed with the arguments of the first one.

Options...

  • -0 or –null: This option causes each character to be taken literally and arguments to be separated because of the NULL character.
  • -a or –arg-file: With this option, arguments aren’t read from standard input, but from a file.
  • -d or –delimiter: Separations are made with this option based on the separator character and not the space character. Each character is treated literally.
  • -p or –interactive: This option queries whether to continue before executing each command.

Examples

The functionality of the xargs command is best shown with some examples.

 $ find -name "*.txt" | xargs rm
Prompt

In this example, you apply xargs with the Linux find command and the Linux rm command. The result is that all files with the extension .txt are removed from the computer’s file system.

$ fi
$ find -name "*.txt" | xargs grep "invoice"

Prompt

Find all files containing the word “invoice”. For this, you can also use the Linux grep command.

$ f
df -m /boot --output=avail | sed 1d | xargs -I {} expr {} \/ 1024
Prompt

Above command will get the output of size of "/boot" directory and xargs will divide that size with 1024