Linux/UNIX xargs command 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.

[email protected]:~$ xargs
hello
john
this is me ( ctrl+d)
hello john this is me
[email protected]:~$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

[email protected]:~$ xargs -d\n
Hi
Welcome here
Now press Ctrl+D

[email protected]:~$ xargs -d\n
Hi
Welcome hereHi
Welcome here
Now press Ctrl+D
[email protected]:~$

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 ,

[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

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

[email protected]:~$ 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
[email protected]:~$

Example:5 Deleting files using find and xargs

[email protected]:~$ 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 :

[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

First create a cars.txt with below conetnts :

[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]:~$

1 Response

  1. Dominik says:

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest