Linux CLI HowTo 13 ๐ง How to Input Output and Redirection
How to Redirect Standard Output to a File
Steps:
- Use the
>operator followed by the filename - The command output will be written to the specified file
- If the file exists, it will be overwritten completely
Example: To save command output to a file:
echo "Hello World" > hello.txt
This creates a file named hello.txt containing “Hello World”
How to Append Output to a File
Steps:
- Use the
>>operator followed by the filename - The command output will be added at the end of the file
- If the file doesn’t exist, it will be created
Example: To add content to an existing file:
echo "New line" >> log.txt
This adds “New line” to the end of log.txt without removing existing content
How to Redirect Input from a File
Steps:
- Use the
<operator followed by the filename - The command will read input from the specified file instead of keyboard
- Useful for processing files with commands that normally expect stdin
Example: To sort contents of a file:
sort < data.txt
This sorts the content of data.txt and displays it on screen
How to Redirect Error Messages to a File
Steps:
- Use the
2>operator followed by error filename - All error messages will be captured in the specified file
- Normal output continues to display on terminal
Example: To capture errors from a command:
ls nonexistent.txt 2> errors.log
This shows normal output but saves any error messages to errors.log
How to Redirect Both Output and Error to Different Files
Steps:
- Use
>for stdout and2>for stderr with different filenames - Standard output goes to first file, errors go to second file
- Both streams can be captured separately
Example: To separate normal output and errors:
command > output.log 2> error.log
This saves regular output to output.log and errors to error.log
How to Redirect Both Output and Error to Same File
Steps:
- Use
&>operator followed by filename - Both standard output and standard error go to the same file
- This captures everything in one location
Example: To capture all output including errors:
command &> complete.log
This saves both normal output and error messages to complete.log
How to Suppress All Output
Steps:
- Use
>or2>with/dev/nullas destination - Redirect either stdout (
>), stderr (2>), or both (&>) - The
/dev/nulldevice discards all data sent to it
Example: To hide both output and errors:
command > /dev/null 2>&1
This discards everything from the command execution
How to Suppress Only Standard Output
Steps:
- Redirect stdout to
/dev/nullusing> - Keep stderr going to terminal or another destination
- Useful when you want to see errors but hide normal output
Example: To hide regular output but show errors:
command > /dev/null
This suppresses normal output while showing any error messages
How to Suppress Only Standard Error
Steps:
- Redirect stderr to
/dev/nullusing2> - Keep stdout going to terminal or another destination
- Useful when you want to see normal output but hide errors
Example: To show normal output but hide errors:
command 2> /dev/null
This shows regular output while discarding error messages
How to Save Command Results to Multiple Files
Steps:
- Use multiple redirection operators in one command
- Redirect stdout to one file and stderr to another
- Combine both operations using separate redirections
Example: To save both output and errors separately:
ls /nonexistent 1> success.txt 2> error.txt
This saves successful output to success.txt and error messages to error.txt
How to Create a Comprehensive Log File
Steps:
- Redirect both stdout and stderr to the same file using
&> - This captures all command output and errors in one location
- Useful for logging complete command execution
Example: To create a full execution log:
process_data.sh &> execution.log
This saves everything from process_data.sh to execution.log, including both normal output and errors
How to Process File Contents with Command
Steps:
- Use
<redirection to feed file content as input - The command reads from the file instead of standard input
- Useful for piping files through text processing commands
Example: To sort file contents:
sort < sorted_data.txt
This sorts the lines in sorted_data.txt and displays the result
How to Capture Command Errors Only
Steps:
- Use
2>redirection to capture only error stream - Redirect stdout normally or suppress it if needed
- This isolates error information for analysis
Example: To collect only errors from a command:
find /etc -name "*.conf" 2> error_report.txt
This finds configuration files and saves only error messages to error_report.txt
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!