Output Redirection and Piping

Output Redirection

  • Most processes initiated by UNIX commands take their input from the standard input (i.e. the keyboard) and write their output to the standard output (i.e. the terminal screen).

  • The "<" sign can be used to redirect the input, that is to specify that the input comes from something other than the keyboard.

  • The ">" sign can be used to redirect the output, that is to specify that the output goes to something other than the terminal screen.

  • The ">>" sign can be used to append the output to something other than the terminal screen.

# list the current files and redirect the output to a file named "mylist.txt"
ls > mylist.txt
# view content of mylist.txt
cat mylist.txt
# redirect the input to a command 
cat < mylist.txt
# redirect the output and append 
cat mylist.txt > list1.txt
cat mylist.txt >> list2.txt
# view content 
cat list2.txt

Piping

  • A pipe is denoted by "|".

  • Several pipes can be used in the same command line to create a "pipeline".

  • A pipe takes the output of a command and immediately sends it as input to another command.

  • A pipe is often used in conjunction with the command "less" to view the output within the pager.

#view users connected
who
#count the number of users connected
who | wc -l
#display the content of bin
ls -la /usr/local/bin
#display the content of bin within the pager provided by "less"
ls -la /usr/local/bin | less

Last updated

Massachusetts Institute of Technology