Output redirection in Linux: > and >>
Updated November 1, 2026
Output redirection sends a command's output to a file instead of the screen.
Syntax
command > file # overwrite the file with the output
command >> file # append to the endCommon cases
echo hello > out.txt # create or overwrite
echo world >> out.txt # append a line to the end
ls /nope 2> err.txt # the error stream (2) to a file
make > log.txt 2>&1 # both output and errors to one fileSample output
$ echo hello > out.txt
$ echo world >> out.txt
$ cat out.txt
hello
world
$ ls /nope > out.txt
ls: cannot access '/nope': No such file or directory
$ wc -c < out.txt
0How it works: the three standard streams
Every process has three standard streams: input (0), output (1) and errors (2).
>redirects stream 1,2>redirects stream 2, and2>&1means 'stream 2 to wherever stream 1 goes'. So> file 2>&1gathers both the normal output and the errors into one file. A single>overwrites the whole file, while>>appends to the end.
Common mistakes
sort list.txt > list.txt leaves an empty file. The shell opens list.txt for writing and truncates it to zero before sort even starts, so the command has nothing left to read: wc -c < list.txt prints 0 afterwards. Write to a second name and rename it, or use the flag sort has for this: sort list.txt -o list.txt reads the whole file first and writes after.
The order inside command > file 2>&1 is not free. ls /nope 2>&1 > out.txt leaves the error ls: cannot access '/nope': No such file or directory on the screen and out.txt empty. 2>&1 points stream 2 at whatever stream 1 points to at that moment, and at that moment it still points at the screen; the > out.txt that follows moves stream 1 alone. The working form is ls /nope > out.txt 2>&1.
> creates the file even when the command prints nothing. grep zzz /etc/hostname > found.txt matched no line, yet found.txt is there, 0 bytes long. So "the file showed up, therefore something matched" is a false test: check the exit status of grep or the size of the file.
Used in
You need this in the lesson Streams and pipelines. The theory is the chapter Streams, redirection, pipelines.
See also
Pipes and filters, cat, head, tail.
Try it
Open the sandbox: a real throwaway Linux terminal in your browser. Run the commands above by hand - the container is deleted when you leave.