LinuxCommandLibrary

greater-than

Redirect standard output to a file

TLDR

Redirect stdout to a file

$ [command] > [path/to/file]
copy

Append to a file
$ [command] >> [path/to/file]
copy

Redirect both stdout and stderr to a file
$ [command] &> [path/to/file]
copy

Redirect stderr to /dev/null to keep the terminal output clean
$ [command] 2> /dev/null
copy

Clear the file contents or create a new empty file
$ > [path/to/file]
copy

Redirect stderr to stdout for piping them together
$ [command1] 2>&1 | [command2]
copy

Open a persistent file descriptor
$ exec [3]>[path/to/file]
copy

Write to a custom file descriptor
$ [echo text] >&[3]
copy

SYNOPSIS

command > file
[n]> file
[n]>| file (force overwrite)

PARAMETERS

>
    Redirect stdout to file, overwriting if exists

>>
    Append stdout to file, creating if absent

>|&
    Force overwrite stdout to file (ignores noclobber)

[n]>
    Redirect file descriptor n (default 1) to file

&>
    Redirect stdout and stderr to file

<>
    Open file for reading and writing (truncate)

DESCRIPTION

The greater-than symbol (>) is a fundamental shell redirection operator in Unix-like systems, including Linux. It redirects the standard output (stdout) of a command to a file, overwriting the file's contents if it exists. For example, ls > files.txt lists directory contents and writes them to files.txt, replacing any prior content.

This operator is part of the POSIX shell standard and is interpreted by shells like Bash, Zsh, and Dash before command execution. It associates with file descriptor 1 (stdout) by default but can target others with prefixes like 2> for stderr.

Key behaviors include automatic file creation if absent and truncation to zero length if present. In interactive shells with noclobber (set via set -o noclobber), it refuses to overwrite existing files unless forced with >|. It supports process substitution and here-documents indirectly but focuses on simple output capture.

Common pitfalls involve accidental data loss from overwriting and permission issues if the target directory lacks write access. It's essential for scripting, logging, and data processing pipelines.

CAVEATS

Overwrites files without warning unless noclobber set; fails silently on permission errors; does not follow symlinks for output (creates literal symlink target).

EXAMPLES

echo 'Hello' > test.txt (create/overwrite file)
grep error log.txt >> errors.log (append matches)
cmd 2> errors.txt (stderr only)
cmd &> all.log (both streams)

SHELL INTERACTION

Parsed before command expansion; use exec 1>file to redirect entire shell session.

HISTORY

Originated in the Bourne shell (sh) by Stephen Bourne in 1977 as part of Unix V7. Evolved in POSIX.1-1988 with standardized redirection syntax; Bash added >| and &> extensions.

SEE ALSO

tee(1), cat(1), | (pipe), bash(1)

Copied to clipboard