Linux xargs Command Tutorial With Examples – POFTUT

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. Echo will automatically get parameters from xargs.

Provide Parameters To Xargs

Provide Parameters To Xargs

Limit Argument Count

In the previous example, we have printed all 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.

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 parameter.

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 file. File content will be read line by line and provided to the command

LEARN MORE  Awk Print Tutorial With Examples

xargs -a

Provide Delimiter

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

Provide Delimiter

Provide Delimiter

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 line and we do not want to make mistake 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.

Prompt User Before Execution Of Each Line

Prompt User Before Execution Of Each Line

Print Command Before Execution

Another useful debug options for xargs is printing command before execution. This will give 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.

Print Command Before Execution

Print Command Before Execution

Find Xargs

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.

LEARN MORE  How To Grep Text Files With Powershell Grep or Select-String Cmdlet In Windows?

Xargs Grep

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

Xargs Grep

Xargs Grep

Xargs Cp

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

Exit Status

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

1 Response

  1. dario says:

    {} is not a xargs replacement string
    there are errors on images

Leave a Reply

Your email address will not be published. Required fields are marked *