bash 带通配符的猫

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10517340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:14:13  来源:igfitidea点击:

Cat with wildcard

bashshellunix

提问by lorendarith

The question is maybe trivial but I can't get it to work. I just want to merge 2 particular files present in multiple specific folders into a new single file again in each specific folder.

这个问题可能是微不足道的,但我无法让它发挥作用。我只想将多个特定文件夹中存在的 2 个特定文件再次合并到每个特定文件夹中的一个新文件中。

cat */folder_*/*/file.a */folder_*/*/file.b > */folder_*/*/file.c

but it does not work 'cause

但它不起作用,因为

-bash: */folder_*/*/file.c: No such file or directory

So I thought maybe for some reason catcan't create files (though it does), so I tried

所以我想也许由于某种原因cat无法创建文件(尽管它确实如此),所以我尝试了

touch */folder_*/*/file.c; cat */folder_*/*/file.a */folder_*/*/file.b > */folder_*/*/file.c

but again it does not work with cator even touch.

但同样它不适用于cat甚至touch.

采纳答案by glenn Hymanman

for dir in */folder_*/*; do 
  [[ -d "$dir" ]] && ( cd "$dir" && cat file.a file.b > file.c )
done

I run the cd && catin a subshell so you don't have to cd back to where you started.

cd && cat在一个子shell 中运行,所以你不必回到你开始的地方。

回答by Paused until further notice.

You can't use globbing for a destination file. You must fully specify the filename. It has nothing to do with catspecifically.

您不能对目标文件使用通配符。您必须完全指定文件名。它与cat具体无关。

回答by tripleee

Maybe you want something like this;

也许你想要这样的东西;

for a in  */folder_*/*/file.a; do
    # maybe continue if b missing
    cat "$a" "${a%.a}.b" >"${a%.a}.c"
done

Wildcards and redirections are processed by the shell; cathas no concept of wildcards, nor does it know where you are sending its output.

通配符和重定向由 shell 处理;cat没有通配符的概念,也不知道您将其输出发送到哪里。