Home > Mobile >  Is it possible to use find along with stderr
Is it possible to use find along with stderr

Time:01-21

I am facing an issue with STDERR,I hope I am missing something. Please look at the below syntax and output.

find /etc/sudoers.d -type f -exec cat {}   | grep kali 2> /dev/null

Output:

cat: /etc/sudoers.d/README: Permission denied
cat: /etc/sudoers.d/kali-grant-root: Permission denied
cat: /etc/sudoers.d/ospd-openvas: Permission denied
cat: /etc/sudoers.d/live: Permission denied

So as per my understanding, the find is looking for a file type in "/etc/sudoers" and the output is executed using the cat command which is further grepped, so finally I will get a permission denied error because I am not a root user but 2> /dev/null should ignore the stedrr, Am I right/?

CodePudding user response:

It's find that produces the output you see, not grep. Where possible, eliminate the source of the error instead of ignoring all error output via redirection. In this case, you only want to call cat on the files you have permission to read.

find /etc/sudoers.d -type f -exec test -r \; -exec cat {}   | grep kali

Note that you don't need a pipe at all; let find execute grep for you.

find /etc/sudoers.d -type f -exec test -r \; -004 -exec grep kali {}  

CodePudding user response:

2>/dev/null only applies to a single command, not the full pipeline. In your case, only stderr of grep is redirected, but not stderr of find.

Either redirect each command or use a command group:

find /etc/sudoers.d -type f -exec cat {}   2> /dev/null | grep kali 2> /dev/null
{ find /etc/sudoers.d -type f -exec cat {}   | grep kali; } 2> /dev/null

It is also possible to use a a subshell:

(find /etc/sudoers.d -type f -exec cat {}   | grep kali) 2> /dev/null

CodePudding user response:

Grouping all together:

{ find /etc/sudoers.d -type f -exec cat {}   | grep kali; }  2>/dev/null
  • Related