Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Linux & Mac & Bash Command Dictionary

  1. Home
  2. Linux & Mac & Bash Command Dictionary
  3. > (Output Redirect)

> (Output Redirect)

Since: All Linux
macOS(2001 Cheetah)
Bash 1.0(1989)

Redirect (>) is a feature that sends a command's standard output or standard error to a file or another stream. It is a fundamental tool used daily in script writing for tasks such as saving logs and suppressing errors.

Syntax

command > file       # Overwrite file with standard output
command >> file      # Append standard output to file
command 2> file      # Overwrite file with standard error
command 2>> file     # Append standard error to file
command &> file      # Overwrite file with both standard output and standard error
command > /dev/null 2>&1 # Discard both standard output and standard error

Redirect Notation Reference

NotationDescription
> fileOverwrites the file with standard output. Creates the file if it does not exist.
>> fileAppends standard output to the file. Creates the file if it does not exist.
2> fileRedirects standard error (fd=2) to the file.
2>&1Sends standard error (fd=2) to the same destination as standard output (fd=1).
&> fileWrites both standard output and standard error to the file (Bash 4 and later).
> /dev/nullDiscards standard output (/dev/null is a bit bucket).
2> /dev/nullDiscards standard error (use when you want to suppress error messages).
>/dev/null 2>&1Silences all output completely.
1>&2Redirects standard output to standard error (use when you want output treated as an error).

Sample Code

The following examples use this directory structure.

📁 ~/project/ 📄 README.md 📄 script.sh

Saves command output to a file, overwriting any existing content. The > operator clears the file and writes new content.

ls -la > filelist.txt
cat filelist.txt
total 16
drwxr-xr-x  4 user staff  128 Mar  5 10:00 .
drwxr-xr-x  8 user staff  256 Mar  5 09:00 ..
-rw-r--r--  1 user staff 1200 Mar  5 09:45 README.md
-rwxr-xr-x  1 user staff 2800 Mar  4 18:30 script.sh

Appends output to a file using >>. This is useful for recording logs.

echo "$(date): backup complete" >> backup.log
echo "$(date): cleanup complete" >> backup.log
cat backup.log
Fri Mar  6 12:00:00 JST 2026: backup complete
Fri Mar  6 12:05:00 JST 2026: cleanup complete

Saves only error messages to a file. The 2> operator redirects standard error (fd=2).

make 2> build_errors.log

Saves both standard output and standard error to a single file.

make > build.log 2>&1

Attempting to delete a non-existent file produces an error message, but using 2> /dev/null suppresses it so nothing is shown on screen. The exit status still reflects the error.

rm nonexistent.txt 2> /dev/null
echo $?
1

Silences all output completely. This is commonly used for scripts run on a schedule via cron.

/path/to/script.sh > /dev/null 2>&1

Using > alone truncates a file to zero bytes.

> logfile.txt

Outputs an error message to standard error from within a script. The 1>&2 operator redirects standard output to standard error.

echo "An error occurred." 1>&2

Common Mistakes

Common Mistake 1: Accidentally overwriting an important file with >

> truncates the target file immediately. Typing the wrong filename destroys its contents with no undo.

echo "test" > /etc/hosts
(/etc/hosts is overwritten immediately)

Use >> to append, or enable noclobber to block accidental overwrites.

set -o noclobber
echo "test" > /etc/hosts
bash: /etc/hosts: cannot overwrite existing file

Common Mistake 2: Wrong order of 2>&1 does not redirect stderr to the file

The order matters. Writing 2>&1 > file redirects stderr to the current stdout (terminal), then redirects stdout to the file. Stderr still goes to the terminal.

command 2>&1 > output.log
(stderr still goes to the terminal)

Redirect stdout to the file first, then redirect stderr to stdout.

command > output.log 2>&1
(both stdout and stderr go to output.log)

Notes

Because > overwrites files, accidentally pointing it at an important file will destroy its contents. To prevent accidental overwriting, use Bash's set -o noclobber option, which disallows > on existing files. To override this protection, use >| instead.

For input redirection and here documents, see Input Redirect / Here Document. For combining redirects with pipes, see Pipe (|).

If you find any errors or copyright issues, please .