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.

Related Posts

0 comments:

Post a Comment