r/programminghelp • • Feb 02 '21

Other Hello World!

First time posting here and asking for help. Anyways, I’m working in the Mac terminal and my current working directory contains a sub-directory called “docs”. And I want to query all the files in it. I imagine doing this is simply the command

ls docs/*

But what about if I want to use cat (or perhaps a combination of cat and ls where the first command has to be cat)?

I’m thinking to use an input redirection instead:

cat < (ls docs/*)

But it’s giving me a syntax error near unexpected token ‘(‘ error. Does anyone know why or what fix I could do?

Thanks in advanced!

5 Upvotes

2 comments sorted by

1

u/ConstructedNewt MOD Feb 02 '21 edited Feb 02 '21

The redirection would be a file-redirection cat <(ls docs/*) - no space before the parenthesis

But it won't work

Try a loop or xargs

for file in docs/*; do
    cat $file
done

or

ls docs/* | xargs cat

(Or something like that)

You cannot cat multiple files like you try

Edit / bonus:

Redirection < redirects a file-lile object to stdin

Redirection <(...) redirects the subshell's stdout to an intermediate tmp file and returns that filename

Edit 2: what you probably want is something more useful, like less which can handle reading many files

1

u/Co0perat0r Feb 02 '21

I’m not sure if this is programming help, but I believe your problem is that you’re trying to run cat on multiple files, which I don’t believe is possible. When you use ls, it lists every file in the directory, and I believe cat can only be used on a single file.