Linux Xargs Command Tutorial With Examples

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-xargs-command-tutorial-with-examples/
PINTEREST
PINTEREST
INSTAGRAM

xargs is a command which can be found in most of the Linux and BSD operating systems and gives scale-ability to execute 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 command line. Xargs gives ability to provide parameters from 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 previous example we have print 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 command. We can provide parameter with {} which is 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  Dstat- Monitor Linux Server Performance

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.

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

Find all c files and copy them directly to the /tmp/ folder.

LEARN MORE  Linux Cp or Copy Command

Exit Status

Xargs provides exit status code according to command execution result.

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-xargs-command-tutorial-with-examples/
PINTEREST
PINTEREST
INSTAGRAM

Leave a Reply

Your email address will not be published.