I have two cases to share where I didn't find the stderr redirection (2>) working as expected:
In both the cases the file/directory, on which the operations are being performed, don't exist.
Case 1:
When you give 2 commands in pipe:
[root@example ~]# cat test2.txt | grep -i 'third'
cat: test2.txt: No such file or directory
[root@example ~]# cat test2.txt | grep -i 'third' 2> /dev/null
cat: test2.txt: No such file or directory
But redirection works simply with grep alone as a command:
[root@example ~]# grep -i 'third' test2.txt
grep: test2.txt: No such file or directory
[root@example ~]# grep -i 'third' test2.txt 2> /dev/null
<redirection worked properly>
I know that using cat and then grep doesn't make sense but I am just being curious here.
Case 2:
When you use the tree
command:
[root@example ~]# tree /var/crash2
/var/crash2 [error opening dir]
0 directories, 0 files
[root@example ~]# tree /var/crash2 2> /dev/null
/var/crash2 [error opening dir]
0 directories, 0 files
But this isn't the case with ls
command:
[root@example ~]# ls -ld /var/crash2
ls: cannot access /var/crash2: No such file or directory
[root@example ~]# ls -ld /var/crash2 2> /dev/null
<redirection worked properly>
Where's the problem here?
cat having an error not grep as mentioned by Cyrus
tree command shows "error opening dir" in standard output
So the command shows if we use 2>/dev/null
root@ubuntu:~# tree -if test 2>/dev/null
test [error opening dir]
0 directories, 0 files
The same thing you mentioned was reported as a bug before, refer: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=76594