Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cat with wildcard

Tags:

bash

shell

unix

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 tried

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

but again it does not work with cat or even touch.

like image 591
lorendarith Avatar asked Oct 16 '25 05:10

lorendarith


2 Answers

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

like image 62
Dennis Williamson Avatar answered Oct 18 '25 18:10

Dennis Williamson


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.

like image 41
tripleee Avatar answered Oct 18 '25 17:10

tripleee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!