当前位置:网站首页>Using skills of xargs -- the way to build a dream

Using skills of xargs -- the way to build a dream

2022-06-24 08:44:00 The road to dream

Unix Commands have arguments , Some commands are acceptable ” The standard input (stdin)” As a parameter . Pipeline command (|) The role of , Is to convert the standard output of the command on the left to standard input , The command provided to the right is used as a parameter . although , stay Unix Most commands in the system do not accept standard input as parameters , You can only enter parameters directly on the command line , This makes it impossible to pass parameters with pipeline commands . such as , What we use everyday echo Command does not accept pipeline parameters . and xargs Role of command , Is to convert standard input to command line parameters .

Be careful :xargs The following default is echo command , So it can be used alone

 Help information 

xargs --help
Usage: xargs [OPTION]... COMMAND INITIAL-ARGS...
Run COMMAND with arguments INITIAL-ARGS and more arguments read from input.

Mandatory arguments to long options are mandatory for short options too.
Non-mandatory arguments are indicated by [square brackets]
  -0, --null                   Items are separated by a null, not whitespace.
                               Disables quote and backslash processing
  -a, --arg-file=FILE          Read arguments from FILE, not standard input
  -d, --delimiter=CHARACTER    Input items are separated by CHARACTER, not by
                               blank space. Disables quote and backslash
                               processing
  -E END                       If END occurs as a line of input, the rest of
                               the input is ignored.
  -e [END], --eof[=END]        Equivalent to -E END if END is specified.
                               Otherwise, there is no end-of-file string
  --help                       Print a summary of the options to xargs.
  -I R                         same as --replace=R (R must be specified)
  -i,--replace=[R]             Replace R in initial arguments with names
                               read from standard input. If R is
                               unspecified, assume {}
  -L,-l, --max-lines=MAX-LINES Use at most MAX-LINES nonblank input lines per
                               command line
  -l                           Use at most one nonblank input line per
                               command line
  -n, --max-args=MAX-ARGS      Use at most MAX-ARGS arguments per command
                               line
  -P, --max-procs=MAX-PROCS    Run up to max-procs processes at a time
  -p, --interactive            Prompt before running commands
  --process-slot-var=VAR       Set environment variable VAR in child
                               processes
  -r, --no-run-if-empty        If there are no arguments, run no command.
                               If this option is not given, COMMAND will be
                               run at least once.
  -s, --max-chars=MAX-CHARS    Limit commands to MAX-CHARS at most
  --show-limits                Show limits on command-line length.
  -t, --verbose                Print commands before executing them
  --version                    Print the version number
  -x, --exit                   Exit if the size (see -s) is exceeded
 Example :

 Basic use :

#  Convert standard input to command line arguments 

echo "hello rumenz" | xargs echo

# grep Accept pipeline parameters 

cat /etc/passwd | grep root

# echo  The command does not accept pipeline parameters 

echo "hello rumenz" | echo


 Parameters :

-d   Specify the separator , By default, space division is used 

#  Space as separator 

echo "one two three" | xargs mkdir

#  Specify tab as separator 

echo -e "a\tb\tc" | xargs -d "\t" echo

-p   Print out the command to be executed and ask the user whether to execute 

echo 'one two three' | xargs -p touch

-0  To express with  null  As a separator 

find The command has a special parameter -print0, Used to specify the list of files to output null As a separator 

find /path -type f -print0 | xargs -0 rm

#  Specify how many lines to use as a command line parameter 

xargs -L 1 find -name "*.txt"

-n   Specify how many items to use as command line arguments at a time 

echo {0..9} | xargs -n 2  echo

#  Specify an alternate string for each command line parameter 

#  Pass command line parameters to multiple commands 

cat foo.txt
one
two
three

cat foo.txt | xargs -I {} sh -c 'echo {}; mkdir {}'

one
two
three

ls
one two three

#  Convert multi line input to single line input 

> cat rumenz.txt
1 2 3 4
5 6
7 8 9
> cat rumenz.txt | xargs
1 2 3 4 5 6 7 8 9

#  Convert single line text to multiple lines 

> cat rumenz.txt
1 2 3 4 5 6 7 8 9
> cat rumenz.txt | xargs -n 3
1 2 3
4 5 6
7 8 9

#  Specify the delimiter for line conversion 

> echo "rumenz:123:rumenz:456:rumenz:789" | xargs -d : -n 2
rumenz 123
rumenz 456
rumenz 789

# xargs and find combination 

find . -type f -name "*.txt" -print | xargs rm -f

#  Download files in bulk 

cat rumenz.txt | xargs wget -c

原网站

版权声明
本文为[The road to dream]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240622199396.html