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

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.

SEE ALSO

tee(1), >>(1), 2>(1), &>(1), cat(1), echo(1)

Copied to clipboard