Pipe and Redirection

Published: 1/12/2009
 

The Beginner

The command line is not only a tool for bashing out program names but an elegant and intelligent environment. Some programming languages can be typed directly into the command line. Also tasks that take some time to execute in the background leaving the user free to enter other commands. There is no limit (apart from resources) on the number of background tasks you can run.

The shell can even run commands and process the output to another command. These tasks can be stringed together until the data you need is displayed or organized.

One simple task is to open the documents you worked on yesterday.

 

[gizard@localhost 2 BasicCLI]$ oowriter `find "./" -type f -name "*.sxw" -ctime -1 -print`

 

This command will open all files within the current working directory that was created yesterday. The files location will be passed on to the oowriter (MS Word clone) and opened.

To do the same under windows will take no less that eight mouse clicks, 2 dialog boxed and then you will have to open the files independently.

Redirection and the Pipe

Redirection allows the output from one command to be passed to a file. The operator > is a redirect operator. If you wish to redirect a commands output to a file then the > operator will perform this task. It would also create the file if one does not exist.

 

[gizard@localhost gizard]$ ls -lat > filesbytime

 

This will create a new file called filebytime. If the file exists then the file will be over written.

The < operator will redirect the output from a file to a commands input.

 

[gizard@localhost gizard]$ wc -l < filebyname

 

This will produce a word count for the file filebyname.

 

The >> operator will append the output to a file and not overwrite any data. This is useful for log reports and diaries.

 

Task

Try out the examples on the previous page. See if you can come up with any more reasons to use the redirect operator.

 

Redirect is good for working on files but sometime the user will wish to get the output from a command and process it with another command. The pipe tool does this task.

 

[root@localhost root]# cat /var/log/messages | grep "Sep 21" | grep gizard

 

This command may look complicated but it is in fact quite simple. First the cat command is run. This command will echo a given files content onto the screen. The user is only interested in the lines that contain the text 'Sep 21'. Also the user is only interested in messages created by the user gizard. Here is the output from this command.

 

[root@localhost root]# cat /var/log/messages | grep "Sep 21" | grep gizard

Sep 21 09:02:20 localhost kde3(pam_unix)[7582]: session opened for user gizard by (uid=0)

Sep 21 09:06:49 localhost gconfd (gizard-20616): starting (version 2.2.0), pid 20616 user 'gizard'

Sep 21 09:45:42 localhost su(pam_unix)[21074]: session opened for user root by gizard(uid=502)

[root@localhost root]#

 

Here we have valuable information about what the user 'gizard' is doing.

Task

Experiment using the above commands on files.

 

Questions

what output do you get from the command below? What you you this this is?

[gizard@localhost gizard]$ cat /etc/passwd | grep gizard

                                                                                                                                        

                                                                                                                                         

                                                                                                                                        

                                                                                                                                         

                                                                                                                                        

                                                                                                                                         

 

What output do you get from the command below? What do you think this is?

[gizard@localhost gizard]$ cat /etc/group | grep gizard

                                                                                                                                         

                                                                                                                                         

                                                                                                                                        

                                                                                                                                         

                                                                                                                                        

 

What output do you get from the command below? What do you think this is?

[gizard@localhost gizard]$ cat /etc/fstab | grep cdrom

                                                                                                                                        

                                                                                                                                         

                                                                                                                                        

                                                                                                                                         

                                                                                                                                        

 

Related Posts

Linux File Structure Part 2

Last Updated: 1/12/2009
When you wish to use the CDROM drive it must first have a cdrom in the drive and data on the cdrom. To view the data you must mount the device to a given place of the Linux filing tree. Everything in Linux is a file. The whole partition of a hard dr
Read more...

 

Linux File Structure

Last Updated: 1/12/2009
Understanding the basic idea behind the Linux file structure can help but only if other people follow this idea!
Read more...

 

Beginners Guide to Vi

Last Updated: 1/12/2009
Beginners Guide to Vi
Read more...

 

Mounting a Drive

Last Updated: 1/12/2009
In Linux, like in UNIX, diskettes and CD-ROMs need to be mounted before they can be used. What mount does is to associate the devices (floppy or CD-ROM drives) to a mount point (a directory) in the root file system. The usual mount point for diskette
Read more...

 

Linux in a nut Shell

Last Updated: 1/12/2009
Why should we bother learning Linux shell command when we have GUI like KDE?
Read more...