The redirection symbol changes the behavior of ls because it alters the command's output destination: when you write ls > file, the shell opens file for writing and attaches it to standard output (file descriptor 1) before ls runs, so ls writes its directory listing into the file instead of to your terminal. This is not a feature of ls itself but of the shell, which sets up redirections before executing the command.
The Role of the Shell in Redirection
When you type a command line, the shell (such as Bash, Zsh, or Dash) parses it into words, redirections, and other operators. Redirections are processed before the command is executed. For example, in ls > output.txt, the shell sees the > operator, opens (or creates) output.txt for writing, and duplicates that file descriptor onto standard output. Then it executes ls with its standard output already pointing to the file. The ls program itself does not know or care where its output goes; it simply writes to file descriptor 1 as usual.
This separation of concerns is a core Unix design principle: programs write to standard streams, and the shell decides where those streams connect. The same mechanism applies to input redirection (<), error redirection (2>), and pipes (|).
Standard Streams and File Descriptors
Every process started by the shell inherits three standard streams:
- Standard input (fd 0): where the program reads input, normally the keyboard.
- Standard output (fd 1): where normal output goes, normally the terminal.
- Standard error (fd 2): where error messages go, also normally the terminal.
Redirection operators let you change these connections. For instance, ls > list.txt redirects fd 1 to a file, while ls 2> errors.txt redirects fd 2. You can redirect both with ls > list.txt 2>&1 (order matters). The shell performs these redirections left to right before running the command.
Common Redirection Operators and Their Effects on ls
Different operators change ls behavior in different ways:
>(or1>): redirects standard output to a file, truncating it first.ls > out.txtwrites the listing toout.txt, overwriting existing content.>>: appends standard output to a file.ls >> out.txtadds the listing to the end ofout.txt.2>: redirects standard error.ls missing 2> err.txtsends the error message toerr.txt.&>(Bash extension): redirects both stdout and stderr.ls missing &> all.txtcaptures both the error and any output.<: redirects standard input.ls < fileis unusual becauselsdoes not read stdin; it has no effect on the listing.|(pipe): connects stdout oflsto stdin of another command, e.g.,ls | grep txt.
Note that ls changes its output format when stdout is not a terminal: it prints one entry per line instead of columns. This is a deliberate design choice in many ls implementations (including GNU coreutils) to make output machine-readable. So redirecting ls to a file not only changes where output goes but also how it is formatted.
Why ls Output Format Changes with Redirection
When ls writes to a terminal, it typically formats output in columns to fit the screen width. When stdout is redirected to a file or pipe, ls detects that it is not a terminal (using the isatty() function) and switches to one name per line. This behavior is intentional: column formatting includes padding and may be ambiguous for parsing, while one-per-line is easier for scripts to process. You can override this with options like -C (force columns) or -1 (force one per line). For example, ls -C > out.txt writes column-formatted output to the file, though the column width may be based on a default rather than your terminal.
Redirection vs. Piping: Different Mechanisms
Redirection and piping both change where output goes, but they work differently. Redirection connects a stream to a file; piping connects stdout of one process to stdin of another. For ls, ls > file writes to a file, while ls | less sends output to less for paging. Both cause ls to see a non-terminal stdout and thus use one-per-line format (unless overridden). The shell sets up pipes with pipe() and dup2(), similar to redirection.
Common Problems and Misconceptions
A frequent mistake is expecting ls > file to display output on the terminal as well. It does not; the terminal sees nothing because stdout is redirected. To see output and save it, use tee: ls | tee file. Another misconception is that ls itself handles redirection; it does not. The shell does. Also, redirection can silently overwrite files: ls > important.txt will clobber important.txt without warning (unless noclobber is set). Use >> to append or set -o noclobber to prevent overwrites.
Another issue: redirecting stderr requires the correct file descriptor number. ls missing > out.txt sends only stdout to the file; the error still appears on the terminal. To capture errors, use ls missing > out.txt 2> err.txt or ls missing &> all.txt.
Practical Examples and Best Practices
Here are some concrete examples of how redirection affects ls:
ls > listing.txt— writes a one-per-line listing tolisting.txt, overwriting it.ls -la >> listing.txt— appends a detailed listing tolisting.txt.ls /nonexistent 2> error.log— captures the error message inerror.log; nothing on terminal.ls | wc -l— counts entries;lsoutputs one per line to the pipe.ls -C > columns.txt— forces column output even when redirected.
Best practices: use >> when you want to keep existing content; redirect stderr separately if you need to distinguish errors; and remember that redirection is handled by the shell, so it works with any command, not just ls.
Frequently Asked Questions
Why does ls > file produce one file per line instead of columns?
Because ls checks whether stdout is a terminal. When redirected to a file, it is not a terminal, so ls defaults to one entry per line for easier parsing. You can force columns with ls -C.
Does redirection change what ls lists?
No. Redirection only changes where output goes, not which files are listed. The set of entries is determined by ls arguments and options, not by redirection.
How can I see ls output on screen and save it to a file at the same time?
Use tee: ls | tee file displays output and writes it to file. Or redirect to a file and then cat it.
Why does ls > file sometimes show an error about permission denied?
The shell tries to open the file for writing before running ls. If you lack write permission in the directory or the file is read-only, the shell reports an error and ls does not run.
What is the difference between > and >>?
> truncates the file (overwrites), while >> appends to it. Both redirect stdout.
Can I redirect ls output to a file and still see errors on the terminal?
Yes. By default, only stdout is redirected; stderr remains on the terminal. So ls missing > out.txt writes any valid output to out.txt and shows the error on screen.