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
Open a persistent file descriptor
Write to a custom file descriptor
SYNOPSIS
The > operator is not a standalone command but an integral part of shell syntax used in conjunction with commands.
command > file_path
Examples:
ls -l > file_list.txt
echo 'Hello, World!' > message.txt
cat /dev/null > empty_file.txt
Optionally, you can explicitly specify the file descriptor to redirect (1 for stdout):
command 1> file_path
PARAMETERS
target_file
The > operator itself does not accept traditional command-line options or flags. Its 'parameter' is the target file or path where the command's standard output will be redirected.
The structure is:
command > target_file
The target_file is a required argument specifying the name and optional path of the file to which the output will be written.
DESCRIPTION
The greater-than symbol (>) is a fundamental shell operator in Linux and Unix-like systems, used for output redirection. It takes the standard output (stdout) of a command and writes it entirely to a specified file. If the target file does not exist, the shell creates it. Crucially, if the file already exists, its contents are overwritten without any warning or prompt, potentially leading to data loss if not used carefully. This operator is essential for capturing command results, logging information, or feeding output from one process into a file for later use. It is distinct from the >> operator, which appends the output to the end of the file rather than overwriting it.
CAVEATS
Data Overwriting: The most significant caveat is that > will mercilessly overwrite the contents of an existing file without any warning or prompt. This can lead to irreversible data loss. To prevent this, consider using >> for appending, or enable the noclobber shell option (e.g., set -o noclobber in Bash) which prevents accidental overwriting of existing files. If noclobber is set, you can still force overwrite using >|.
Permissions: The shell user must have write permissions to the directory where the file is being created, or write permissions to the file itself if it already exists.
Shell Feature: > is a shell built-in redirection operator, not an external executable command. Its behavior is handled directly by the shell (e.g., Bash, Zsh), meaning its functionality can sometimes vary slightly between different shells, though the core behavior remains consistent.
FILE DESCRIPTORS AND DEFAULT BEHAVIOR
In Unix-like systems, processes interact with files and devices using file descriptors. Standard output (stdout) is associated with file descriptor 1. When you use >, it is implicitly redirecting file descriptor 1. You can explicitly specify the file descriptor, for example, 1> file.txt is identical to > file.txt. This distinction becomes important when redirecting other streams like standard error (stderr), which uses file descriptor 2 (e.g., 2> error.log).
PREVENTING ACCIDENTAL OVERWRITE
To prevent accidental overwriting of existing files when using >, you can set the noclobber option in your shell. For Bash, this is done with
set -o noclobber
. Once set, any attempt to use > on an existing file will result in an error. To override noclobber for a specific redirection, you can use the >| operator (e.g., command >| existing_file.txt).
COMMON USE CASES
1. Saving Command Output: Easily store the results of a command for later analysis or viewing (e.g., ls -R > directory_contents.txt).
2. Creating Empty Files: Quickly create an empty file (e.g., > new_file.txt or touch new_file.txt).
3. Clearing File Contents: Empty an existing file without deleting it (e.g., > log_file.txt).
4. Basic Logging: Redirecting command output to a log file (e.g., my_script.sh > script_log.txt 2>&1).
HISTORY
The concept of I/O redirection, including output redirection using the > operator, is a cornerstone of the Unix operating system. It was developed at Bell Labs in the early 1970s as part of the original Unix design by Ken Thompson and Dennis Ritchie. This mechanism is central to the 'Unix philosophy' of creating small, single-purpose tools that can be combined in powerful ways. The standardization of file descriptors (0 for stdin, 1 for stdout, 2 for stderr) and their redirection capabilities became a fundamental feature of all subsequent Unix shells (like Bourne Shell, C Shell, Bash, Zsh) and Linux distributions, enabling sophisticated command chaining and scripting.
SEE ALSO
>> (Append standard output to a file) (Redirect standard input from a file)2> (Redirect standard error to a file)&> or >& (Redirect both standard output and standard error)| (Pipe: Redirect standard output of one command as standard input to another)tee(1) (Read from standard input and write to both standard output and files)cat(1) (Concatenate files and print on the standard output, often used to create or view files in conjunction with >)echo(1) (Display a line of text, frequently used to write short strings to files with >)