Showing posts with label xargs command. Show all posts
Showing posts with label xargs command. Show all posts

Tuesday, 12 November 2019

xargs command examples in Unix / Linux

LPI Tutorials and Materials, LPI Guides, Unix Command, Linux Command, LPI Certifications

Xargs command in unix or linux operating system is used to pass the output of one command as an argument to another command. Some of the unix or linux commands like ls and find produces a long list of filenames. We want to do some operation on this list of file names like searching for a pattern, removing and renaming files etc. The xargs command provide this capability by taking the huge list of arguments as input , divides the list into small chunks and then passes them as arguments to other unix commands.

Unix Xargs Command Examples:


1. Renaming files with xargs

We have to first list the files to be renamed either by using the ls or find command and then pipe the output to xargs command to rename the files. First list the files which end with ".log" using the ls command.

ls *.log
oracle.log  storage.log

> ls *.log | xargs -i mv {} {}_bkp
> ls *_bkp
oracle.log_bkp  storage.log_bkp

You can see how the log files are renamed with backup (bkp) suffix. Here the option "i" tells the xargs command to replace the {} with the each file returned by the ls command.

2. Searching for a pattern

We can combine the grep command with xargs to search for a pattern in a list of files returned by another unix command (ls or find). Let’s list out all the bash files in the current directory with the find command in unix.

find . -name "*.bash"
./sql_server.bash
./mysql_backup.bash
./oracle_backup.bash

Now we grep for the "echo" statements from the list of files returned by the find command with the help of xargs. The command is shown below:

find . -name "*.bash" |xargs grep "echo"

If you don’t use xargs and piped the output of find command to grep command directly, then the grep command treats each file returned by the find command as a line of string and searches for the word "echo" in that line rather in that file.

3. Removing files using xargs

We can remove the temporary files in a directory using the rm command along with the xargs command. This is shown below:

ls "*.tmp" | xargs rm

This removes all the files with ".tmp" suffix.

4. Converting Multi-line output to Single line output.

If you run the ls -1 command, it will list each file on a separate line. This is shown below:

ls -1
oracle.txt
online_backup.dat
mysql_storage.bat

We can convert this multi-line output to single line output using the xargs command. This is shown below:

ls -1 | xargs
oracle.txt online_backup.dat mysql_storage.bat

5. Handling spaces in file names

By default the xargs command treats the space as a delimiter and sends each item as an argument to the unix command. If the file name contains a space (example: "oracle storage"), then each item will be treated as a separate file and will be passed as an argument. This will cause an issue. Let see how to handle the spaces in file names with an example.

Creating a file which contains space in the name
> touch "oracle storage"

> ls oracle\ storage | xargs grep "log"
grep: oracle: No such file or directory
grep: storage: No such file or directory

You can see that grep command is treating oracle as separate file and storage as separate file. This is because of xargs treats space as delimiter. To avoid this kind of errors use the -i option with braces as shown in below:

> ls oracle\ storage | xargs -i grep "log" {}

If you want to know what command the xargs is executing use the -t option with xargs. This will print the command on the terminal before executing it.

6. Passing subset of arguments

We can pass only a subset of arguments from a long list of arguments using the -n option with xargs command. This is shown in below.

> ls -1
backup
mysql
network
online
oracle
storage
wireless

> ls -1 | xargs -n 3 echo
backup mysql network
online oracle storage
wireless

You can see from the above output that 3 arguments are passed at a time to the echo statement.

LPI Tutorials and Materials, LPI Guides, Unix Command, Linux Command, LPI Certifications

Important Notes on Xargs Command:


1. Xargs directly cannot handle files which contain new lines or spaces in their names. To handle this kind of files use the -i option with xargs command. Another way to handle these characters is to treat the new line or spaces as null characters using th -0 option with xargs. However this requires that the input to xargs should also use the null as separator. An example is shown below

find . -print0 | xargs -0 rm

The print0 in find command makes the newline or space characters as null separator.

2. By default the xargs uses the end of the file string as "_". If this string appears in input string, then xargs command stops reading the input and rest of the input is ignored. You can change the end of file string by using the "-eof" option.

3. To know more about xargs command, run the xargs --help on the unix or linux terminal.

Saturday, 11 May 2019

Uses of the "xargs" Command

xargs Command, Linux Command, LPI Study Materials, LPI Guides, LPI Certifications

The xargs command is typically used in a command line where the output of one command is passed on as input arguments to another command.

Tuesday, 11 September 2018

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, Linux Command, Unix Command, LPI Study Materials

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.

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