我试图复制一堆文件下面的目录和一些文件有空格和单引号在他们的名字。当我尝试用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。
当前回答
我使用了Bill Star在Solaris上稍作修改的回答:
find . -mtime +2 | perl -pe 's{^}{\"};s{$}{\"}' > ~/output.file
这将在每行周围加上引号。我没有使用“-l”选项,尽管它可能会有所帮助。
我要去的文件列表可能有'-',但没有换行符。我没有使用输出文件与任何其他命令,因为我想回顾什么是发现之前,我只是开始大规模删除他们通过xargs。
其他回答
我围绕“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
那里的脚本使它可执行。
查看一下在xargs中使用——null命令行选项和find中的-print0选项。
这更有效,因为它不会多次运行“cp”:
find -name '*FooBar*' -print0 | xargs -0 cp -t ~/foo/bar
我也遇到了同样的问题。以下是我的解决方法:
find . -name '*FoooBar*' | sed 's/.*/"&"/' | xargs cp ~/foo/bar
我使用sed将每一行输入替换为同一行,但是用双引号括起来。在sed手册页中,“…替换中出现的&号('' & ")将被匹配RE…"的字符串替换。"——在本例中,.*表示整行。
这解决了xargs: unterminated quote错误。
bill_starr的Perl版本不能很好地用于嵌入换行符(只处理空格)。对于那些没有GNU工具的Solaris,一个更完整的版本可能是(使用sed)…
find -type f | sed 's/./\\&/g' | xargs grep string_to_find
根据需要调整find和grep参数或其他命令,但sed将修复嵌入的换行符/空格/制表符。