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 10 practical examples of xargs command :
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.
lpicentral@mail:~$ xargs
hello
john
this is me ( ctrl+d)
hello john this is me
lpicentral@mail:~$home/Downloads#
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
lpicentral@mail:~$ xargs -d\n
Hi
Welcome here
Now press Ctrl+D
lpicentral@mail:~$ xargs -d\n
Hi
Welcome hereHi
Welcome here
Now press Ctrl+D
lpicentral@mail:~$
We can limit the output as per requirement using -n option in xargs command, for example to display only 2 items per line ,
lpicentral@mail:~$ echo a1 b2 c3 d4 e45
a1 b2 c3 d4 e5
lpicentral@mail:~$ echo a1 b2 c3 d4 e5 | xargs -n 2
a1 b2
c3 d4
e5
lpicentral@mail:~$
Using the option -p in xargs command , user will be prompted before the execution with y (means yes) and n (means no) options.
lpicentral@mail:~$ echo a1 b2 c3 d4 e5 | xargs -p -n 2
/bin/echo a1 b2 ?…y
/bin/echo c3 d4 ?…a1 b2
y
/bin/echo e5 ?…c3 d4
n
lpicentral@mail:~$ echo a1 b2 c3 d4 e5 | xargs -p -n 2
/bin/echo a1 b2 ?…y
/bin/echo c3 d4 ?…a1 b2
y
/bin/echo e5 ?…c3 d4
y
e5
lpicentral@mail:~$
lpicentral@mail:~$ find /tmp -name ‘abc.txt’ | xargs rm
we can use the grep command with xargs to filter the particular search from the result of find command. An example is shown below :
lpicentral@mail:~$ find . -name “stamp” | xargs grep “country”
country_name
lpicentral@mail:~$
xargs can also handle spaces in files by using print0 and xargs -0 arguments to find command.
lpicentral@mail:~$ find /tmp -name “*.txt” -print0 | xargs -0 ls
/tmp/abcd asd.txt /tmp/asdasd asdasd.txt /tmp/cdef.txt
lpicentral@mail:~$ find /tmp -name “*.txt” -print0 | xargs -0 rm
lpicentral@mail:~$
First create a cars.txt with below conetnts :
lpicentral@mail:~$ cat cars.txt
Hundai,Santro
Honda,Mobilio
Maruti,Ertiga
Skoda,Fabia
To display data in first column as shown below.
lpicentral@mail:~$ cut -d, -f1 cars.txt | sort | xargs
Honda Hundai Maruti Skoda
lpicentral@mail:~$
lpicentral@mail:~$ ls -1 *.txt | xargs wc -l
4 cars.txt
13 trucks.txt
17 total
lpicentral@mail:~$
lpicentral@mail:~$ pwd
/home/lpicentral
lpicentral@mail:~$ ls -l *.sh
-rw-rw-r– 1 lpicentral lpicentral 0 Sep 15 22:53 abcde.sh
-rw-rw-r– 1 lpicentral lpicentral 0 Sep 15 22:53 abcd.sh
-rw-rw-r– 1 lpicentral lpicentral 0 Sep 15 22:53 fg.sh
lpicentral@mail:~$ sudo find . -name “*.sh” -print0 | xargs -0 -I {} mv {} backup/
lpicentral@mail:~$ ls -ltr backup/
total 0
-rw-rw-r– 1 lpicentral lpicentral 0 Sep 15 22:53 abcd.sh
-rw-rw-r– 1 lpicentral lpicentral 0 Sep 15 22:53 abcde.sh
-rw-rw-r– 1 lpicentral lpicentral 0 Sep 15 22:53 fg.sh
lpicentral@mail:~$
In this post we will discuss 10 practical examples of 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.
lpicentral@mail:~$ xargs
hello
john
this is me ( ctrl+d)
hello john this is me
lpicentral@mail:~$home/Downloads#
Example:2 Use of Delimiters
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
lpicentral@mail:~$ xargs -d\n
Hi
Welcome here
Now press Ctrl+D
lpicentral@mail:~$ xargs -d\n
Hi
Welcome hereHi
Welcome here
Now press Ctrl+D
lpicentral@mail:~$
Example:3 Limiting output per line
We can limit the output as per requirement using -n option in xargs command, for example to display only 2 items per line ,
lpicentral@mail:~$ echo a1 b2 c3 d4 e45
a1 b2 c3 d4 e5
lpicentral@mail:~$ echo a1 b2 c3 d4 e5 | xargs -n 2
a1 b2
c3 d4
e5
lpicentral@mail:~$
Example:4 Enable User Prompt before execution
Using the option -p in xargs command , user will be prompted before the execution with y (means yes) and n (means no) options.
lpicentral@mail:~$ echo a1 b2 c3 d4 e5 | xargs -p -n 2
/bin/echo a1 b2 ?…y
/bin/echo c3 d4 ?…a1 b2
y
/bin/echo e5 ?…c3 d4
n
lpicentral@mail:~$ echo a1 b2 c3 d4 e5 | xargs -p -n 2
/bin/echo a1 b2 ?…y
/bin/echo c3 d4 ?…a1 b2
y
/bin/echo e5 ?…c3 d4
y
e5
lpicentral@mail:~$
Example:5 Deleting files using find and xargs
lpicentral@mail:~$ find /tmp -name ‘abc.txt’ | xargs rm
Example:6 Using grep to query files
we can use the grep command with xargs to filter the particular search from the result of find command. An example is shown below :
lpicentral@mail:~$ find . -name “stamp” | xargs grep “country”
country_name
lpicentral@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.
lpicentral@mail:~$ find /tmp -name “*.txt” -print0 | xargs -0 ls
/tmp/abcd asd.txt /tmp/asdasd asdasd.txt /tmp/cdef.txt
lpicentral@mail:~$ find /tmp -name “*.txt” -print0 | xargs -0 rm
lpicentral@mail:~$
Example:8 Use xargs with cut command
First create a cars.txt with below conetnts :
lpicentral@mail:~$ cat cars.txt
Hundai,Santro
Honda,Mobilio
Maruti,Ertiga
Skoda,Fabia
To display data in first column as shown below.
lpicentral@mail:~$ cut -d, -f1 cars.txt | sort | xargs
Honda Hundai Maruti Skoda
lpicentral@mail:~$
Example:9 Count the number of lines in each file.
lpicentral@mail:~$ ls -1 *.txt | xargs wc -l
4 cars.txt
13 trucks.txt
17 total
lpicentral@mail:~$
Example:10 Move files to a different location
lpicentral@mail:~$ pwd
/home/lpicentral
lpicentral@mail:~$ ls -l *.sh
-rw-rw-r– 1 lpicentral lpicentral 0 Sep 15 22:53 abcde.sh
-rw-rw-r– 1 lpicentral lpicentral 0 Sep 15 22:53 abcd.sh
-rw-rw-r– 1 lpicentral lpicentral 0 Sep 15 22:53 fg.sh
lpicentral@mail:~$ sudo find . -name “*.sh” -print0 | xargs -0 -I {} mv {} backup/
lpicentral@mail:~$ ls -ltr backup/
total 0
-rw-rw-r– 1 lpicentral lpicentral 0 Sep 15 22:53 abcd.sh
-rw-rw-r– 1 lpicentral lpicentral 0 Sep 15 22:53 abcde.sh
-rw-rw-r– 1 lpicentral lpicentral 0 Sep 15 22:53 fg.sh
lpicentral@mail:~$
0 comments:
Post a Comment