greater-than
Redirect standard output to a file
TLDR
Redirect stdout to a file
Append to a file
Redirect both stdout and stderr to a file
Redirect stderr to /dev/null to keep the terminal output clean
Clear the file contents or create a new empty file
Redirect stderr to stdout for piping them together
SYNOPSIS
command > file
DESCRIPTION
The > operator in Linux is a fundamental redirection command. It redirects the standard output (stdout) of a command to a file. If the file exists, it will be overwritten. If the file doesn't exist, it will be created. This is a powerful tool for saving command output, logging information, and creating configuration files.
The command takes the output intended for the terminal and writes it to a specified file instead. This is a core component of scripting, automation, and data processing in Linux. It avoids the need to manually copy and paste the output.
CAVEATS
Using > will overwrite the contents of the file if it already exists. To append to a file, use >> instead. The > redirects only standard output (stdout). Standard error (stderr) will still be displayed on the terminal unless redirected separately (e.g., 2>) or together (e.g., &>).
EXAMPLE USAGE
- ls -l > file.txt: Saves the output of the ls -l command to file.txt, overwriting any existing content.
- date > log.txt: Records the current date and time in log.txt, overwriting the existing file.
- echo "Hello World" > greeting.txt: Creates a file named greeting.txt with the content "Hello World".
COMBINING WITH OTHER REDIRECTIONS
The > operator can be used in conjunction with other redirection operators. For instance:
- command > output.txt 2> errors.txt: redirects standard output to output.txt, and standard error to errors.txt.
HISTORY
The concept of output redirection is deeply rooted in the early history of Unix and Linux. It was designed as a means of creating modular tools and enabling easy data processing by chaining commands together. The > operator has been a crucial part of this philosophy since the early days of Unix development.