Linux xargs命令

发布时间:2023-04-27 09:18:50

  评: xargs是将参数传递给命令的过滤器,也是组合多个命令的工具。它将数据流分成足够小的块,方便过滤器和命令处理。xargs通常从管道或stdin中读取数据,但它也可以从文件输出中读取数据。xargs的默认命令是echo,这意味着通过管道传输给xargs的输入将包括换行和空白,但通过xargs的处理,换行和空白将被空白所取代。 xargs 这是一个强有力的命令,它可以捕获一个命令的输出,然后传递给另一个命令,以下是如何有效地使用xargs 实用例子。 1. 当您尝试使用rm时 删除太多文件可能会得到错误的信息:/bin/rm Argument list too long. 使用xargs 避免这个问题find ~ -name ‘*.log’ -print0 | xargs -0 rm -f

  2. 获得/etc/ 下所有*.conf 文件列表的结尾,有几种不同的方法可以得到相同的结果,下面的例子只是展示如何实用xargs ,在这个例子中实用 xargs将军find 命令的输出传递给ls -l # find /etc -name "*.conf" | xargs ls –l

  3. 假设你有一个包含很多URL的文件,你想下载, 可以使用xargsss 下载所有链接 # cat url-list.txt | xargs wget –c

  4. 找到所有的jpg 并压缩文件 # find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz

  5. 将所有图片文件复制到外部硬盘驱动器 # ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory

  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. xargs sh -c 'emacs "$@" < /dev/tty' emacs Launches the minimum number of copies of Emacs needed, one after the other, to edit the files listed on xargs' standard input. This example achieves the same effect as BSD's -o option, but in a more flexible and portable way.

上一篇 IDEA导出jar,打包成exe应用程序
下一篇 Nature研究成果遭抢发,两论文作者曾友好合作

文章素材均来源于网络,如有侵权,请联系管理员删除。

标签: Java教程Java基础Java编程技巧面试题Java面试题