0% found this document useful (0 votes)
202 views4 pages

Activity 4: Unix Streams, Pipes and Redirects: Objectives

This document provides an overview of streams, pipes, and redirects in Linux. It defines standard input, output, and error files and how programs can connect to these files. It also explains how pipes allow the output of one program to serve as input to another program, and how redirection can connect programs to files on disk rather than standard streams. Redirection and pipes allow chaining programs together and managing program input/output.

Uploaded by

ErwinMacaraig
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
202 views4 pages

Activity 4: Unix Streams, Pipes and Redirects: Objectives

This document provides an overview of streams, pipes, and redirects in Linux. It defines standard input, output, and error files and how programs can connect to these files. It also explains how pipes allow the output of one program to serve as input to another program, and how redirection can connect programs to files on disk rather than standard streams. Redirection and pipes allow chaining programs together and managing program input/output.

Uploaded by

ErwinMacaraig
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Activity 4: Unix Streams, Pipes and Redirects

Name: ______________ CAC: ______ Date:_______ Section: _____

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 Standard Error


Standard Input
• Programs can read data from their standard input file
• Abbreviated to stdin
• By default this reads from the keyboard
• Characters typed into an interactive program (e.g. text editor) go to stdin

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

Connecting Programs to Files


• Redirection connects a program to a named file .
• The < symbol indicates the file to read input from
◦ The file specified becomes the program's standard input
• The > symbol indicates the file to write output to.
◦ The program's standard output goes into the file.
◦ If the file already exists, it is overwritten

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

Examples: Suppose you want to join two files


$ cat file1 >> file2

Redirecting Multiple Files


• Open files have numbers, called file descriptors
• These can be used with redirection
• The three standard files always have the same numbers:
Name Descriptor
Standard input 0
Standard output 1
Standard error 2

Redirection with File Descriptors


•Redirection normally works with stdin and stdout
•Specify different files by putting the file descriptor number before the redirection symbol:
•The descriptors 3-9 can be connected to normal files and are mainly used in shell scripts
Example: Suppose you have a file myfile.txt
$ ls -l myfile.txt > test.txt
$ rm myfile.txt
$ ls -l myfile.txt > test.txt
ls: myfile.txt: No such file or directory - -ERROR

$ls -l myfile.txt 2> test.txt

Running Programs with xargs


• xargs reads pieces of text and runs another program with them as its arguments
• Usually its input is a list of filenames to give to a file processing program
• Syntax:
xargs command [initial arguments]
• Use -l n to use n items each time the command is run
• The default is 1
• xargs is very often used with input piped from find
Example: There are so many files in a particular directory. Use xargs to delete ten files at a time
$ find /tmp/rubbish/ | xargs -l10 rm -f
tee
• The tee program makes a 'T-junction' in a pipeline
• It copies data from stdin to stdout and also to a file
• Like > and | combined
Example: Save details of everyone's logins and save Bob's logins in a separate
file.
$ last | tee everyone.txt | grep bob > bob.txt

Example: Use tee command for backing up files and wanted to put a date on the
file
$ date | tee -a directory_listing.txt

Activity Exercises: Specify the command(s) that will satisfy the


requirement. Please specify the output as well.

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.

You might also like