我试图复制一堆文件下面的目录和一些文件有空格和单引号在他们的名字。当我尝试用xargs将find和grep串在一起时,我得到以下错误:

find .|grep "FooBar"|xargs -I{} cp "{}" ~/foo/bar
xargs: unterminated quote

对于更健壮地使用xargs有什么建议吗?

这是在Mac OS X 10.5.3 (Leopard)上使用BSD xargs。


当前回答

你可能需要像这样grep Foobar目录:

find . -name "file.ext"| grep "FooBar" | xargs -i cp -p "{}" .

其他回答

这更有效,因为它不会多次运行“cp”:

find -name '*FooBar*' -print0 | xargs -0 cp -t ~/foo/bar

请注意,其他答案中讨论的大多数选项在不使用GNU实用程序的平台上都不是标准的(例如Solaris、AIX、HP-UX)。关于“标准”xargs行为,请参阅POSIX规范。

我还发现xargs的行为,即它至少运行一次命令,即使没有输入,这是一个麻烦。

我写了我自己的xargs (xargl)的私人版本来处理名称中的空格问题(只有换行符分开-尽管'find…-print0'和'xargs -0'的组合非常简洁,因为文件名不能包含ASCII NUL '\0'字符。我的xargl还没有完整到值得发布的程度——特别是因为GNU的工具至少和它一样好。

你可能需要像这样grep Foobar目录:

find . -name "file.ext"| grep "FooBar" | xargs -i cp -p "{}" .

你可以把所有这些组合成一个find命令:

find . -iname "*foobar*" -exec cp -- "{}" ~/foo/bar \;

这将处理包含空格的文件名和目录。您可以使用name来获得区分大小写的结果。

注意:传递给cp的——标志阻止它处理以-开头的文件。

我围绕“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

那里的脚本使它可执行。