xargs is a Linux/Unix powerful command to build and execute command lines from standard input. It takes output of a command and pass it as argument of another command. xargs takes standard input, delimited by blanks or newlines, and executes the command one or more times with any arguments followed by items. Blank lines on the standard input are ignored.

Advertisement

Syntax

xargs [OPTION]... COMMAND [INITIAL-ARGS]...

Here the COMMAND is executed with arguments INITIAL-ARGS and more arguments read from the input.

xargs Command Options

The xargs commands provides a limited number of command line arguments but enough to utilize its features.

  • -0, --null – The items are separated by a null, not whitespace
  • -a, --arg-file=FILE – Read arguments from FILE, not the standard input.
  • -d, --delimiter=CHARACTER – The items in input stream are separated by CHARACTER, not whitespace
  • -E END – Set a string as login EOF. If the string found, the rest will be ignored.
  • -L, --max-lines=MAX-LINES – Specify maximum number of lines (non-blank) to take as input at command line
  • -n, --max-args=MAX-ARGS – Specify max arguments per command line
  • -P, --max-procs=MAX-PROCS – Specify maximum processes to run at a time
  • -p, --interactive – Run processes interactive with prompt before running commands
  • -r, --no-run-if-empty – Do not run command if input arguments are empty.
  • -t, --verbose – Print all commands on screen executing them

xargs Command Examples

Example 1. Copy large number of files to another folder.

Some times we required to copy a long list of files, In that case cp command failed with error “Argument list too long”. We can use xargs to do that task.

find /backup/ -type f | xargs -n1 -i cp {} /var/www/backup/ 
Example 2: Delete multiple files from a folder.

Some times we requird to delete a large number of files from a folder. Below example will delete all .log files from /var/log directory.

find /var/www/tmp/ -type f | xargs rm -f 

Above command will failed to remove files with spaces in named. To handle spaces in xargs command try below command.

find /var/www/tmp/ -type f -print0 | xargs -0 rm -f 
Example 3: Count number of lines in multiple files.

Below example will count number of lines for each .txt file in /opt directory and its subdirectory

find /opt -name "*.txt" | xargs wc -l 

To handle files having spaces in there name, use following command.

find /opt/ -name "*.log" -print0 | xargs -0 wc -l 
Example 4: Make a backup of all configuration files.

If you want to make a backup of all configuration files ( extension .conf ) in your system, use below command.

find / -name "*.conf" |  xargs tar czf  config.tar.gz 
ls -l config.tar.gz 

-rw-r--r--. 1 root root 193310 Apr  1 13:26 config.tar.gz
Example 5. Use custome delimeter with xargs.

We can have also use custom delemeter with xargs command, By default it uses space and new line as delimeter. Use -d parameter to define delimeter.

echo "1,2,3,4,5" | xargs -d, echo 

Output:

1 2 3 4 5
Example 6: Show output in sepreate line with xargs.

In example 5 ouput is showing in single line, We can also specify to show each output in seprate line.

echo "1,2,3,4,5" | xargs -d, -L 1 echo 

Output:

1
2
3
4
5
Example 7: Handling blank space in filenames or path.

To handle spaces in names use -print0 with find command and -0 with xargs command as parameter.

find /tmp -print0 | xargs -0 -L 1 echo 
Share.

1 Comment

  1. plz explain the xargs options like -l, -n and what they r doing ? i didn’t understand example 3 . plz elaborate that also

Leave A Reply

Exit mobile version