Use input-output redirection (>,Β >>,Β |,Β 2>, etc.)

1. Understand and Use Essential Tools

πŸ“˜Red Hat Certified System Administrator (RHCSA – EX200)


Input-output (I/O) redirection is a core skill in Linux. It allows you to control where a command gets its input and where its output goes. In Linux, every process has three standard data streams:

  1. stdin (standard input) – usually your keyboard.
  2. stdout (standard output) – usually your screen.
  3. stderr (standard error) – usually your screen, used for error messages.

Redirection lets you send input from files, send output to files, or even chain commands together. This is crucial for scripting, managing logs, and automating tasks.


1. Redirecting Standard Output (>)

  • > sends the output of a command to a file instead of the screen.
  • If the file exists, it will overwrite it.

Example:

ls -l > file_list.txt
  • Lists all files in the current directory.
  • Output is saved in file_list.txt.
  • If file_list.txt already exists, it will be replaced.

2. Appending Standard Output (>>)

  • >> sends output to a file, but adds it to the end of the file instead of overwriting it.

Example:

echo "New user created" >> audit.log
  • Adds the text "New user created" to the end of audit.log.
  • This is useful for logs or records.

3. Redirecting Standard Error (2>)

  • Errors from commands go to stderr. You can redirect errors to a file with 2>.
  • 2> means β€œsend error messages to this file”.

Example:

ls /no_such_directory 2> error.log
  • This command will fail because the directory doesn’t exist.
  • Instead of showing the error on the screen, it is written to error.log.

Appending errors:

ls /no_such_directory 2>> error.log
  • Adds the error to the end of error.log instead of overwriting it.

4. Redirecting Both Output and Error (&>)

  • Sometimes you want all output, both normal and error messages, in one file.

Example:

ls /etc /no_such_directory &> combined.log
  • Normal output of /etc and errors from /no_such_directory go into combined.log.

5. Using Input Redirection (<)

  • < sends the contents of a file to a command as input instead of typing manually.

Example:

sort < unsorted_users.txt
  • Takes data from unsorted_users.txt and sorts it.
  • Useful for processing data files automatically.

6. Piping (|)

  • | sends the output of one command as input to another command.
  • Think of it as connecting commands together.

Example:

ps aux | grep apache
  • ps aux lists all running processes.
  • grep apache searches for the word “apache”.
  • Only the processes related to apache are displayed.

Another Example:

cat users.txt | sort | uniq
  • cat users.txt outputs file content.
  • sort orders it alphabetically.
  • uniq removes duplicates.
  • Useful for cleaning and analyzing log files.

7. Combining Redirection and Pipes

You can mix them for more complex tasks.

Example:

ps aux | grep apache > apache_processes.txt 2> errors.log
  • Normal output goes to apache_processes.txt.
  • Any errors go to errors.log.

8. Common Exam Commands to Practice

  1. Save a list of files:
ls -l /etc > etc_files.txt
  1. Append system info to a log:
date >> system.log
  1. Redirect errors:
cat missing_file 2> missing.log
  1. Combine output and error:
df -h &> disk_report.txt
  1. Pipe and filter:
dmesg | grep -i error

βœ… Key Points for RHCSA Exam

  • > β†’ overwrite file with command output
  • >> β†’ append command output to file
  • 2> β†’ redirect errors to a file
  • < β†’ take input from a file
  • | β†’ pipe output to another command
  • &> β†’ redirect both output and errors together
  • Combine commands for automation and analysis

Tip: Always check your files after redirection using cat, less, or tail to ensure output went where you expected.

Buy Me a Coffee