`
standalone
  • 浏览: 610940 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

xargs: How To Control and Use Command Line Arguments

阅读更多

 am trying to use xargs command using shell pipes and not able to understand how to control and use command line arguments. For example I'd like to find out all *.c file located in 100s of sub-directories and move them to another directory called ~/old.src. How do I use command line args with xargs to achieve the same?

xargs command is designed to construct argument lists and invoke other utility. xargs reads items from the standard input or pipes, delimited by blanks or newlines, and executes the command one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.

xargs is more safer and easy to use

xargs functionality can be achived using the backquote feature of shell. But, it offers more options. It can deal with blanks or special characters in file names easily. It is often used with find, grep and other commands.

xargs examples

For example following example will print 1 2 3 4 using xargs (echo command is default)
$ echo 1 2 3 4 | xargs echo
OR
$ echo 1 2 3 4 | xargs
You can force xargs to use at most max-args arguments per command line. For example following will use first two argument per command:
$ echo 1 2 3 4 | xargs -n 2
Find all .bak files in or below the current directory and delete them.
$ find . -name "*.bak" -type f -print | xargs /bin/rm -f

{} as the argument list marker

{} is the default argument list marker. You need to use {} this with various command which take more than two arguments at a time. For example mv command need to know the file name. The following will find all .bak files in or below the current directory and move them to ~/.old.files directory:
$ find . -name "*.bak" -print0 | xargs -0 -I mv {} ~/old.files
You can rename {} to something else. In the following example {} is renamed as file. This is more readable as compare to previous example:
$ find . -name "*.bak" -print0 | xargs -0 -I file mv file ~/old.files
Where,

  1. -0 If there are blank spaces or characters (including newlines) many commands will not work. This option take cares of file names with blank space.
  2. -I 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.

Dealing file names with blank spaces and newline

The following will work incorrectly if there are any filenames containing newlines or spaces (it will find out all .mp3 file located in current directory and play them using mplayer):
$ find . -iname "*.mp3" -print | xargs mplayer
To get rid of this problem use -0 option:
$ find . -iname "*.mp3" -print0 | xargs -0 -I mp3file mplayer mp3file
To find out all *.c file located in 100s of subdirectories and move them to another directory called ~/old.src, use:
$ find /path/to/dir -iname "*.c" -print0 | xargs -0 -I file mv file ~/old.src

Avoiding errors and resource hungry problems with xargs and find combo

To copy all media files to another location called /bakup/iscsi, you can use cp as follows:
$ cp -r -v -p /share/media/mp3/ /backup/iscsi/mp3
However, cp command may fail if an error occurs such as if the number of files is too large for the cp command to handle. xargs in combination with find can handle such operation nicely. xargs is more resource efficient and will not halt with an error:

 

$ find /share/media/mp3/ -type f -name "*.mp3" -print0 | xargs -0 -r -I file cp -v -p file --target-directory=/bakup/iscsi/mp3

 

Please note that all of the above commands are tested with GNU/xargs version. BSD and UNIX xargs command may not have options such as -r. Please refer to your local xargs man page for further info:
man xargs

 

分享到:
评论

相关推荐

    git-xargs:git-xargs 是一个命令行工具 (CLI),用于使用单个命令跨多个 Github 存储库进行更新

    git-xargs是一个命令行工具 (CLI),用于使用单个命令跨多个 Github 存储库进行更新。 你给git-xargs : 要运行的脚本或命令 回购清单 和git-xargs将: 克隆每个 repo 针对它运行您指定的脚本或命令 提交任何更改...

    Linux_Command_Line

    ### Linux Command Line 知识点详解 #### 一、引言 在《Linux Command Line》这本书中,作者为我们提供了一系列关于Linux命令行的基础知识与实用技巧。这些内容不仅适合Linux初学者快速上手,同时也为有经验的用户...

    mesos-xargs:轻松使用Xargs在Mesos集群上执行命令!

    mesos-xargs 有一个Mesos集群,您只想在上面运行东西吗? 曾经希望您可以只使用xargs吗? 不? 好的。 您是个理智的人,应该像普通人一样使用Hadoop。安装Mesos python绑定有点混乱。 主mesos绑定需要与主Mesos...

    node-xargs:构建和执行命令行,node.js 方式

    这是“xargs”unix 命令的流媒体版本。安装npm install --save xargs用法该模块导出的函数返回一个双工流,该流将写入其中的所有字符串块收集到一个数组中,该数组用作指定命令的参数向量: var streamify = ...

    windows dos下xargs使用

    在Windows DOS环境下,xargs命令通常不是内置的,但可以通过安装一些开源工具,如Cygwin,来获得这个功能。Cygwin是一个提供Linux环境下的命令行工具在Windows上运行的开源软件。在这个环境中,我们可以找到`xargs....

    xargs_for_window

    在IT领域,尤其是在Linux和Unix系统中,`xargs`是一个非常实用的命令行工具,用于从标准输入中读取数据并将其作为参数传递给其他命令。然而,标题中的"xargs_for_window"表明我们将讨论如何在Windows环境中实现类似...

    Linux基础之xargs命令的入门实例

    xargs命令有两个要点。第一,你必须列出目标文件。第二,你必须指定对每个文件需要执行的命令或脚本。 xargs命令被用来处理分布在不同目录下的文件: 计算所有文件的行数 打印指定文件的第一行 对每个文件执行一...

    Small.Sharp.Software.Tools.epub

    Hands-on activities and exercises will cement your newfound knowledge and give you the confidence to use the CLI to its fullest potential. And if you're worried you'll wreck your system, this book ...

    Linux中xargs命令的使用方式.doc

    xargs [OPTIONS] [COMMAND [initial-arguments]] ``` 3. **实例** - **创建文件**:创建三个文件`file1`, `file2`, `file3`,使用`echo`和`xargs`结合`touch`命令。 ``` [root@localhost ~]# echo "file1 file...

    可在windows使用的类linux工具xargs

    然而,有一款名为`xargs`的工具,它为Windows用户提供了类Linux的体验,使得在Windows上进行文件处理变得更加方便。`xargs`是Linux系统中一个非常强大的命令,它能将`find`命令或者其他命令的输出作为参数传递给其他...

    Linux Shell Scripting Cookbook

    Fast command-line navigation using pushd and popd 126 Counting number of lines, words, and characters in a file 128 Printing directory tree 129 Chapter 4: Texting and Driving 131 Introduction 132 ...

    git-xargs是一个命令行工具(CLI),可使用单个命令在多个Github存储库中进行更新。-Golang开发

    git-xargs是一个命令行工具...您给git-xargs:运行回购清单的脚本或命令,而git-xargs将:克隆每个回购对您的脚本或命令运行指定的回购提交任何更改打开拉取请求可提供发生的一切的详细报告例如,您是否需要添加特定的

    Linux文件查找命令find,xargs详述

    Linux文件查找命令`find`和`xargs`是Linux系统中非常重要的工具,它们帮助用户在文件系统中高效地定位和处理文件。本文将详细解释这两个命令的基本使用和相关选项。 `find`命令是一个功能强大的命令,用于在指定...

    Linux xargs 命令用法详解

    Linux xargs 命令 xargs 是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。 xargs 可以将管道或标准输入(stdin)数据转换成命令行参数,也能够从文件的输出中读取数据。 xargs 也可以将单行或多行文本...

    linux运维常用命令

    | === Welcome to Tunoff services === | +--------------------------------------------------------------+ EOF 说明:该命令可以在 SHELL 中显示多个信息,cat 选项可以显示文件,EOF 选项指定结束符。 10. for...

    Linux 下xargs命令详解及xargs与管道的区别

    **xargs:** 相比之下,`xargs`不仅能够传递数据,还能够根据需要构建复杂的命令。它从标准输入中读取数据,并将这些数据作为参数传递给指定的命令,从而可以执行更复杂的操作,比如删除或移动文件。 例如,使用`...

    【IT十八掌徐培成】Linux基础-04.xargs-mount-umount.zip

    -type f -name 'pattern' | xargs command`,其中find命令用于查找符合特定条件的文件,然后通过管道(|)传递给xargs,最后由xargs调用command执行操作。 接着,我们转向Linux的文件系统挂载和卸载。在Linux中,...

    xargs的简单应用

    只要介绍XARGS的应用

    Linux_的命令find_xargs详述(2).txt

    ### Linux的find与xargs命令详解 在Linux系统中,`find`与`xargs`是两个非常强大的命令行工具,它们分别用于查找文件和执行命令。本文将深入解析这两个命令的功能、用法以及如何结合使用,以实现高效的数据处理。 ...

Global site tag (gtag.js) - Google Analytics