我试图复制一堆文件下面的目录和一些文件有空格和单引号在他们的名字。当我尝试用xargs将find和grep串在一起时,我得到以下错误:
find .|grep "FooBar"|xargs -I{} cp "{}" ~/foo/bar
xargs: unterminated quote
对于更健壮地使用xargs有什么建议吗?
这是在Mac OS X 10.5.3 (Leopard)上使用BSD xargs。
我试图复制一堆文件下面的目录和一些文件有空格和单引号在他们的名字。当我尝试用xargs将find和grep串在一起时,我得到以下错误:
find .|grep "FooBar"|xargs -I{} cp "{}" ~/foo/bar
xargs: unterminated quote
对于更健壮地使用xargs有什么建议吗?
这是在Mac OS X 10.5.3 (Leopard)上使用BSD xargs。
当前回答
框架挑战-你正在询问如何使用xargs。答案是:不使用xargs,因为不需要它。
user80168的注释描述了一种直接使用cp的方法,而不需要对每个文件调用cp:
find . -name '*FooBar*' -exec cp -t /tmp -- {} +
这很有效,因为:
cp -t标志允许在靠近cp开头的位置给出目标目录,而不是靠近结束的位置。来自男人cp:
- t,目标目录=目录 将所有SOURCE参数复制到DIRECTORY中
——标志告诉cp将后面的所有内容解释为文件名,而不是标志,因此以-或——开头的文件不会混淆cp;您仍然需要这样做,因为-/——字符由cp解释,而任何其他特殊字符由shell解释。 find -exec命令{}+变体本质上与xargs相同。从人类发现:
-exec command {} + This variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invoca‐ matched files. The command line is built in much the same way that xargs builds its command lines. Only one instance of `{}' is allowed within the command, and (when find is being invoked from a shell) it should be quoted (for example, '{}') to protect it from interpretation by shells. The command is executed in the starting directory. If any invocation returns a non-zero value as exit status, then find returns a non-zero exit status. If find encounters an error, this can sometimes cause an immedi‐ ate exit, so some pending commands may not be run at all. This variant of -exec always returns true.
通过直接在find中使用它,就避免了管道或shell调用的需要,这样您就不需要担心文件名中任何讨厌的字符。
其他回答
我围绕“xargs”创建了一个名为“xargsL”的小型便携式包装器脚本,该脚本可以解决大多数问题。
与xargs相反,xargsL接受每行一个路径名。路径名可以包含除换行符或NUL字节以外的任何字符。
在文件列表中不允许或不支持引用—您的文件名可能包含各种空格、反斜杠、反勾号、shell通配符等等—xargsL将把它们作为文字字符处理,不会造成任何损害。
作为一个额外的特性,如果没有输入,xargsL将不会运行一次命令!
注意区别:
$ true | xargs echo no data
no data
$ true | xargsL echo no data # No output
给xargsL的任何参数都将传递给xargs。
下面是"xargsL" POSIX shell脚本:
#! /bin/sh # Line-based version of "xargs" (one pathname per line which may contain any # amount of whitespace except for newlines) with the added bonus feature that # it will not execute the command if the input file is empty. # # Version 2018.76.3 # # Copyright (c) 2018 Guenther Brunthaler. All rights reserved. # # This script is free software. # Distribution is permitted under the terms of the GPLv3. set -e trap 'test $? = 0 || echo "$0 failed!" >& 2' 0 if IFS= read -r first then { printf '%s\n' "$first" cat } | sed 's/./\\&/g' | xargs ${1+"$@"} fi
将脚本放到$PATH中的某个目录中,不要忘记
$ chmod +x xargsL
那里的脚本使它可执行。
我使用了Bill Star在Solaris上稍作修改的回答:
find . -mtime +2 | perl -pe 's{^}{\"};s{$}{\"}' > ~/output.file
这将在每行周围加上引号。我没有使用“-l”选项,尽管它可能会有所帮助。
我要去的文件列表可能有'-',但没有换行符。我没有使用输出文件与任何其他命令,因为我想回顾什么是发现之前,我只是开始大规模删除他们通过xargs。
我尝试了一下,开始考虑修改xargs,并意识到对于我们在这里讨论的这种用例,用Python简单地重新实现是一个更好的主意。
首先,整个事情有大约80行代码意味着很容易弄清楚发生了什么,如果需要不同的行为,你可以在更短的时间内把它黑进一个新的脚本,而不是在Stack Overflow等地方得到回复。
参见https://github.com/johnallsup/jda-misc-scripts/blob/master/yargs和https://github.com/johnallsup/jda-misc-scripts/blob/master/zargs.py。
使用编写好的yargs(并且安装了Python 3),您可以键入:
find .|grep "FooBar"|yargs -l 203 cp --after ~/foo/bar
一次拷贝203个文件。(这里203只是一个占位符,当然,使用像203这样奇怪的数字可以清楚地表明这个数字没有其他意义。)
如果你真的想要更快,不需要Python,可以把zargs和yargs作为原型,然后用c++或C重写。
如果你正在使用Bash,你可以通过mapfile将stdout转换为一个行数组:
find . | grep "FooBar" | (mapfile -t; cp "${MAPFILE[@]}" ~/foobar)
好处是:
它是内置的,所以更快。 一次执行所有文件名的命令,因此速度更快。 您可以将其他参数附加到文件名。对于cp,您还可以: 找到。-name '*FooBar*' -exec cp -t ~/ FooBar——{}+ 但是,有些命令不具备这样的特性。
的缺点:
如果文件名太多,可能伸缩性不好。(极限?我不知道,但我已经测试了10 MB的列表文件,其中包括10000多个文件名没有问题,在Debian下)
嗯…谁知道Bash在OS X上是否可用呢?
此方法适用于Mac OS X v10.7.5 (Lion):
find . | grep FooBar | xargs -I{} cp {} ~/foo/bar
我还测试了你发布的确切语法。这在10.7.5上也能正常工作。