Wednesday, October 8, 2014

Linux/UNIX xargs command examples

http://www.linuxtechi.com/xargs-command-with-examples

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 :

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#

Eample: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
linuxtechi@mail:~$ xargs -d\n
Hi
Welcome here
Now press Ctrl+D

linuxtechi@mail:~$ xargs -d\n
Hi
Welcome hereHi
Welcome here
Now press Ctrl+D
linuxtechi@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 ,
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

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
/bin/echo a1 b2 ?…y
/bin/echo c3 d4 ?…a1 b2
y
/bin/echo e5 ?…c3 d4
n

linuxtechi@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
linuxtechi@mail:~$

Example:5 Deleting files using find and xargs

linuxtechi@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 :
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

First create a cars.txt with below conetnts :
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:~$

No comments:

Post a Comment