cancel
Showing results for 
Search instead for 
Did you mean: 
TudorRaduta
Community Manager
Community Manager
  • 420 Views

RHCSA Practice: Mastering I/O Redirection

Friday Challenge: Mastering Redirection (>, >>, |, 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."

The Scenario

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.

  • Save the list of found files (standard output) to found.log.
  • Save any “Permission denied” messages (standard error) to errors.log.
  • Do it all with a single command line.

How would you solve it?

What single command would you use to get this done? Drop your solution in the comments below!

  • Bonus Question: How would you modify it to send both outputs (success and error) to the same file called all_output.log?

Let’s see those one-liners. Have a great weekend!

8 Replies
87951469
Flight Engineer
Flight Engineer
  • 381 Views

find /etc/ -type f -name "*.conf"  1> found.log 2> errors.log

Bonus Question: 

find /etc/ -type f -name "*.conf"  &> all_output.log 

Chetan_Tiwary_
Community Manager
Community Manager
  • 339 Views

@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).

Joe_Wulf2112
Mission Specialist
Mission Specialist
  • 346 Views

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

 

 

Thank you.
R,
-Joe Wulf
Chetan_Tiwary_
Community Manager
Community Manager
  • 327 Views

@Joe_Wulf2112 how is that achieving the objective ? your cmmand attempts to merge everything into one line with conflicting instructions - no ?

0 Kudos
Joe_Wulf2112
Mission Specialist
Mission Specialist
  • 268 Views

@Chetan_Tiwary_  Oh, it doesn't.    I had glommed the initial question and the bonus in my brain, yet still attempted to solve it, then winged it off the top of my head. 

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

Thank you.
R,
-Joe Wulf
Chetan_Tiwary_
Community Manager
Community Manager
  • 267 Views

ok @Joe_Wulf2112 np !

0 Kudos
LittleCoconut
Mission Specialist
Mission Specialist
  • 200 Views

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

0 Kudos
Andrew
Flight Engineer
Flight Engineer
  • 170 Views

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

 

 

0 Kudos
Join the discussion
You must log in to join this conversation.