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