11 Xargs Command Examples in Linux
Abstract: ~$ echo a1 b2 c3 d4 e5 | xargs -n 2Honda Hundai Maruti Skoda
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 xargsType 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.
[email protected]:~$ xargs hello john this is me ( ctrl+d) hello john this is me [email protected]:~$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
[[email protected] ~]# xargs -d\n Hi Welcome here Now press Ctrl+D Hi Welcome here Now press Ctrl+D [[email protected] ~]#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 ,
[email protected]:~$ echo a1 b2 c3 d4 e45 a1 b2 c3 d4 e5 [email protected]:~$ echo a1 b2 c3 d4 e5 | xargs -n 2 a1 b2 c3 d4 e5 [email protected]:~$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.
[email protected]:~$ 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 [email protected]:~$ [email protected]:~$ 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 [email protected]:~$Example:5 Deleting files using find and xargs
Let’s assume we want to delete *.txt files from /tmp folder, run the following command,
[email protected]:~$ 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 searchingwe can use the grep command with xargs to filter the particular search from the result of find command. An example is shown below :
[email protected]:~$ find . -name "stamp" | xargs grep "country" country_name [email protected]:~$Example:7 Handle space in file names
xargs can also handle spaces in files by using print0 and xargs -0 arguments to find command.
[email protected]:~$ find /tmp -name "*.txt" -print0 | xargs -0 ls /tmp/abcd asd.txt /tmp/asdasd asdasd.txt /tmp/cdef.txt [email protected]:~$ find /tmp -name "*.txt" -print0 | xargs -0 rm [email protected]:~$Example:8 Use xargs with cut command
For the demonstration, let’s first create a cars.txt with below contents :
[email protected]:~$ cat cars.txt Hundai,Santro Honda,Mobilio Maruti,Ertiga Skoda,Fabia
To display data in first column as shown below.
[email protected]:~$ cut -d, -f1 cars.txt | sort | xargs Honda Hundai Maruti Skoda [email protected]:~$Example:9 Count the number of lines in each file
[email protected]:~$ ls -1 *.txt | xargs wc -l 4 cars.txt 13 trucks.txt 17 total [email protected]:~$Example:10 Move files to a different location
[email protected]:~$ pwd /home/linuxtechi [email protected]:~$ 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 [email protected]:~$ sudo find . -name "*.sh" -print0 | xargs -0 -I {} mv {} backup/ [email protected]:~$ 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 [email protected]:~$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
[email protected]:~$ 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
[email protected]:~$ 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.