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:
- stdin (standard input) β usually your keyboard.
- stdout (standard output) β usually your screen.
- 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.txtalready 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 ofaudit.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.loginstead 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
/etcand errors from/no_such_directorygo intocombined.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.txtand 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 auxlists all running processes.grep apachesearches for the word “apache”.- Only the processes related to
apacheare displayed.
Another Example:
cat users.txt | sort | uniq
cat users.txtoutputs file content.sortorders it alphabetically.uniqremoves 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
- Save a list of files:
ls -l /etc > etc_files.txt
- Append system info to a log:
date >> system.log
- Redirect errors:
cat missing_file 2> missing.log
- Combine output and error:
df -h &> disk_report.txt
- Pipe and filter:
dmesg | grep -i error
β Key Points for RHCSA Exam
>β overwrite file with command output>>β append command output to file2>β 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.
