Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to use find along with stderr

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/?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading