Pipes


As I keep alluding to, Unix commands alone are powerful, but when you combine them together, you can accomplish complex tasks with ease. The way you combine Unix commands is through using pipes.

To create a pipe in Unix, you simply use a | character between the programs on the command line. A pipe is created by using the keystroke Shift+\. This creates a pipe, which looks like this: |. For those of you who are old MS-DOS users, yes, the command has generally the same meaning and is also called a pipe. The pipe, used at the shell prompt, will also help manipulate input. Let's look at an example of a pipe in use:

 >cat newfile1.txt Hello How are you Fine >cat newfile1.txt | wc       1       3      1 

The word count command (WC) was used with cat and that's what the pipe is good at. Instead of being able to change output from one direction to another based on a file, you can now do the same with commands. This essentially is your primer for joining Unix commands together to unleash even more power under the hood.

We can also pipe into pagers. We learned about pagers earlier in this book. Piping things into pagers is common when you want to view a long listing and do not want it to run off the screen. This was another commonly used MS-DOS command. In Unix, simply type

 > ls -l | more 

This looks at your current directory listing, and if it is too long, you can use the more command in conjunction with the ls command to stop the listing at a page. Then you need to press Enter or your spacebar to cycle through the listing. Ctrl+z can break the list if it appears to go on forever.

Watch future examples carefully because the pipe will appear in more useful contexts throughout the rest of the book. In the meantime, make sure that you practice the pipe command with this exercise and master the fundamentals.



    SAMS Teach Yourself Unix in 10 Minutes
    Sams Teach Yourself Unix in 10 Minutes (2nd Edition)
    ISBN: 0672327643
    EAN: 2147483647
    Year: 2005
    Pages: 170

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net