如何在linux中的命令中间使用xargs传递所有参数

时间:2013-02-01 03:23:31

标签: linux xargs

我想在Linux上将所有文件作为单个参数传递,但我无法做到这一点。

这是有效的

ls | sort -n | xargs  -i pdftk  {} cat output combinewd2.pdf

这会为每个命令传递一个参数,但我希望所有命令都在一个命令中。

9 个答案:

答案 0 :(得分:18)

使用<div th:fragment='paginationbar'> <div> <ul class='pagination pagination-centered'> <li th:class="${page.firstPage}?'disabled':''"><span th:if='${page.firstPage}'>← First</span> <a th:if='${not page.firstPage}' th:href='@{${page.url}(page=0,size=${page.size})}'>← First</a></li> <li th:class="${page.hasPreviousPage}? '' : 'disabled'"><span th:if='${not page.hasPreviousPage}'>«</span> <a th:if='${page.hasPreviousPage}' th:href='@{${page.url}(page=${page.number-2},size=${page.size})}' title='Go to previous page'>«</a></li> <li th:each='item : ${page.items}' th:class="${item.current}? 'active' : ''"><span th:if='${item.current}' th:text='${item.number}'>1</span> <a th:if='${not item.current}' th:href='@{${page.url}(page=${item.number-1},size=${page.size})}'><span th:text='${item.number}'>1</span></a></li> <li th:class="${page.hasNextPage}? '' : 'disabled'"><span th:if='${not page.hasNextPage}'>»</span> <a th:if='${page.hasNextPage}' th:href='@{${page.url}(page=${page.number},size=${page.size})}' title='Go to next page'>»</a></li> <li th:class="${page.lastPage}? 'disabled' : ''"><span th:if='${page.lastPage}'>Last →</span> <a th:if='${not page.lastPage}' th:href='@{${page.url}(page=${page.totalPages - 1},size=${page.size})}'>Last →</a></li> </ul> </div> </div> 选项:

-I

输出:

echo prefix | xargs -I % echo % post

答案 1 :(得分:16)

这是一种方法

pdftk $(ls | sort -n) cat output combinewd2.pdf

或使用反引号

pdftk `ls | sort -n` cat output combinewd2.pdf

正如评论中所指出的,这不适用于包含空格的文件名。在这种情况下,您可以使用eval

eval pdftk $(while IFS= read -r file; do
    echo \"$file\"
done < <(ls | sort -n)) cat output combinewd2.pdf

假设有两个名为“0 foo”和“1 bar”的文件,那么eval的结果将是所需的命令,文件名用双引号:

pdftk " 0 foo " " 1 bar " cat output combinewd2.pdf

如果文件名可能包含换行符,则使用find命令,请参阅@joeytwiddle在@ andrewdotn的回答评论中的讨论。 以下解决方案还使用sed命令处理带双引号的文件名以转义双引号:

eval pdftk $(while IFS= read -r -d '' file; do
    echo \"$file\"
done < <(find . -maxdepth 1 -type f -print0 | \
    sed 's/"/\\"/g'| sort -zn)) cat output combinewd2.pdf

答案 2 :(得分:9)

这很难看,但您可以运行sh -c并访问xargs传递的"${@}"参数列表,如下所示:

ls | sort -n | xargs -d'\n' sh -c 'pdftk "${@}" cat output combinewd2.pdf' "${0}"

最后的额外"${0}"是因为,sh man page says

  

-c 字符串

     

如果存在 -c 选项,则从 string 中读取命令。如果在字符串之后有参数,则会将它们分配给位置参数,从 $ 0 开始。

为了测试这个,让我们首先创建一些名称复杂的文件,这些文件会破坏大多数其他解决方案:

$ seq 1 100 | xargs -I{} touch '{} with "spaces"'
$ ls
1 with "spaces"    31 with "spaces"  54 with "spaces"  77 with "spaces"
10 with "spaces"   32 with "spaces"  55 with "spaces"  78 with "spaces"
100 with "spaces"  33 with "spaces"  56 with "spaces"  79 with "spaces"
11 with "spaces"   34 with "spaces"  57 with "spaces"  8 with "spaces"
12 with "spaces"   35 with "spaces"  58 with "spaces"  80 with "spaces"
13 with "spaces"   36 with "spaces"  59 with "spaces"  81 with "spaces"
14 with "spaces"   37 with "spaces"  6 with "spaces"   82 with "spaces"
15 with "spaces"   38 with "spaces"  60 with "spaces"  83 with "spaces"
16 with "spaces"   39 with "spaces"  61 with "spaces"  84 with "spaces"
17 with "spaces"   4 with "spaces"   62 with "spaces"  85 with "spaces"
18 with "spaces"   40 with "spaces"  63 with "spaces"  86 with "spaces"
19 with "spaces"   41 with "spaces"  64 with "spaces"  87 with "spaces"
2 with "spaces"    42 with "spaces"  65 with "spaces"  88 with "spaces"
20 with "spaces"   43 with "spaces"  66 with "spaces"  89 with "spaces"
21 with "spaces"   44 with "spaces"  67 with "spaces"  9 with "spaces"
22 with "spaces"   45 with "spaces"  68 with "spaces"  90 with "spaces"
23 with "spaces"   46 with "spaces"  69 with "spaces"  91 with "spaces"
24 with "spaces"   47 with "spaces"  7 with "spaces"   92 with "spaces"
25 with "spaces"   48 with "spaces"  70 with "spaces"  93 with "spaces"
26 with "spaces"   49 with "spaces"  71 with "spaces"  94 with "spaces"
27 with "spaces"   5 with "spaces"   72 with "spaces"  95 with "spaces"
28 with "spaces"   50 with "spaces"  73 with "spaces"  96 with "spaces"
29 with "spaces"   51 with "spaces"  74 with "spaces"  97 with "spaces"
3 with "spaces"    52 with "spaces"  75 with "spaces"  98 with "spaces"
30 with "spaces"   53 with "spaces"  76 with "spaces"  99 with "spaces"
$  ls | sort -n | xargs -d'\n' sh -c 'set -x; pdftk "${@}" cat output combinewd2.pdf' "${0}"
+ pdftk '1 with "spaces"' '2 with "spaces"' '3 with "spaces"' '4 with "spaces"' '5 with "spaces"' '6 with "spaces"' '7 with "spaces"' '8 with "spaces"' '9 with "spaces"' '10 with "spaces"' '11 with "spaces"' '12 with "spaces"' '13 with "spaces"' '14 with "spaces"' '15 with "spaces"' '16 with "spaces"' '17 with "spaces"' '18 with "spaces"' '19 with "spaces"' '20 with "spaces"' '21 with "spaces"' '22 with "spaces"' '23 with "spaces"' '24 with "spaces"' '25 with "spaces"' '26 with "spaces"' '27 with "spaces"' '28 with "spaces"' '29 with "spaces"' '30 with "spaces"' '31 with "spaces"' '32 with "spaces"' '33 with "spaces"' '34 with "spaces"' '35 with "spaces"' '36 with "spaces"' '37 with "spaces"' '38 with "spaces"' '39 with "spaces"' '40 with "spaces"' '41 with "spaces"' '42 with "spaces"' '43 with "spaces"' '44 with "spaces"' '45 with "spaces"' '46 with "spaces"' '47 with "spaces"' '48 with "spaces"' '49 with "spaces"' '50 with "spaces"' '51 with "spaces"' '52 with "spaces"' '53 with "spaces"' '54 with "spaces"' '55 with "spaces"' '56 with "spaces"' '57 with "spaces"' '58 with "spaces"' '59 with "spaces"' '60 with "spaces"' '61 with "spaces"' '62 with "spaces"' '63 with "spaces"' '64 with "spaces"' '65 with "spaces"' '66 with "spaces"' '67 with "spaces"' '68 with "spaces"' '69 with "spaces"' '70 with "spaces"' '71 with "spaces"' '72 with "spaces"' '73 with "spaces"' '74 with "spaces"' '75 with "spaces"' '76 with "spaces"' '77 with "spaces"' '78 with "spaces"' '79 with "spaces"' '80 with "spaces"' '81 with "spaces"' '82 with "spaces"' '83 with "spaces"' '84 with "spaces"' '85 with "spaces"' '86 with "spaces"' '87 with "spaces"' '88 with "spaces"' '89 with "spaces"' '90 with "spaces"' '91 with "spaces"' '92 with "spaces"' '93 with "spaces"' '94 with "spaces"' '95 with "spaces"' '96 with "spaces"' '97 with "spaces"' '98 with "spaces"' '99 with "spaces"' '100 with "spaces"' cat output combinewd2.pdf

所有参数都被正确引用。请注意,如果任何文件名包含换行符,并且ls -v基本上是ls | sort -n,则会失败。

答案 3 :(得分:4)

这应该适用于包含空格,换行符,撇号和引号的文件名(所有这些都可以在UNIX文件系统上使用):

#include <thread>
#include <memory>
#include <iostream>

class Result
{
public:
    Result() : x(0) { }
    int x;
};


void RunSimulation(std::unique_ptr<Result> result)
{
    result->x = 10;
    std::cout << result->x << std::endl;
}


int main()
{
    std::unique_ptr<Result> result = std::make_unique<Result>();
    std::thread t(RunSimulation, std::move(result));
    t.join();
}

如果你知道你正在处理简单的文件名,那么与接受的答案相比,这可能是过度的。

但是如果你正在编写一个将来会再次使用的脚本,那么当它遇到异常(但是有效)的输入时,它有一天不会爆炸。

这基本上是对andrewdotn的答案的改编,它以零字节而不是换行符终止输入文件,因此保留了包含一个或多个换行符的文件名。

相应的选项find . -maxdepth 1 -type f -print0 | sort -zn | xargs -0 sh -c 'pdftk "$@" cat output combinewd2.pdf' "$0" -print0-z告诉每个程序输入/输出应由零字节分隔。三个不同的程序,三个不同的论点!

答案 4 :(得分:4)

这是我针对相同问题所做的,实际上已在生产中使用:

cat chapter_order-pdf-sample.txt | xargs -J % pdftk % cat output sample.pdf

答案 5 :(得分:2)

我发现的最直观的方法是:

  • 首先使用-I {}和“ echo”构造命令,
  • 然后使用“ bash”执行命令(就像您正在执行shell脚本一样)

以下是将扩展名从“ .txt”重命名为“ .txt.json”的示例:

find .|grep txt$|xargs -I{} echo "mv {} {}.json"|bash

将.txt重命名为.json(删除.txt扩展名)的高级示例

find $PWD|grep txt$|cut -d"." -f1|xargs -I{} echo "mv {}.txt {}.json"|bash

我曾经要求将字符串“文件结尾”附加到所有文件中。

find .|grep txt|xargs -I{} echo "echo End of File >> {}"|bash

如果操作正确,xargs将是所有命令之王!

答案 6 :(得分:1)

这里的问题是,虽然xargs可以使用-i{}将单个参数放在命令的中间,但它拒绝为多个参数执行此操作。这似乎是一种疏忽,最终导致我们遇到很多麻烦!

除上述解决方案外,另一种方法是简单地将文件后面的参数添加到文件列表的末尾。

(
  ls | sort -n
  echo cat
  echo output
  echo combinewd2.pdf
) | xargs -d'\n' pdftk

此方法不适用于包含换行符的文件名。对于那种罕见的情况,您应该将以零字节终止的行传递给xargs,如我的其他答案所示。

答案 7 :(得分:1)

您可以通过将两个调用链接到xargs来完成此操作。使用第一个将所有args链接到一个字符串中,并将其作为参数传递给echo,第二个使用-I将args链放到您想要的地方,如如下:

ls | sort -n | xargs echo | xargs -I {} pdftk {} cat output combinewd2.pdf

答案 8 :(得分:0)

我知道这不是OP的问题,但是我发现这很有用。

如果您想改组参数,可以将labelparallel结合使用。

xargs

GNU-parallel现在可以使用# limit the number of arguments per line echo last first middle last first middle | xargs -n 3 last first middle last first middle 参数随意重排这些参数。

--colsep

您也可以在其中添加常量参数。

echo last first middle last first middle | xargs -n 3 | parallel --colsep=" " echo {2} {3} {1}
first middle last
first middle last