xargs is a command in UNIX like System that reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.
xargs command is very handy when combined with other commands. By default it expects input from STDIN.xargs is basically used to enhance the output of the initial command and utilize the output for performing numerous operations.
In this post we will discuss 11 practical examples of linux xargs command
Example:1 Basic Usage of xargs
Type xargs , it will expect an input from us , start typing with enter for next line and then do a ctrl+d to see the output as below.
linuxtechi@mail:~$ xargs hello john this is me ( ctrl+d) hello john this is me linuxtechi@mail:~$home/Downloads#
Example:2 Use of Delimiters in xargs (-d)
Here we specify a delimiter using the option -d , with \n as the delimiter. It echoes the string back to the screen when we press the ctrl+d
[root@linuxtechi ~]# xargs -d\n Hi Welcome here Now press Ctrl+D Hi Welcome here Now press Ctrl+D [root@linuxtechi ~]#
Example:3 Limiting output per line (-n)
We can limit the output as per requirement using -n option in xargs command, for example to display only 2 items per line ,
linuxtechi@mail:~$ echo a1 b2 c3 d4 e45 a1 b2 c3 d4 e5 linuxtechi@mail:~$ echo a1 b2 c3 d4 e5 | xargs -n 2 a1 b2 c3 d4 e5 linuxtechi@mail:~$
Example:4 Enable User Prompt before execution (-p)
Using the option -p in xargs command , user will be prompted before the execution with y (means yes) and n (means no) options.
linuxtechi@mail:~$ echo a1 b2 c3 d4 e5 | xargs -p -n 2 /echo a1 b2 ?...y a1 b2 echo c3 d4 ?...y c3 d4 echo e5 ?...n linuxtechi@mail:~$ linuxtechi@mail:~$ echo a1 b2 c3 d4 e5 | xargs -p -n 2 /echo a1 b2 ?...y a1 b2 echo c3 d4 ?...y c3 d4 echo e5 ?...y e5 linuxtechi@mail:~$
Example:5 Deleting files using find and xargs
Let’s assume we want to delete *.txt files from /tmp folder, run the following command,
linuxtechi@mail:~$ find /tmp -type f -name '*.txt' | xargs rm
Note: It always recommended to use above combination of find and xargs command to delete 1000+ files as it takes less time and less resources of system.
Example:6 Use Xargs and grep command for searching
we can use the grep command with xargs to filter the particular search from the result of find command. An example is shown below :
linuxtechi@mail:~$ find . -name "stamp" | xargs grep "country" country_name linuxtechi@mail:~$
Example:7 Handle space in file names
xargs can also handle spaces in files by using print0 and xargs -0 arguments to find command.
linuxtechi@mail:~$ find /tmp -name "*.txt" -print0 | xargs -0 ls /tmp/abcd asd.txt /tmp/asdasd asdasd.txt /tmp/cdef.txt linuxtechi@mail:~$ find /tmp -name "*.txt" -print0 | xargs -0 rm linuxtechi@mail:~$
Example:8 Use xargs with cut command
For the demonstration, let’s first create a cars.txt with below contents :
linuxtechi@mail:~$ cat cars.txt Hundai,Santro Honda,Mobilio Maruti,Ertiga Skoda,Fabia
To display data in first column as shown below.
linuxtechi@mail:~$ cut -d, -f1 cars.txt | sort | xargs Honda Hundai Maruti Skoda linuxtechi@mail:~$
Example:9 Count the number of lines in each file
linuxtechi@mail:~$ ls -1 *.txt | xargs wc -l 4 cars.txt 13 trucks.txt 17 total linuxtechi@mail:~$
Example:10 Move files to a different location
linuxtechi@mail:~$ pwd /home/linuxtechi linuxtechi@mail:~$ ls -l *.sh -rw-rw-r-- 1 linuxtechi linuxtechi 0 Sep 15 22:53 abcde.sh -rw-rw-r-- 1 linuxtechi linuxtechi 0 Sep 15 22:53 abcd.sh -rw-rw-r-- 1 linuxtechi linuxtechi 0 Sep 15 22:53 fg.sh linuxtechi@mail:~$ sudo find . -name "*.sh" -print0 | xargs -0 -I {} mv {} backup/ linuxtechi@mail:~$ ls -ltr backup/ total 0 -rw-rw-r-- 1 linuxtechi linuxtechi 0 Sep 15 22:53 abcd.sh -rw-rw-r-- 1 linuxtechi linuxtechi 0 Sep 15 22:53 abcde.sh -rw-rw-r-- 1 linuxtechi linuxtechi 0 Sep 15 22:53 fg.sh linuxtechi@mail:~$
Example:11 Replace String in Xargs Command (-i)
If we run the below command, it will create three files a,b & c in the current working directory
linuxtechi@mail:~$ printf "a\nb\nc\n" | xargs touch
If wish you to create a.txt, b.txt and c.txt then use -i parameter in xargs command, it will replace a string ‘a‘ with a.txt and so on
linuxtechi@mail:~$ printf "a\nb\nc\n" | xargs -i touch {}.txt
That’s all from this article, I hope these xargs command examples are informative to you. Feel free to share your feedback and comments.
my favorite feature of xargs is parallelization, e.g. I use
find . -name “*.MTS” -print0 | xargs -n1 -0 -P 2 ~/skritps/mts2webm.sh
to convert my videos using two cores.