Skip to content
Advertisement

Trouble redirecting an error in pipeline using Bash?

ls -lhR /etc/ | egrep *.conf$ >/home/student/total_size.txt 2>/home/student/error.txt

So I used this command to get all .conf files from /etc/. I want the output in total_size.txt and my errors in error.txt. My output looks good, but the errors won’t redirect to error.txt, they appear in my terminal:

ls: cannot open directory '/etc/cups/ssl': Permission denied

I don’t know what to do; I tried 2>> instead of 2> but it won’t work either.

Advertisement

Answer

This happens because ls‘s stderr still points to the terminal. You need to wrap pipeline in curly braces, and do the redirection outside. E.g:

{ ls -lhR /etc/ | egrep *.conf$; } >/home/student/total_size.txt 2>/home/student/error.txt
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement