YouTip LogoYouTip

Linux Comm Xargs

[![Image 1: Linux Command Manual](#) Linux Command Manual](#) xargs (full English name: eXtended ARGuments) is a filter for passing arguments to commands, and also a tool for combining multiple commands. xargs can convert data from a pipe or standard input (stdin) into command-line arguments, and can also read data from the output of a file. xargs can also convert single-line or multi-line text input into other formats, for example, converting multiple lines to a single line, or a single line to multiple lines. The default command for xargs is echo, which means that input passed to xargs via a pipe will contain newlines and whitespace. However, after processing by xargs, the newlines and whitespace will be replaced by spaces. xargs is a powerful command that can capture the output of one command and then pass it to another command. The reason this command is useful is that many commands do not support passing arguments via the `|` pipe, but there is often a need for this in daily work, so the xargs command was created. For example: find /sbin -perm +700 |ls -l # This command is incorrect find /sbin -perm +700 |xargs ls -l # This is the correct way xargs is generally used in conjunction with pipes. **Command Format:** somecommand |xargs -item command **Parameters:** * -a file Read from a file as stdin. * -e flag, note that sometimes it might be -E. The flag must be a space-separated token. When xargs encounters this flag, it stops processing. * -p Prompts the user each time an argument is executed. * -n num Followed by a number, indicates the number of arguments to use each time the command is executed. By default, all arguments are used. * -t Indicates to print the command first, and then execute it. * -i or -I, depending on Linux support. Assigns each item from xargs, typically line by line, to {}, which can be used as a placeholder. * -r no-run-if-empty Stops xargs if the input is empty, so it doesn't need to execute. * -s num Maximum number of characters in the command line, referring to the maximum command-line character count for the command following xargs. * -L num Reads num lines from standard input at a time and passes them to the command. * -l Same as -L. * -d delim Delimiter. The default xargs delimiter is a newline, and the argument delimiter is a space. This option modifies the xargs delimiter. * -x exit, mainly used in conjunction with -s. * -P Modifies the maximum number of processes. The default is 1. When set to 0, it runs as many as possible. I haven't thought of an example for this, and it's probably rarely used. ### Examples xargs is used as a replacement tool, reading input data, reformatting it, and then outputting it. Define a test file containing multiple lines of text data: # cat test.txt a b c d e f g h i j k l m n o p q r s t u v w x y z Multi-line input, single-line output: # cat test.txt | xargs a b c d e f g h i j k l m n o p q r s t u v w x y z The -n option for multi-line output: # cat test.txt | xargs -n3 a b c d e f g h i j k l m n o p q r s t u v w x y z The -d option allows you to define a custom delimiter: # echo "nameXnameXnameXname" | xargs -dX name name name name Used in conjunction with the -n option: # echo "nameXnameXnameXname" | xargs -dX -n2 name name name name Reads stdin and passes formatted arguments to a command. Assume a command is sk.sh and a file containing arguments is arg.txt: #!/bin/bash #sk.sh command content, prints all arguments. echo $* arg.txt file content: # cat arg.txt aaa bbb ccc An xargs option -I, using -I specifies a replacement string {}. This string is replaced when xargs expands. When -I is used with xargs, each argument command is executed once: # cat arg.txt | xargs -I {} ./sk.sh -p {} -l -p aaa -l -p bbb -l -p ccc -l Copy all image files to the /data/images directory: ls *.jpg | xargs -n1 -I {} cp {} /data/images xargs used with find. When using rm to delete too many files, you might get an error message: **/bin/rm Argument list too long.** Use xargs to avoid this problem: find . -type f -name "*.log" -print0 | xargs -0 rm -f xargs -0 uses as the delimiter. Count the number of lines in all PHP files in a source code directory: find . -type f -name "*.php" -print0 | xargs -0 wc -l Find all jpg files and compress them: find . -type f -name "*.jpg" -print | xargs tar -czvf images.tar.gz Other applications of xargs. If you have a file containing many URLs you want to download, you can use xargs to download all the links: # cat url-list.txt | xargs wget -c [![Image 2: Linux Command Manual](#) Linux Command Manual](#)
← React Event HandleJs Htmldom Collections β†’