>, >>, |, 2>, etc.)Happy Friday, everyone!
Let's wrap up our week on Essential Tools with a hands-on challenge. This one is all about controlling where your command output goes, a core skill covered in the RHCSA objective "Use input-output redirection."
You need to find all files in the /etc/ directory that end with .conf. But there’s a catch — you need to handle both standard output and errors separately.
found.log.errors.log.What single command would you use to get this done? Drop your solution in the comments below!
all_output.log?Let’s see those one-liners. Have a great weekend!
find /etc/ -type f -name "*.conf" 1> found.log 2> errors.log
Bonus Question:
find /etc/ -type f -name "*.conf" &> all_output.log
@87951469 Spot on !
The bonus can also be done using :
find /etc -type f -name '*.conf' >all_output.log 2>&1
>all_output.log first sends the standard output (stdout) of the command into the file named all_output.log.
2>&1 then redirects the standard error (stderr) stream (which is file descriptor 2) to the exact same location as the standard output stream (file descriptor 1).
Upon reflection, I realized I read the initial and follow-on challenges differently than they likely were intended. On that bent though, here is what I came up with:
# Predicated on being a non-root user; which I believe the initial question should have stipulated.
find /etc -type f -name "*.conf" > found.log 2> errors.log 2>&1 > all_output.log
Thank you.
R,
-Joe Wulf
@Joe_Wulf2112 how is that achieving the objective ? your cmmand attempts to merge everything into one line with conflicting instructions - no ?
@Chetan_Tiwary_ Oh, it doesn't.
In the past I'd done a lot of work needing output to go in particular directions, but now when attempting my answer above, I discover it doesn't work.
I played around with trying to 'do it', and the best I could come up with is two commands.
Thank you.
R,
-Joe Wulf
ok @Joe_Wulf2112 np !
Alternately, we should be able to use the updatedb since /etc dir and it's contents should be included, right?
sudo plocate "*.conf" /etc 1> found.log && 2> errors.log
How about, if, in addition to files ending in ".conf" (i.e. "configuration files"), we ALSO wanted directories containing .conf or .d (e.g. /etc/httpd/conf.d or /etc/yum.repos.d or /etc/sudoers.d as many of our services are moving to a complimentary directory for their "standalone" configurations, rather than monolithic .config files)?
I'm pretty sure this is not perfect, but it's a stab at it:
ls -Ra /etc 2>errors.log | grep -e ".*\.conf" -e ".*\.d" > found.log
EDITING to add:
an alternate for splitting, after sending errors & all into the pipe:
ls -Ra /etc 2>&1 | tee >(grep "Permission denied" > errors.log) >(grep -e ".*\.conf" -e ".*\.d" | tee >(grep -v "Permission denied" > found.log) > /dev/null)
one file collection for the same info:
ls -Ra /etc 2>&1 | grep -e "Permission denied" -e ".*\.conf" -e ".*\.d" > all_output.log
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.