Open Directory Site  Advanced Unix Tutorials

Home | Windows 98 | Windows Me | Windows 2000 Windows XP | Unix | Advanced Unix

Home >> Operating Systems >> Advanced Unix >> Pipes and Redirection

Pipes and Redirection

UNIX allows you to redirect the input/output of commands to files or to other commands. The < redirects the contents of a file as input to a command. The pipe, |, redirects the output of one command as the input of another command. The > symbol redirects the output of a command to a file, and the >> redirects the output of the command to the end of a file (appends to the given file). Take the time now to try out some of the following commands:

 

mkdir Temp; cd Temp                                            Create a Temp directory for this session.

cp /usr/caen/doc/workshops/advanced_unix . Copy the sample files to your Temp directory.

cd advanced_unix                                                     Change directories to the example directory

date > today.txt                                                         Send the output of the date command to the                                                                                                 file today.txt.

ls > also.txt                                                                Send the output of ls to the file also.txt.

cat today.txt also.txt                                                  Display the files today.txt and also.txt.

cat today.txt also.txt today.txt > zzz                       Send the output of the cat command to the file zzz.

cat zzz                                                                                    Display the file zzz.

date > logfile                                                             Send the output of date to the file logfile.

date >> logfile                                                          Append the output of date to the file logfile.

date >> logfile                                                          Append another date entry to the end of the file logfile.

cat logfile                                                                   Display logfile.

echo “The last date”                                                            Display the string “The last date”.

echo “The last date” >> logfile                              Append the string “The last date” to logfile.

cat logfile                                                                   Display logfile.

mail uniqname < logfile                                          Use the mail command to mail yourself the logfile. Here the input for the mail command comes from a file instead of the terminal screen.

cat tao2.txt                                                                 Display the file tao2.txt.

cat tao2.txt | wc                                                        Send the output of cat tao2.txt to the input of the command wc.

cat tao2.txt | grep master                                        Send the output of cat tao2.txt to the input grep master. This will, in turn,output all of the lines that contain the word master to the screen.

cat tao2.txt | tail                                                       Pipe the output through the command tail.                                                                                         This will display the last 10 lines.

cat tao2.txt | head                                                    Pipe the output through the command head.                                                                                                 This will display the first 10 lines.

cat tao2.txt | more                                                    Pipe through the page handling command more. This will pause between pages on the screen.

cat campcaen.txt | grep “\- “ | sort                      Pipe the output of cat campcaen.txt through grep to search for the lines with a hyphen. Then pipe the lines that contain hyphens through sort.

cat logfile | mail uniqname                                     Mail the output of cat logfile to the user with the loginID uniqname.

uuencode feet.gif sendfeet.gif | mail uniqname  Take the file feet.gif and then mail the user with the loginID uniqname the encoded graphic file sendfeet.gif. The user could decode the graphic by running uudecode.

Cheap Web Hosting Articles - Web Site Design & Web Hosting Tutorials - Domain Hosting