# Short answer:
command > filename # Redirect stdout to file "filename", overwrite if present
command 1> filename # Redirect stdout to file "filename" (same as >)
command >> filename # Redirect and append stdout to file "filename"
command 2> filename # Redirect stderr to file "filename"
command 2>> filename # Redirect and append stderr to file "filename"
command &> filename # Redirect both stdout and stderr to file "filename"
command &>> filename # Redirect and append both stdout and stderr to file "filename"
command < filename # Send contents of file "filename" to command (redirect stdin)
command | command_2 # Redirect (pipe) stdout of command to command_2
# Long answer:
# First, it's helpful to know about file descriptors (fds). A fd is a number
# that the operating system assigns to an open file to keep track of it.
# Consider it a simplified type of file pointer. It is analogous to a file
# handle in C.
# In bash, the three default file descriptors are:
# 0, which refers to the standard input (stdin),
# 1, which refers to the standard output (stdout), and
# 2, which refers to the standard error (stderr)
# Redirection allows commands’ fds to be duplicated, opened, closed, made to
# refer to different files, and can change the files the command reads from
# and writes to. Redirection may also be used to modify file handles in the
# current shell execution environment
# Note, redirections are processed in the order they appear, from left to right
# Note, if the fd isn't specified during a redirection, one is usually assumed
# e.g. if you use >, it assumes an fd of 1, identical to: 1>
# Given this background, there are two main redirection formats to be aware of:
i>j
# Here, "i" is a file descriptor and "j" is a filename
# File descriptor "i" is redirected to file "j"
i>&j
# Here, "i" is still a file descriptor but the & makes "j" another file
# descriptor. This syntax causes fd i to be redirected to fd j. In other
# words, all output of the file or command pointed to by i gets sent to
# the file pointed to by j
# Example usage:
ls -ld /tmp /tnt
-> ls: cannot access /tnt: No such file or directory # stderr
-> drwxrwxrwt 118 root root 196608 Jan 7 11:49 /tmp # stdout
ls -ld /tmp /tnt > /dev/null # redirect stdout to trash
-> ls: cannot access /tnt: No such file or directory # stderr only
ls -ld /tmp /tnt 2> /dev/null # redirect stderr to trash
-> drwxrwxrwt 118 root root 196608 Jan 7 11:49 /tmp # stdout
# The following differences in behavior is slightly confusing to me. The only
# way I can make sense of it is if in the top example, stdout is first
# redirected to trash and then stderr is redirected to stdout which is still
# pointing to the trash, causing it to be discarded as well. In the bottom
# example, it would seem like stderr is first sent to stdout (which gets
# printed to the console) and then the stdout gets redirected to the trash,
# and therefore isn't printed
ls -ld /tmp /tnt > /dev/null 2>&1
ls -ld /tmp /tnt 2>&1 > /dev/null
-> ls: cannot access /tnt: No such file or directory # stderr
# See these links for more on redirection:
https:
https: