Activity 4: Unix Streams, Pipes and Redirects: Objectives
Activity 4: Unix Streams, Pipes and Redirects: Objectives
Objectives:
• Define what generally streams are.
• Have an in-depth knowledge of how pipes works in Linux.
• Gain hands-on experience on how to connect programs to files
• Learn how redirection works
Concepts
Standard Files
• Processes are connected to three standard files
• Many programs open other files as well
Standard Input
PROGRAM
Standard Output
• Programs can write data to their standard output file
• Abbreviated to stdout
• Used for a program's normal output
• By default this is printed on the terminal
Standard Error
• Programs can write data to their standard error output
• Standard error is similar to standard output but used for error and warning messages
• Abbreviated to stderr
• Useful to separate program output from any program errors
• By default this is written to your terminal
◦ It get mixed in with the standard output
Pipes
•A pipe channels the output of one program to the input of another
•Allows program to be chained together
•Programs in the chain run concurrently
•Use the vertical bar |
•Sometimes known as the pipe character.
•Programs don't need to do anything special to use pipes
•They read from stdin and write to stdout as normal
Example: Pipe the output of echo into the program rev (which reverses each line if its input)
$ echo “Hello Sysad313 Students” | rev
stnedutS 313dasyS olleH
Example: list the contents of the /bin directory (which contains user commands) in a convenient
paged format:
$ ls -al /bin | less
Example: redirect the output of 'ls' to a text file instead of the console.
$ ls -al /bin > binContents.txt
Example: Suppose you want to find the exact number of lines, number of words and characters
respectively in a text file and at the same time you want to write it to another file
$ wc < my_text_file.txt > output_file.txt
Appending To Files
• Use >> to append to a file
Examples: Suppose you want to add a single line to an existing file.
$ echo “this line was added” >> existing_file
Example: Use tee command for backing up files and wanted to put a date on the
file
$ date | tee -a directory_listing.txt
1. Try the example on the Pipe section, using rev to reverse some text.
1. Try replacing the echo command with some other commands which
produce output (e.g. whoami)
2. What happens when you replace rev with cat? You might like running cat
with no arguments and entering some text
2. Run the command ls - -color in a directory with a few files and directories.
Some Linux distributions have ls set up to always use the - -color option in
normal circumstances, but in this case we will give it explicitly.
3. Try running the same command but pipe the output into another program
(e.g. cat or less). You should spot two differences in the output.