0%

Linux xargs 命令

Linux xargs命令

xargs是一个非常非常强大的命令,是eXtended ARGuments的缩写,可以取一个命令的输出作为另一个命令的参数。

默认的用法为初始化输入字符,加上一些参数就能达到超级赞的效果(这里说的参数一般是和管道一起使用)。

官方定义为:

xargs - build and execute command lines from standard input

含义为从标准输入构建和执行命令。

用法为:

1
$ xargs [options] [command [initial-arguments]]

常用参数为:

  • -n max-args, --max-args=max-args :后面加次数,表示命令在执行的时候一次用的argument的个数,默认是用所有的
  • --delimiter=delim, -d delim :默认的分隔符是回车,这里重定义了分隔符修改的是xargs的分隔符。

基本用法

读取输入数据重新格式化后输出。比如一个测试文件,内容如下:

1
2
3
4
5
$ cat test.txt

h e l l o w o r l d

H O W A R E Y O U

现在我们单行输出:

1
2
3
$ cat test.txt | xargs

h e l l o w o r l d H O W A R E Y O U

指定输出几个字符

也可以使用参数-n指定一行输出几个字符,比如

1
2
3
4
5
6
7
8
9
$ cat test.txt | xargs -n3

h e l
l o w
o r l
d H O
W A R
E Y O
U

这对于对仗的唐诗是个绝佳的选择,比如五言绝句。

指定分界符

使用-d参数可以指定定界符:

1
2
3
$ echo "name1,name2,name3,name4" | xargs -d,

name1 name2 name3 name4

可以认为简单快捷地初步解析了CSV格式的数据了。

同上,配合上-n选项,可以指定每行输出几项

1
2
3
4
$ echo "name1,name2,name3,name4" | xargs -d, -n2

name1 name2
name3 name4

结合管道的简单使用

前面说了很强大,到底如何强大呢,加入你有一个文件夹photo,里面有几百个文件夹,可能还有各种文件,其中有一些jpg后缀的特别想保存,怎么办,一个命令搞定。

接下来这个命令就是找出所有的.jpg格式的图片,并将其打包归档。

1
$ find /the/path/of/photo -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz

其他的强大组合,以后再续。

获得/etc下所有以.conf结尾的文件。
可以有多种方法获得如下结果。
以下命令仅仅为了帮助大家理解如何使用xargs.find命令的输入结果一个接一个的传递给xargs,作为ls -l的参数。

1
$ find /etc -name "*.conf" | xargs ls –l

当你想下载一些URL,这些URL都保存在一个文件里,你可以以如下的方式使用xargs命令

1
$ cat url-list.txt | xargs wget –c

Linux xargs 命令

xargs(英文全拼: eXtended ARGuments)是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。

xargs 可以将管道或标准输入(stdin)数据转换成命令行参数,也能够从文件的输出中读取数据。

xargs 也可以将单行或多行文本输入转换为其他格式,例如多行变单行,单行变多行。

xargs 默认的命令是 echo,这意味着通过管道传递给 xargs 的输入将会包含换行和空白,不过通过 xargs 的处理,换行和空白将被空格取代。

xargs 是一个强有力的命令,它能够捕获一个命令的输出,然后传递给另外一个命令。

之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中有有这个必要,所以就有了 xargs 命令,例如:

1
2
find /sbin -perm +700 |ls -l       #这个命令是错误的
find /sbin -perm +700 |xargs ls -l #这样才是正确的

命令格式:

1
somecommand |xargs -item  command

参数:

  • -a file 从文件中读入作为 stdin
  • -e flag ,注意有的时候可能会是-E,flag必须是一个以空格分隔的标志,当xargs分析到含有flag这个标志的时候就停止。
  • -p 当每次执行一个argument的时候询问一次用户。
  • -t 表示先打印命令,然后再执行。
  • -i 或者是-I,这得看linux支持了,将xargs的每项名称,一般是一行一行赋值给 {},可以用 {} 代替。
  • -r no-run-if-empty 当xargs的输入为空的时候则停止xargs,不用再去执行了。
  • -s num 命令行的最大字符数,指的是 xargs 后面那个命令的最大命令行字符数。
  • -L num 从标准输入一次读取 num 行送给 command 命令。
  • -l 同 -L。
  • -x exit的意思,主要是配合-s使用。。
  • -P 修改最大的进程数,默认是1,为0时候为as many as it can ,这个例子我没有想到,应该平时都用不到的吧。

假设一个命令为 sk.sh 和一个保存参数的文件 arg.txt:

1
2
3
4
#!/bin/bash
#sk.sh命令内容,打印出所有参数。

echo $*

arg.txt文件内容:

1
2
3
4
5
# cat arg.txt

aaa
bbb
ccc

xargs 的一个选项 -I,使用 -I 指定一个替换字符串 {},这个字符串在 xargs 扩展时会被替换掉,当 -I 与 xargs 结合使用,每一个参数命令都会被执行一次:

1
2
3
4
5
# cat arg.txt | xargs -I {} ./sk.sh -p {} -l

-p aaa -l
-p bbb -l
-p ccc -l

复制所有图片文件到 /data/images 目录下:

1
ls *.jpg | xargs -n1 -I {} cp {} /data/images

xargs 结合 find 使用

用 rm 删除太多的文件时候,可能得到一个错误信息:**/bin/rm Argument list too long.** 用 xargs 去避免这个问题:

1
find . -type f -name "*.log" -print0 | xargs -0 rm -f

xargs -0 将 \0 作为定界符。

统计一个源代码目录中所有 php 文件的行数:

1
find . -type f -name "*.php" -print0 | xargs -0 wc -l

查找所有的 jpg 文件,并且压缩它们:

1
find . -type f -name "*.jpg" -print | xargs tar -czvf images.tar.gz

xargs 其他应用

假如你有一个文件包含了很多你希望下载的 URL,你能够使用 xargs下载所有链接:

1
# cat url-list.txt | xargs wget -c

DESCRIPTION
This manual page documents the GNU version of xargs. xargs reads items from the standard input, delimited by blanks (which can be pro‐
tected 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.

   The  command  line for command is built up until it reaches a system-defined limit (unless the -n and -L options are used).  The speci‐
   fied command will be invoked as many times as necessary to use up the list of input items.  In general, there will be many fewer  invo‐
   cations  of  command  than there were items in the input.  This will normally have significant performance benefits.  Some commands can
   usefully be executed in parallel too; see the -P option.

   Because Unix filenames can contain blanks and newlines, this default behaviour is often problematic; filenames containing blanks and/or
   newlines  are  incorrectly  processed  by  xargs.  In these situations it is better to use the -0 option, which prevents such problems.
   When using this option you will need to ensure that the program which produces the input for xargs also uses a null character as a sep‐
   arator.  If that program is GNU find for example, the -print0 option does this for you.

   If  any  invocation of the command exits with a status of 255, xargs will stop immediately without reading any further input.  An error
   message is issued on stderr when this happens.

OPTIONS
-0, –null
Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every
character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input
items might contain white space, quote marks, or backslashes. The GNU find -print0 option produces input suitable for this
mode.

   -a file, --arg-file=file
          Read  items from file instead of standard input.  If you use this option, stdin remains unchanged when commands are run.  Other‐
          wise, stdin is redirected from /dev/null.



   -E eof-str
          Set  the  end of file string to eof-str.  If the end of file string occurs as a line of input, the rest of the input is ignored.
          If neither -E nor -e is used, no end of file string is used.

   -e[eof-str], --eof[=eof-str]
          This option is a synonym for the -E option.  Use -E instead, because it is POSIX compliant while this option is not.  If eof-str
          is omitted, there is no end of file string.  If neither -E nor -e is used, no end of file string is used.

   -I replace-str
          Replace  occurrences  of replace-str in the initial-arguments with names read from standard input.  Also, unquoted blanks do not
          terminate input items; instead the separator is the newline character.  Implies -x and -L 1.

   -i[replace-str], --replace[=replace-str]
          This option is a synonym for -Ireplace-str if replace-str is specified.  If the replace-str argument is missing, the  effect  is
          the same as -I{}.  This option is deprecated; use -I instead.

   -L max-lines
          Use  at  most max-lines nonblank input lines per command line.  Trailing blanks cause an input line to be logically continued on
          the next input line.  Implies -x.

   -l[max-lines], --max-lines[=max-lines]
          Synonym for the -L option.  Unlike -L, the max-lines argument is optional.  If max-lines is not specified, it defaults  to  one.
          The -l option is deprecated since the POSIX standard specifies -L instead.


   -P max-procs, --max-procs=max-procs
          Run up to max-procs processes at a time; the default is 1.  If max-procs is 0, xargs will run as many processes as possible at a
          time.   Use  the -n option or the -L option with -P; otherwise chances are that only one exec will be done.  While xargs is run‐
          ning, you can send its process a SIGUSR1 signal to increase the number of commands to run simultaneously, or a  SIGUSR2  to  de‐
          crease  the number.  You cannot increase it above an implementation-defined limit (which is shown with --show-limits).  You can‐
          not decrease it below 1.  xargs never terminates its commands; when asked to decrease, it merely waits for more than one  exist‐
          ing command to terminate before starting another.

          Please  note that it is up to the called processes to properly manage parallel access to shared resources.  For example, if more
          than one of them tries to print to stdout, the output will be produced in an indeterminate order (and very likely mixed up)  un‐
          less  the processes collaborate in some way to prevent this.  Using some kind of locking scheme is one way to prevent such prob‐
          lems.  In general, using a locking scheme will help ensure correct output but reduce performance.  If you don't want to tolerate
          the  performance  difference,  simply  arrange for each process to produce a separate output file (or otherwise use separate re‐
          sources).

   -o, --open-tty
          Reopen stdin as /dev/tty in the child process before executing the command.  This is useful if you want xargs to run an interac‐
          tive application.

   -p, --interactive
          Prompt  the user about whether to run each command line and read a line from the terminal.  Only run the command line if the re‐
          sponse starts with `y' or `Y'.  Implies -t.

   --process-slot-var=name
          Set the environment variable name to a unique value in each running child process.  Values are reused once child processes exit.
          This can be used in a rudimentary load distribution scheme, for example.

   -r, --no-run-if-empty
          If  the  standard input does not contain any nonblanks, do not run the command.  Normally, the command is run once even if there
          is no input.  This option is a GNU extension.

   -s max-chars, --max-chars=max-chars
          Use at most max-chars characters per command line, including the command and initial-arguments and the terminating nulls at  the
          ends of the argument strings.  The largest allowed value is system-dependent, and is calculated as the argument length limit for
          exec, less the size of your environment, less 2048 bytes of headroom.  If this value is more than 128KiB, 128Kib is used as  the
          default  value;  otherwise,  the  default value is the maximum.  1KiB is 1024 bytes.  xargs automatically adapts to tighter con‐
          straints.

   --show-limits
          Display the limits on the command-line length which are imposed by the operating system, xargs' choice of buffer size and the -s
          option.  Pipe the input from /dev/null (and perhaps specify --no-run-if-empty) if you don't want xargs to do anything.

   -t, --verbose
          Print the command line on the standard error output before executing it.

   -x, --exit
          Exit if the size (see the -s option) is exceeded.

   --help Print a summary of the options to xargs and exit.

   --version
          Print the version number of xargs and exit.

EXAMPLES
find /tmp -name core -type f -print | xargs /bin/rm -f

   Find  files named core in or below the directory /tmp and delete them.  Note that this will work incorrectly if there are any filenames
   containing newlines or spaces.

   find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f

   Find files named core in or below the directory /tmp and delete them, processing filenames in such a way that file or  directory  names
   containing spaces or newlines are correctly handled.

   find /tmp -depth -name core -type f -delete

   Find  files  named  core  in or below the directory /tmp and delete them, but more efficiently than in the previous example (because we
   avoid the need to use fork(2) and exec(2) to launch rm and we don't need the extra xargs process).

   cut -d: -f1 < /etc/passwd | sort | xargs echo

   Generates a compact listing of all the users on the system.

EXIT STATUS
xargs exits with the following status:
0 if it succeeds
123 if any invocation of the command exited with status 1-125
124 if the command exited with status 255
125 if the command is killed by a signal
126 if the command cannot be run
127 if the command is not found
1 if some other error occurred.

   Exit codes greater than 128 are used by the shell to indicate that a program died due to a fatal signal.

STANDARDS CONFORMANCE
As of GNU xargs version 4.2.9, the default behaviour of xargs is not to have a logical end-of-file marker. POSIX (IEEE Std 1003.1,
2004 Edition) allows this.

   The  -l and -i options appear in the 1997 version of the POSIX standard, but do not appear in the 2004 version of the standard.  There‐
   fore you should use -L and -I instead, respectively.

   The -o option is an extension to the POSIX standard for better compatibility with BSD.

   The POSIX standard allows implementations to have a limit on the size of arguments to the exec functions.  This limit could be  as  low
   as  4096  bytes  including  the size of the environment.  For scripts to be portable, they must not rely on a larger value.  However, I
   know of no implementation whose actual limit is that small.  The --show-limits option can be used to  discover  the  actual  limits  in
   force on the current system.

SEE ALSO
find(1), locate(1), locatedb(5), updatedb(1), fork(2), execvp(3), kill(1), signal(7),

   The   full  documentation  for  xargs is maintained as a Texinfo manual.  If the info and xargs programs are properly installed at your
   site, the command info xargs should give you access to the complete manual.

COPYRIGHT
Copyright © 1990-2019 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later https://gnu.org/licenses/gpl.html.
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

BUGS
The -L option is incompatible with the -I option, but perhaps should not be.

   It is not possible for xargs to be used securely, since there will always be a time gap between the production of  the  list  of  input
   files  and  their  use in the commands that xargs issues.  If other users have access to the system, they can manipulate the filesystem
   during this time window to force the action of the commands xargs runs to apply to files that you didn't intend.  For a  more  detailed
   discussion  of  this  and related problems, please refer to the ``Security Considerations'' chapter in the findutils Texinfo documenta‐
   tion.  The -execdir option of find can often be used as a more secure alternative.

   When you use the -I option, each line read from the input is buffered internally.   This means that there is  an  upper  limit  on  the
   length of input line that xargs will accept when used with the -I option.  To work around this limitation, you can use the -s option to
   increase the amount of buffer space that xargs uses, and you can also use an extra invocation of xargs to ensure that very  long  lines
   do not occur.  For example:

   somecommand | xargs -s 50000 echo | xargs -I '{}' -s 100000 rm '{}'

   Here,  the  first  invocation  of  xargs has no input line length limit because it doesn't use the -i option.  The second invocation of
   xargs does have such a limit, but we have ensured that the it never encounters a line which is longer than it can handle.   This is not
   an ideal solution.  Instead, the -i option should not impose a line length limit, which is why this discussion appears in the BUGS sec‐
   tion.  The problem doesn't occur with the output of find(1) because it emits just one filename per line.

   The best way to report a bug is to use the form at https://savannah.gnu.org/bugs/?group=findutils.  The reason for  this  is  that  you
   will  then  be able to track progress in fixing the problem.   Other comments about xargs(1) and about the findutils package in general
   can be sent to the bug-findutils mailing list.  To join the list, send email to bug-findutils-request@gnu.org.

                                                                                                                                  XARGS(1)
处无为之事,行不言之教;作而弗始,生而弗有,为而弗恃,功成不居!

欢迎关注我的其它发布渠道