Cat with wildcard
12,034
Solution 1
for dir in */folder_*/*; do
[[ -d "$dir" ]] && ( cd "$dir" && cat file.a file.b > file.c )
done
I run the cd && cat
in a subshell so you don't have to cd back to where you started.
Solution 2
You can't use globbing for a destination file. You must fully specify the filename. It has nothing to do with cat
specifically.
Solution 3
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; cat
has no concept of wildcards, nor does it know where you are sending its output.
Author by
Admin
Updated on June 04, 2022Comments
-
Admin almost 2 years
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.
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
cat
can't create files (though it does), so I triedtouch */folder_*/*/file.c; cat */folder_*/*/file.a */folder_*/*/file.b > */folder_*/*/file.c
but again it does not work with
cat
or eventouch
. -
Admin almost 12 yearsSo the only alternative I can come up with is to loop through all the directories and subdirectories by
ls
, build up and save the entire path then execute thecat
command for each file instance. Oh well... anyway thx. -
SourceSeeker almost 12 years@lorendarith: Don't loop with
ls
- use globbingfor f in *