One of the key principles of the UNIX philosophy is that all command-line interface (CLI) commands should accept text as input and produce text as output. Since this concept was applied in the development of UNIX (and later Linux), there are commands to receive text as input, perform an operation on it, and then produce text as output. Commands that read text input, modify it in some way, and then produce text output are sometimes called filters.
To use filter commands and work with text streams effectively, we should understand several types of redirection available with most commands: pipelines, standard output redirection, error output redirection, and input redirection.
When a command executes without errors, the resulting output is called standard output, also known as STDOUT. By default, this output is sent to the terminal where the command is run.
We can redirect standard output from the command so it goes to a file instead of the terminal using the greater-than symbol (>
) followed by the target file. For example, the command ls ~
lists files in the home directory. To save this list to a text file:
ls ~ > /tmp/home.txt
The contents of home.txt
would then look like this:
cat /tmp/home.txt
Desktop
Documents
Downloads
Music
Pictures
Public
Templates
Videos
Using a single >
creates a new file or overwrites an existing one. Using two greater-than symbols >>
also creates a file if it doesn't exist but appends output to the end if the file already exists. For example, to append the output of the date command:
date >> /tmp/home.txt
cat /tmp/home.txt
Desktop
Documents
Downloads
Music
Pictures
Public
Templates
Videos
Sun Mar 30 07:36:02 UTC 2025
In Linux, when a command encounters an error, it produces output known as standard error, also called stderr or STDERR. Like standard output, it is usually sent to the same terminal. The file descriptor number for standard error is 2.
For example, trying to run:
ls /junk
Will produce:
ls: cannot access /junk: No such file or directory
Since this output goes to stderr, using just >
won't redirect it:
ls /junk > output
ls: cannot access /junk: No such file or directory
To redirect error messages, use the correct file descriptor:
ls /junk 2> /tmp/ls.err
Like with standard output, >
will create or overwrite the file. To append to the error log instead:
ls /junk 2>> /tmp/ls.err
Some commands produce both stdout and stderr. For example:
find /etc -name passwd
/etc/pam.d/passwd
/etc/passwd
find: '/etc/ssl/private': Permission denied
You can redirect these into two separate files:
find /etc -name passwd > /tmp/output.txt 2> /tmp/error.txt
To verify:
cat /tmp/output.txt
/etc/pam.d/passwd/etc/passwd
cat /tmp/error.txt
find: '/etc/ssl/private': Permission denied
If you don't want to display or save error messages, redirect them to /dev/null
. This file acts like a trash bin where all input disappears. Any output type can be redirected there, most commonly stderr:
find /etc -name passw 2> /dev/null
/etc/pam.d/passwd
/etc/passwd
To redirect both stdout and stderr into a single file, use either of the following methods:
ls > /tmp/ls.all 2>&1
ls &> /tmp/ls.all
Both commands create the file /tmp/ls.all
containing both standard output and error messages. In the first command, 2>&1
means "send stderr to the same place as stdout." The second uses &>
as shorthand for "redirect all output."
Standard input, also known as stdin or STDIN, typically comes from the keyboard and is entered by the user running the command. While most commands can read input from files, some expect the user to type input manually using the keyboard.
One common way to use text files as standard input is by creating script files. Scripts are simple text files that are interpreted by the shell when given the appropriate permissions and start with #!/bin/sh
in the first line, which tells the shell to interpret the script as standard input:
cat examplescriptfile.sh
#!/bin/sh
echo HelloWorld
When a script file is called in the command line using the ./
syntax, the shell executes all commands in the script file and returns the result to the terminal window or wherever the output is directed:
./examplescriptfile.sh
HelloWorld
In some cases, it’s useful to redirect standard input so that it comes from a file instead of the keyboard. A good example where input redirection is desirable is the tr
command. The tr
command translates characters by reading from standard input, converting one set of characters to another, and writing the transformed text to standard output.
For example, the following tr command takes input from the user (via keyboard) to convert all lowercase letters to uppercase:
tr 'a-z' 'A-Z'
hello
HELLO
The tr
command will not stop reading from standard input unless it is terminated. You can do this by pressing Ctrl+D.
The tr
command does not accept a filename as a command-line argument. To perform translation using a file as input, use input redirection. To do this, enter the command with its parameters and arguments, followed by the less-than symbol <
and the file path to be used for input. For example:
cat Documents/animals.txt
1 retriever
2 badger
3 bat
4 wolf
5 eagle
tr 'a-z' 'A-Z' < Documents/animals.txt
1 RETRIEVER
2 BADGER
3 BAT
4 WOLF
5 EAGLE
Command pipelines are often used to efficiently apply filter commands. In a command pipeline, the output of one command is sent to another command as input. In Linux and most operating systems, the vertical bar |
represents a pipeline between two commands.
For example, if the output of the history command is very long, you can send it to the less command to display one page at a time:
history | less
An even better example is taking the output of history and filtering it using the grep
command. In the following example, the text output from history is redirected to grep
as input. The grep
command matches lines containing "ls" and sends the result to standard output:
history | grep "ls"
1 ls ~ > /tmp/home.txt
5 ls l> /tmp/ls.txt
6 ls 1> /tmp/ls.txt
7 date 1>> /tmp/ls.txt
8 ls /junk
9 ls /junk > output
10 ls /junk 2> /tmp/ls.err
11 ls /junk 2>> /tmp/ls.err
14 ls > /tmp/ls.all 2>&1
15 ls &> /tmp/ls.all
16 ls /etc/au* >> /tmp/ls.all 2>&1
17 ls /etc/au* &>> /tmp.ls.all
20 history | grep "ls"
Command pipelines become especially powerful when combining three or more commands. For example, view the contents of the os.csv
file in the Documents
directory:
cat Documents/os.csv
1970,Unix,Richie
1987,Minix,Tanenbaum
1970,Unix,Thompson
1991,Linux,Torvalds
The following command line extracts some fields from os.csv
using the cut
command, then sorts those lines using sort
, and finally removes duplicates using uniq
:
cut -f1 -d',' Documents/os.csv | sort -n | uniq
1970
1987
1991
The tee
command splits the output of a command into two streams: one is directed to standard output (displayed in the terminal), and the other is written to a file.
The tee
command is useful for logging the output of a command or script. For example, to record the execution time of a process, start with the date
command and copy the output to a file timer.txt
:
date | tee timer.txt
Tue Apr 2 02:21:24 UTC 2025
The timer.txt
file now contains a copy of the date, the same output as shown above:
cat timer.txt
Tue Apr 2 02:21:24 UTC 2025
To append the time to the end of timer.txt
, use the -a
option:
date | tee -a timer.txt
Tue Apr 2 02:28:43 UTC 2025
To run multiple commands as one line, use the semicolon ;
as a separator:
date | tee timer.txt; sleep 15; date | tee -a timer.txt
Tue Apr 2 02:35:47 UTC 2025
Tue Apr 2 02:36:02 UTC 2025
The command above displays and writes the first date output, pauses for 15 seconds, then displays and writes the second date output. The timer.txt
file now contains a persistent execution log.
Command options and parameters are typically specified directly in the command line as arguments. Alternatively, you can use the Linux xargs
command to collect arguments from another input source (such as a file or standard input) and pass them to a command. xargs
can be called directly and will accept any input:
xargs
Hello
There
To exit xargs
, press Ctrl+C.
By default, xargs
passes input to the echo command if no other command is explicitly given. After pressing Ctrl+D, xargs
sends the input to echo:
Pressing Ctrl+D after exiting
xargs
with Ctrl+C will also exit the current shell. To send input toecho
without exiting the shell, press Ctrl+D duringxargs
execution.
The Linux xargs
command is most useful when used in a pipeline. The following example uses the touch
command to create four files. The file names are 1a
, 1b
, 1c
, and 1d
, based on the output from the echo
command:
echo '1a 1b 1c 1d' | xargs touch
ls
1a 1c Desktop Downloads Pictures Templates timer.txt
1b 1d Documents Music Public Videos
We’ve reviewed input/output stream redirection in Linux: standard output redirection, error output redirection, input redirection, and pipelines. Understanding these capabilities makes working with bash scripts easier and helps efficiently administer servers running Linux-based operating systems.