WebMasterCampus
WEB DEVELOPER Resources

Linux xargs Command

Learn Linux xargs Command with examples


Linux xargs Command

In Linux, we can use “xargs” command that reads streams of data from standard input, then generates and executes command lines.

Usually, xargs command is used in a UNIX shell to convert input from standard input into arguments to a command.

If no command is specified, xargs executes echo by default. You many also instruct it to read data from a file instead of stdin.

xargs Command Syntax

>> xargs command [command-argument ...] 
>> command1 | xargs command2

A pipe (|) use to pass the output to xargs. That will take care of running the command2 command, using the output of command1 as its argument(s).

xargs Command Example

>> echo -e "music.txt\nmusic2.txt" > music_list.txt

>> cat music_list.txt

music.txt
music2.txt
>> music_list.txt | xargs rm

xargs -p Command

Using xargs -p option will make xargs print a confirmation prompt with the action it’s going to take.

>> music_list.txt | xargs rm

xargs -n Command

xargs -n option lets you tell xargs to perform one iteration at a time, so you can individually confirm them with -p.

Here we tell xargs to perform one iteration at a time with -n1

>> music_list.txt | xargs -p -n1 rm

xargs -I Command

xargs -I option allows you to get the output into a placeholder, and then you can do various things.

>> music_list.txt | xargs -I % /bin/bash -c 'command2 %; command3 %;'

xargs -d Command Specify the Delimiter

The default xargs delimiter is a blank space. To change the default delimiter, use the -d command followed by a single character or an escape character such as n (a new line).

>> [command-providing-input] | xargs -d [new-delimiter] | xargs [command]

In the following example the xargs command instructs the system to use * as a delimiter and apply mkdir to each of the obtained arguments.

>> echo "Jan*Feb*March" | xargs -d* | xargs mkdir

xargs Combine xargs with find Command

The find command often precedes xargs in a pipeline. Use it to provide a list of files for further processing by xargs. The syntax looks like this:

>> find [location] -name "[search-term]" -type f | xargs [command]

The following example uses the find command to find all files with the .sh extension.

The list of files is then piped to xargs, which uses the rm command to delete them.

>> find /home/tariq . -name "*.sh" -type f | xargs rm -f

However, xargs does not automatically include files which contain blank spaces in their names.

To include those files too, use the -print0 option for find, and the -0 option for xargs:

>> find /home/tariq . -name "*.sh" -type f  -print0 | xargs rm -f

Note: use username available in your system. In my system I have /home/tariq

xargs Combine with grep Command

Use xargs with the grep command to search for a string in the list of files provided by the find command.

>> find . -name '[search-term]' | xargs grep '[string-to-find-in-files]'

Using the grep command with xargs to search files for stings The example above searched for all the files with the .txt extension and piped them to xargs, which then executed the grep command on them.

>> find . -name '*.txt' | xargs grep 'www'

xargs with Find and Archive Images Using tar

When used with the tar command, xargs creates a tar.gz archive and populates it with files provided by the find command.

>> find [location] -name "[search-term]" -type f -print0 | xargs -0 tar -cvzf [tar-gz-archive-name]
>> find /home/tariq -name "*.jpg*" -type f | xargs tar -cvzf photos.tar.gz

xargs to List All Linux User Accounts on the System

Use xargs to organize the output of the commands, such as cut. Consider the following example:

>> cut -d: -f1 < /etc/passwd | sort | xargs

The cut command accesses the /etc/passwd file and uses the : delimiter to cut the beginning of each line in the file. The output is then piped to sort, which sorts the received strings, and finally to xargs that displays them.

xargs to List Number of Lines/Words/Characters in Each File

Use xargs with the wc command to display a list of files with the line, word, and character count.

>> ls | xargs wc

The example below instructed the ls command to pipe to xargs only the files containing the word “example”. xargs then applied wc to that list:

>> ls "*example" | xargs wc

xargs to Remove Blank Spaces in String

Since xargs ignores blank spaces when looking for arguments, the command is useful for removing unnecessary blank spaces from strings.

>> echo "   Line with     more    spaces  " | xargs

Removing unnecessary blank spaces with xargs

>> echo "[string-with-unnecessary-spaces]" | xargs

xargs to Copy File to Multiple Directories

Copy files to multiple directories using the xargs command. The syntax is simple:

>> echo [directory-1] [directory-2] | xargs -n 1 cp -v [filename]

The echo command provides directory names, and xargs uses the cp command to copy the given file into each of the directories.

>> echo ./Documents/ ./public/ | xargs -n 1 cp -v file_1.txt

xargs options

Option Description
-0 input items are terminated by null character instead of white spaces
-a file read items from file instead of standard input
–delimiter = delim input items are terminated by a special character
-E eof-str set the end of file string to eof-str
-I replace-str replace occurrences of replace-str in the initial arguments with names read from standard input
-L max-lines use at-most max-lines non-blank input lines per command line.
-p prompt the user about whether to run each command line and read a line from terminal.
-r If the standard input does not contain any nonblanks; do not run the command
-x exit if the size is exceeded.
–help print the summary of options to xargs and exit
–version print the version no. of xargs and exit
Created with love and passion.