Linux Topic
   >  Why use the Command Line?
   >  Common Linux Commands
   >  Directory Structure
   >  Using Regular Expressions
   >  $STDIN; $STDOUT and $STDERR
   >  IO Redirection and Pipes

 

Redirectors

Input / Output Redirectors

One of the most powerful tools on Linux is the ability to direct the input and output of any command. This may not seem like much, but think about it: you can direct the output of any command to a file, device or to be used as input to any other Linux command. It seems trivial, but this really does give Linux (-and Unix, of course) a huge advantage over other operating systems. Furthermore, it can be used on the command line or in scripts in exactly the same way!

When it comes down to it, there are only a few redirectors in Linux:

CommandDescriptionExample Usage
>Redirect the $STDOUT from the command to the left of the > symbol to the file or device listed on the right of the > symbol$ ls > /var/temp/myfiles.txt
>>The same as the > symbol, except that the output is appended to the file rather than overwriting it$ ls >> todaysFiles.txt
<Takes the input ($STDIN) for the command to the left of the < symbol from the file or device listed on the right of the < symbol$ more < /var/temp/myfiles.txt
|Takes the output ($STDOUT) of the command to the left of the pipe (|) symbol as input to the command listed on the right of the pipe symbol$ ps -ef | grep "root"
2>Redirect the $STDERR from the command to the left of the > symbol to the file or device listed on the right of the > symbol$ grep "abc" /* 2> errorFile
2>>The same as the 2> symbol, except that the output is appended to the file rather than overwriting it$ grep "abc" /* 2>> todaysErrors

Getting your mind around these redirectors is fundamental to Linux scripting. There's a lot of useful tutorials out there on the web (-use your favourite search engine to look to "linux io redirection" if you require more information).


Redirection Examples

Let's go back to the example in the chapter on the differences between $STDIN, $STDOUT and $STDERR. Let's assume that, in this case, we don't care about the files which we don't have read access to (-i.e. the original cause of the error messages). As a result, we can just redirect $STDERR to the null device (/dev/null) without affecting the valid output, thus:

$ grep "abc" /* 2> /dev/null
/* this is file "abc" */

Note: in the real world, you need to be careful about throwing away all error messages (-especially in Scripts, as you may miss something crucial).

Now let's look at an example of what we find is the most powerful of the redirectors: the "pipe". This allows you to "pipe" the output of one command ($STDOUT) to the input of another ($STDIN). Here's a very common example of using the "grep" command to pick out running processes on the system that are owned by a particular user:

$ ps -ef | grep "root"
root         1     0  0 10:16 ?        00:00:00 /sbin/init
root         2     0  0 10:16 ?        00:00:00 [kthreadd]
(....etc....)

What this is doing is running the ps command, then using it's output as input to the grep command. The result is a list of all processes running that are owned by the "root" user (-or, more properly, that mention the string "root" somewhere). Now let's add another command to remove all the other output on the line and just return the command used to start the process:

$ ps -ef | grep "root" | awk '{ print $8 }'
/sbin/init
[kthreadd]
(....etc....)

Now we have three commands on a line all joined together using pipes! What this is doing is running the same command as before except that the "grep" output is then used as input to an awk command which prints out the 8th argument on each line. The result is now a list of just the command lines used by all "root" processes!

Obviously, this can continue ad-infinitum, until you get the result you desire, for example:

$ ps -ef | grep root | grep events | awk '{ printf("Process is %s\n", $8) }'
Process is [events/0]
Process is [events/1]
Process is [events/2]
Process is [events/3]
(....etc....)

Dealing with Long Lines of Code

It's this ability to stick together multiple commands on a line that gives the command line it's power: you can do an awful lot in just one line of code! However, for the novice user it does tend to make it intimidating when you see a long line of what looks like gobbledygook: the trick is to work from left to right, taking each command up to the next pipe and just working out what it does. The command as a whole may be daunting, but the individual stanzas are normally trivial. It's like eating an elephant: take it one bite at a time!


HomeSite IndexDesktop GuideServer GuideHints and TipsHardware CornerVideo SectionContact Us

 sitelock verified Firefox Download Button