Shell Redirection: >, >>, <, |, 2> and tee Explained
Methods for redirecting data through the shell, either from/to files or commands.
Standard Input - stdin
Acts as the source for command input. This is usually coming from your mouse or keyboard.
Standard Output - stdout
Acts as the destination for command output. This is usually going to your screen/ the shell terminal.
Standard Error - stderr
Used as the destination for error messages. Default is the screen/ shell terminal.
overwrite >
The use of a single > will overwrite the entire contents of a file.
root@jump:~# echo 'hello' > test1.txt
root@jump:~# cat test1.txt
hello
Append >>
The use of 2 >> will append to the end of a file.
root@jump:~# cat test1.txt
hello
hello1
Input Redirection <
The use of a single < can be used to direct a file's contents as standard input.
root@jump:~# tr h H < test1.txt
Hello
Hello1
Heredoc <<
The << allows you to feed multiple lines of input to a command from within your shell session, rather than reading from a file.
command <<EOF
line1
line2
line3
EOF
The shell takes everything between the first EOF and the second EOF as input to command. Note that you can use literally any text you want after the << - this is just to specify the delimiter.
Example
cat <<END
Hello, world!
This is a heredoc example.
END
Hello, world!
This is a heredoc example.
This sends the two lines as input to cat, which prints them out.
Command Redirection |
The pipe | symbol can be used to direct the output of one command into another.
root@jump:~# cat test1.txt | grep -i hello
hello
hello1
Pipeline Redirection tee
The tee command reads from standard input and writes to standard output and one or more files.
It acts like a T-junction (hence the name) — it splits a stream of data into two paths:
One goes to a file (or multiple files)
One continues to the terminal or the next command in the pipeline
ls -l | tee list.txt | grep "log"
Saves the full ls -l output to list.txt
Passes it to grep to filter for "log"
So you get the best of both worlds
stderr Redirection 2>
The 2> operator can be used to redirect stderr to a file or location. Similarly to the usage for normal redirection, a single > symbol will overwrite the target file, and a double >> symbol will append to the target file.
Example
root@jump:~# cat test11.txt
cat: test11.txt: No such file or directory
root@jump:~# cat test11.txt 2> error.txt
root@jump:~# cat error.txt
cat: test11.txt: No such file or directory
In this example, the file test11.txt does not exist. I've run the cat command on this file (which has generated an error), using the 2> I've redirected this error to the error.txt file.
stderr & stdout Redirection &>
The &> operator will redirect both the stdout and stderr of a command to a target file. Similarly to the usage for normal redirection, a single > symbol will overwrite the target file, and a double >> symbol will append to the target file.
Example
root@jump:~# cat test1.txt test2.txt
hello world
cat: test2.txt: No such file or directory
root@jump:~# cat test1.txt test2.txt &> error.txt
root@jump:~# cat error.txt
hello world
cat: test2.txt: No such file or directory
in this example, test1.txt contains 'hello world', and test2.txt doesn't exist.
catting both these files and using &> redirects both the hello world test from test1.txt, and the error received when attempmting to cat the none-existent test2.txt.
xargs
Reads streams of data from stdin, then generates and executes command lines.
command [options] [arguments] | xargs [options] {command}
| Option | Function |
| -l {replacement string} | Consider each line as a single argument |
| -L {number of lines} | Read the specified number of lines and cat in one long string. |
| -p | Prompt the user before each command. |
| -n {number of arguments} | Read the maximum number of arguments and insert at the end of the command template |
| -E {end of string} | Represent the end of the stdin |
| -t | Write the command to stderr output before executing the command. |
| -s {max size} | Set maximum allowable size of an argument list. |
Example
We have a directory with a number of files within;
root@jump:~/test# ls -l
total 4
-rw-r--r-- 1 root root 0 Jun 16 19:11 file1
-rw-r--r-- 1 root root 0 Jun 16 19:11 file2
-rw-r--r-- 1 root root 0 Jun 16 19:11 file3
-rw-r--r-- 1 root root 18 Jun 16 19:11 files.txt
We have a file which lists out the file names within that directory;
root@jump:~/test# cat files.txt
file1
file2
file3
root@jump:~/test# cat files.txt | xargs rm
We cat the files.txt file and pass the output to xargs which runs the rm command and deletes the files;
root@jump:~/test# ls -l
total 4
-rw-r--r-- 1 root root 18 Jun 16 19:11 files.txt
root@jump:~/test#
No Comments