1

我有一个非常微妙的 bash 问题:

给定一个目录 foo

$ ls
foo.tgz bar.tgz baz.tgz

我想生成一个 bash 单行,它提取模式的第一个 tarball,例如:

bash -c "tar -zxvf foo.tgz file1" # fine
bash -c 'tar -zxvf   *.tgz file1" # oups trying to extract bar.tgz from foo.tgz!

是否有可能将模式匹配限制为第一个扩展参数?

细化:

find -iname '*.tgz' | xargs tar -zxvf # oups! cannot add restriction to only extract file1
4

2 回答 2

2

当然,试试这个:

tar -zxvf $(ls *.tgz | head -1) file1

你应该考虑如果没有匹配的模式会发生什么......

于 2013-02-21T13:06:30.897 回答
2

使用-quit主要与find

find -iname '*.tgz' -exec tar -zxvf '{}' \; -quit

由于动作是从左到右处理的,这将tar在第一次匹配时运行,然后结束find命令。

于 2013-02-21T14:57:33.550 回答