Home  >  Article  >  xargs command and examples in Linux

xargs command and examples in Linux

不言
不言Original
2019-03-02 16:08:552651browse

xargs is a Linux/Unix powerful command for building and executing command lines from standard input. It accepts the output of one command and passes it as an argument to another command. xargs takes standard input, separated by spaces or newlines, and executes the command one or more times with any arguments followed by items. Empty lines on standard input are ignored.

xargs command and examples in Linux

Example

Example 1: Copy a large number of files to another folder.

Sometimes we need to copy a long list of files, in this case, the cp command fails with the error "Parameter list is too long". We can use xargs to accomplish this task.

# find /home/linuxman/public_html/tecadmin.net/ -type f | xargs -n1 -i cp {} /var/www/backup/

Example 2: Delete multiple files from a folder.

Sometimes we want to delete a large number of files from a folder. The example below will delete all .log files from the /var/log directory.

# find /var/www/tmp/ -type f | xargs rm -f

The above command will not be able to delete files with spaces. To handle spaces in xargs command, the following command is required.

# find /var/www/tmp/ -type f -print0 | xargs -0 rm -f

Example 3: Count the number of lines in multiple files.

The following example will count the number of lines for each .txt file in the /opt directory and its subdirectories

# find /opt -name "*.txt" | xargs wc -l

To process files that contain spaces in their names, you need to use the following command .

# find /opt/ -name "*.log" -print0 | xargs -0 wc -l

Example 4: Back up all configuratin files.

If you want to back up all configuration files (extension .conf) in the system, please use the following command.

# find / -name "*.conf" |  xargs tar czf  config.tar.gz
# ls -l config.tar.gz
-rw-r--r--. 1 root root 193310 Apr  1 13:26 config.tar.gz

Example 5: Using custom delimeter with xargs.

We can also use custom deleter with xargs command, which uses spaces and new lines as delimiters by default. Use the -d parameter to define the delimiter.

# echo "1,2,3,4,5" | xargs -d, echo

Output

1 2 3 4 5

Example 6: Using xargs to display output in sepreate lines.

In example 5, the output is displayed in a single line, we can also specify to display each output in a separate line.

# echo "1,2,3,4,5" | xargs -d, -L 1 echo

Output

1
2
3
4
5

Example 7: Handling white space in file names or paths.

To handle spaces in names, use -print0 and the find command, using -0 and the xargs command as arguments.

#  find /tmp -print0 | xargs -0 -L 1 echo

This article has ended here. For more exciting content, you can pay attention to other related column tutorials on the PHP Chinese website! ! !

The above is the detailed content of xargs command and examples in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn