Linux xargs Command Tutorial With Examples

xargs is a command which can be found in most of the Linux and BSD operating systems and gives scale-ability to execute a command from standard input. Grep and awk accept parameters from standard input but cp or echo do not accept parameters from standard input. They only accept parameters from the command line. xargs gives the ability to provide parameters from the command line.

Provide Parameters To Xargs

We will provide some command parameters to the echo command. The echo command will automatically get parameters from xargs.

$ echo pof tut com | xargs echo

Limit Argument Count

In the previous example, we have printed all the provided words in a single line. But in some situations, this can not be suitable. We can limit argument count per command line with -n parameter. In this example, we will provide each word one by one to the echo command. So each word will be printed line by line.

$ echo pof tut com | xargs -n 1 echo
Limit Argument Count
Limit Argument Count

Here for each round of xargs only one command line parameter is provided one by one

Provide Argument Specifically

Up to now, we have provided our parameters automatically at the end of the command. We can provide parameter with {} which is the place holder for the parameter.

$ ls -1 | xargs file {}
Provide Argument Specifically
Provide Argument Specifically

Read Inputs From File

Up to now, we have read inputs from standard input but xargs can read inputs from the file. The file content will be read line by line and provided to the command.

$ xargs -a myinput echo

Provide Delimiter

The delimiter is used to parse the provided text into parts. Normally we have used space as a delimiter. In some situations, it can be provided specific char as a delimiter. In this example, we use ; as a delimiter for the xargs. And this will create three separate parts to process like pof tut and com .

$ echo "pof;tut;com" | xargs -d ";" echo

Prompt User Before Execution Of Each Line

xargs gives us the ability to execute provided parameters and command one by one. Some times we may need to check each line before the execution. Think about that we use rm command with a lot of execution lines and we do not want to make mistakes because of its catastrophic results. In this situation, we can -p parameter for prompt to us. We have 6 files starts with txt and we want to delete some of them.

$ ls txt* | xargs -p -n 1  rm
Prompt User Before Execution Of Each Line
Prompt User Before Execution Of Each Line

Print Command Before Execution

Another useful debug option for xargs is printing command before execution. This will give the exact situation while using xargs with other commands. We can call this option as verbose mode also. --verbose or -t options can be used for this feature.

$ ls -1 | xargs -t file {}
Print Command Before Execution
Print Command Before Execution

Find Command with Xargs Command

Here we will make an example with xargs and find command. Find all files those end with .PNG and then send the results to the xarg in order to delete with rm.

$ find . -name \*.PNG -type f -print | xargs rm {}

Xargs Command with Grep Command

In this example, we will use xargs with grep command. We will search .c files to grep for expression "pass"

$ find . -name \*.c -type f -print | xargs grep "pass" {}
Xargs Grep
Xargs Grep

Xargs Command with Cp Command

We can use cp command with xargs. In this example, we will find all c files and copy them directly to the /tmp/ folder.

$ find project -name "*.c" | xargs cp -t /tmp/

Exit Status For Xargs Command

xargs provides an exit status code according to the command execution result. Exit status is expressed as an integer. The meaning of the exit status code can be found below.

0 if it succeeds
123 if any invocation of the command exited with status 1-125
124 if the command exited with status 255
125 if the command is killed by a signal
126 if the command cannot be run
127 if the command is not found
1 if some other error occurred

LEARN MORE  How To Remove Files Older Than 1 Day/1 Week/1 Month Recursively

1 thought on “Linux xargs Command Tutorial With Examples”

Leave a Comment