Xargs Command

Linux Xargs Command Examples

What is xargs?


Xargs is a useful utility that allows you to build command lines from standard input. What this basically means is that xargs can take the output from one command and then execute another command against that output.



Xargs Command Examples


The following directory contains four test files:



john@ls001a:~/testing> ls -l
total 16
-rw-r--r-- 1 john users 145 May 28 09:39 file1.txt
-rw-r--r-- 1 john users 145 May 28 09:39 file2
-rw-r--r-- 1 john users 145 May 28 09:39 file3.txt
-rw-r--r-- 1 john users 145 May 28 09:40 file4

We will now use the command: find . -name "*.txt" | xargs ls -l to display only files that matched the pattern of "*.txt". Although there are easier ways of achieving this, this example is a good way to show how the xargs command works.



john@ls001a:~/testing> find . -name "*.txt" | xargs ls -l
-rw-r--r-- 1 john users 145 May 28 09:39 ./file1.txt
-rw-r--r-- 1 john users 145 May 28 09:39 ./file3.txt

From the above output, we can see that the xargs command only displayed files that matched the specified pattern.


Create an archive for files matching "*.jpg"


From the listing below we can see that there are several image files all ending with the file extension of ".jpg". To archive these files, we can run the following xargs command: find . -name "*.jpg" -type f -print | xargs tar -cvzf my_pictures.tar.gz



john@ls001a:~/testing> ls -l
total 36
-rw-r--r-- 1 john users  145 May 28 09:39 file1.txt
-rw-r--r-- 1 john users  145 May 28 09:39 file2
-rw-r--r-- 1 john users  145 May 28 09:39 file3.txt
-rw-r--r-- 1 john users  145 May 28 09:40 file4
-rw-r--r-- 1 john users 2610 May 28 10:02 picture.jpg
-rw-r--r-- 1 john users 2610 May 28 10:03 snap01.jpg
-rw-r--r-- 1 john users 2610 May 28 10:03 snap02.jpg
-rw-r--r-- 1 john users 2610 May 28 10:03 snap03.jpg
-rw-r--r-- 1 john users 2610 May 28 10:03 snap04.jpg

john@ls001a:~/testing> find . -name "*.jpg" -type f -print | xargs tar -cvzf my_pictures.tar.gz
./snap03.jpg
./picture.jpg
./snap04.jpg
./snap01.jpg
./snap02.jpg

john@ls001a:~/testing> ls -l
total 40
-rw-r--r-- 1 john users  145 May 28 09:39 file1.txt
-rw-r--r-- 1 john users  145 May 28 09:39 file2
-rw-r--r-- 1 john users  145 May 28 09:39 file3.txt
-rw-r--r-- 1 john users  145 May 28 09:40 file4
-rw-r--r-- 1 john users  369 May 28 10:04 my_pictures.tar.gz
-rw-r--r-- 1 john users 2610 May 28 10:02 picture.jpg
-rw-r--r-- 1 john users 2610 May 28 10:03 snap01.jpg
-rw-r--r-- 1 john users 2610 May 28 10:03 snap02.jpg
-rw-r--r-- 1 john users 2610 May 28 10:03 snap03.jpg
-rw-r--r-- 1 john users 2610 May 28 10:03 snap04.jpg

After the command has been run, we can now see that we have a compressed tar file called "my_pictures.tar.gz". This file now contains a copy of our image files.


Copy files that match a pattern to another directory


Often when running a command that either moves, renames or deletes a file or files, it is often useful to be able to test that we are gong to process the correct files only. In our example, we are going to substitute the "cp" command for the list "ls" command:



john@ls001a:~/testing> find . -name "*.txt" | xargs -n1 -i ls -l {}
-rw-r--r-- 1 john users 145 May 28 09:39 ./file3.txt
-rw-r--r-- 1 john users 145 May 28 09:39 ./file1.txt

john@ls001a:~/testing> find . -name "*.txt" | xargs -n1 -i cp {} ./backup/
john@ls001a:~/testing> ls -l ./backup/
total 8
-rw-r--r-- 1 john users 145 May 28 10:09 file1.txt
-rw-r--r-- 1 john users 145 May 28 10:09 file3.txt

As you can see, our first use of the xargs command displayed the files that we were going to copy to another location. Now that we are happy, we can change the "ls -l" command to the copy command "cp". Now when we ran the command, our files have now been copied to our backup directory.



Find files that end .txt and add the extension _.backup


In the following example we are going to find files that have a file extension of ".txt". We will then rename these files to have a new extension of "_.backup".



john@ls001a:~/testing> ls -l
total 44
drwxr-xr-x 2 john users 4096 May 28 10:12 backup
-rw-r--r-- 1 john users  145 May 28 09:39 file1.txt
-rw-r--r-- 1 john users  145 May 28 09:39 file2
-rw-r--r-- 1 john users  145 May 28 09:39 file3.txt
-rw-r--r-- 1 john users  145 May 28 09:40 file4
-rw-r--r-- 1 john users  369 May 28 10:04 my_pictures.tar.gz
-rw-r--r-- 1 john users 2610 May 28 10:02 picture.jpg
-rw-r--r-- 1 john users 2610 May 28 10:03 snap01.jpg
-rw-r--r-- 1 john users 2610 May 28 10:03 snap02.jpg
-rw-r--r-- 1 john users 2610 May 28 10:03 snap03.jpg
-rw-r--r-- 1 john users 2610 May 28 10:03 snap04.jpg


john@ls001a:~/testing> ls *.txt | xargs -i mv {} {}_.backup
john@ls001a:~/testing> ls -l
total 44
drwxr-xr-x 2 john users 4096 May 28 10:12 backup
-rw-r--r-- 1 john users  145 May 28 09:39 file1.txt_.backup
-rw-r--r-- 1 john users  145 May 28 09:39 file2
-rw-r--r-- 1 john users  145 May 28 09:39 file3.txt_.backup
-rw-r--r-- 1 john users  145 May 28 09:40 file4
-rw-r--r-- 1 john users  369 May 28 10:04 my_pictures.tar.gz
-rw-r--r-- 1 john users 2610 May 28 10:02 picture.jpg
-rw-r--r-- 1 john users 2610 May 28 10:03 snap01.jpg
-rw-r--r-- 1 john users 2610 May 28 10:03 snap02.jpg
-rw-r--r-- 1 john users 2610 May 28 10:03 snap03.jpg
-rw-r--r-- 1 john users 2610 May 28 10:03 snap04.jpg

Remove files that match a pattern


In the next example of the xargs command we are going to find files that have a file extension of ".bak" and delete them. Initially we test our xargs command by simply listing any files found. Once we have verified these files are OK to be deleted, we can then change our xargs command to use the "rm" command to delete the files:



john@ls001a:~/testing> ls | grep ".bak" | xargs -I{} ls {}
file5.bak
file6.bak
john@ls001a:~/testing> ls | grep ".bak" | xargs -I{} rm {}
john@ls001a:~/testing> ls | grep ".bak" | xargs -I{} ls {}

List only a subset of information using the "-n" option


We can limit the information handled by the xargs command by specifying the "-n" parameter.



john@ls001a:~/testing> ls
backup             file3.txt_.backup   picture.jpg  snap03.jpg
file1.txt_.backup  file4               snap01.jpg   snap04.jpg
file2              my_pictures.tar.gz  snap02.jpg

Now if we use xargs in conjunction with the "-n" parameter:



john@ls001a:~/testing> ls | xargs -n 3 echo
backup file1.txt_.backup file2
file3.txt_.backup file4 my_pictures.tar.gz
picture.jpg snap01.jpg snap02.jpg
snap03.jpg snap04.jpg

From the above we can see that only 3 arguments are passed at a time to the echo statement.


Xargs Options



Usage: xargs [-0prtx] [--interactive] [--null] [-d|--delimiter=delim]
       [-E eof-str] [-e[eof-str]]  [--eof[=eof-str]]
       [-L max-lines] [-l[max-lines]] [--max-lines[=max-lines]]
       [-I replace-str] [-i[replace-str]] [--replace[=replace-str]]
       [-n max-args] [--max-args=max-args]
       [-s max-chars] [--max-chars=max-chars]
       [-P max-procs]  [--max-procs=max-procs] [--show-limits]
       [--verbose] [--exit] [--no-run-if-empty] [--arg-file=file]
       [--version] [--help] [command [initial-arguments]]