0
0
Linux CLIscripting~5 mins

stdout redirection (>, >>) in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to save the output of a command to a file instead of seeing it on the screen. Stdout redirection lets you do this by sending the output to a file, either replacing its content or adding to it.
When you want to save the list of files in a folder to a text file for later review.
When you want to keep a log of command outputs over time by adding new results to the same file.
When you want to create a report file from a command output without manually copying it.
When you want to overwrite an old file with fresh command output.
When you want to combine outputs from multiple commands into one file.
Commands
This command lists files in the current folder and saves the output to 'files.txt', replacing any existing content.
Terminal
ls > files.txt
Expected OutputExpected
No output (command runs silently)
This command shows the content of 'files.txt' to verify that the output was saved correctly.
Terminal
cat files.txt
Expected OutputExpected
example.txt notes.doc script.sh
This command adds the text 'New line' at the end of 'files.txt' without removing the existing content.
Terminal
echo "New line" >> files.txt
Expected OutputExpected
No output (command runs silently)
This command shows the updated content of 'files.txt' to confirm the new line was added.
Terminal
cat files.txt
Expected OutputExpected
example.txt notes.doc script.sh New line
Key Concept

If you remember nothing else from this pattern, remember: use > to overwrite a file with command output and >> to add to the file without erasing it.

Common Mistakes
Using > when you want to add output, which erases the existing file content.
The > operator replaces the file content, so previous data is lost.
Use >> to append output to the file instead of overwriting it.
Expecting output to appear on the screen after redirecting with > or >>.
Redirecting stdout sends output to the file, so nothing shows on the screen.
Check the file content with commands like cat to see the output.
Summary
Use > to send command output to a file and replace its content.
Use >> to add command output to the end of a file without deleting existing content.
Check the file content with cat or similar commands to verify redirection.