David Winiecki
David Winiecki

Reputation: 4213

How to hide cat errors?

( find -print0 | xargs -0 cat ) | wc -l (from How to count all the lines of code in a directory recursively?) prints the total number of lines in all files in all subdirectories. But it also prints a bunch of lines like cat: ./x: Is a directory.

I tried ( find -print0 | xargs -0 cat ) | wc -l &> /dev/null (and also 2> /dev/null and > /dev/null 2>&1) but the messages are still printed to the shell.

Is it not possible to hide this output?

( find -type f -print0 | xargs -0 cat ) | wc -l overcomes this problem, but I'm still curious why redirecting stderr doesn't work, and if there is a more general purpose way to hide errors from cat.

Upvotes: 1

Views: 469

Answers (2)

lxg
lxg

Reputation: 13117

If you want find only to find “regular” files, you must use find -type f ….

By the way, if you want to calculate lines of code, you should take a look at ohcount.

Upvotes: 0

tkocmathla
tkocmathla

Reputation: 911

You need to redirect the stderr stream of the cat command to /dev/null. What you have done is redirected the stderr stream of wc. Try this:

( find -print0 | xargs -0 cat 2>/dev/null ) | wc -l

Upvotes: 3

Related Questions