The xargs
command is used to build and execute a command line from a standard input. Commands such as cp
, echo
, rm
, wc
, and so on, don't take input from a standard input or redirected output from another command. In such commands, we can use xargs
to provide an input as an output of another command. The syntax is as follows:
xargs [option]
Some of options are explained in the following table:
Option |
Description |
---|---|
- |
This reads items from a file instead of stdin |
|
Inputs are null-terminated instead of whitespace |
|
Prints a command line on a standard output before executing |
|
This displays the limit on the length of the command line imposed by OS |
|
Runs upto the max-procs processes one at a time |
|
This at most uses the max-args argument per command line |
The
xargs
command can be used without any option. It allows you to enter an input from stdin, and when ctrl + d
is called, it prints whatever was typed:
$ xargs Linux shell scripting ctrl + d Linux shell scripting
The --show-limits
option can be used to know the limit of the command line length:
$ xargs --show-limits Your environment variables take up 4017 bytes POSIX upper limit on argument length (this system): 2091087 POSIX smallest allowable upper limit on argument length (all systems): 4096 Maximum length of command we could actually use: 2087070 Size of command buffer we are actually using: 131072
The following shell script will explain how xargs
can be used to get a file with the maximum size in a given directory recursively:
#!/bin/bash # Filename: max_file_size.sh # Description: File with maximum size in a directory recursively echo "Enter path of directory" read path echo "File with maximum size:" find $path -type f | xargs du -h | sort -h | tail -1
The output after running this script is as follows:
Enter path of directory /usr/bin File with maximum size: 12M /usr/bin/doxygen
In this example, we are using xargs
to pass each regular file obtained from the find
command for size calculation. Furthermore, the output of du
is redirected to the sort
command for a human-numeric sort and then we can print the last line or sort to get the file with a maximum size.
Another useful example of using xargs
is to archive all the files that we are interested in, and these files can be kept as back files.
The following shell script finds all the shell script in a specified directory and creates tar
of it for further reference:
#!/bin/bash # Filename: tar_creation.sh # Description: Create tar of all shell scripts in a directory echo "Specify directory path" read path find $path -name "*.sh" | xargs tar cvf scripts.tar
The output after running the script is as follows:
Specify directory path /usr/lib64 /usr/lib64/nspluginwrapper/npviewer.sh /usr/lib64/xml2Conf.sh /usr/lib64/firefox/run-mozilla.sh /usr/lib64/libreoffice/ure/bin/startup.sh
In this example, all the files with an extension .sh
are searched and passed as parameters to the tar
command to create an archive. The file scripts.tar
is created in the directory from where the scripts are being called.
3.139.80.52