-->
Previous | Table of Contents | Next |
Many Linux commands let you specify which file or directory they are to act upon, as we saw earlier with the example ls -l /bin.
You can also pipe the output from a command so that it becomes another commands input. This is done by typing two or more commands separated by the | character. (This character normally is found on the same key as the \ character. You must hold the Shift key or you get \ instead of |). The | character means Use the output from the previous command as the input for the next command. Therefore, typing command_1|command_2 does both commands, one after the other, before giving you the results.
Using our assembly-line metaphor, items are being processed through two black boxes instead of just one. When we use piping, its like hooking up the first commands output conveyor belt to become the input conveyor belt for the second command.
Tip:
Although Linux doesnt care whether | is set off by spaces, if command_1 | command_2 is easier for you to read and understand than command_1|command_2, by all means use spaces around |.
You may have noticed that the output of ls -l /bin is many lines long, so that much of the information scrolls off the screen before you can read it. You can pipe this output to a formatting program called more, which displays information in screen-sized chunks. When you enter ls -l /bin | more, you see the following:
darkstar:~$ ls -l /bin | more total 1611 -rwxr-xr-x 1 root bin 1248 Sep 17 04:25 arch* -rwxr-xr-x 1 root bin 295940 Sep 5 01:45 bash* -rwxr-xr-x 1 root bin 4840 Nov 24 1993 cat* -rwxr-xr-x 1 root bin 9220 Jul 20 12:06 chgrp* -rwxr-xr-x 1 root bin 13316 Jul 20 12:06 chmod* -rwxr-xr-x 1 root bin 13316 Jul 20 12:06 chown* lrwxrwxrwx 1 root root 17 Dec 7 13:37 compress -> /usr/bin/comp ress* -rwxr-xr-x 1 root bin 21508 Jul 20 12:06 cp* -rwxr-xr-x 1 root bin 41988 May 1 1994 cpio* lrwxrwxrwx 1 root root 4 Dec 7 13:40 csh -> tcsh* -rwxr-xr-x 1 root bin 5192 Nov 24 1993 cut* -rwxr-xr-x 1 root bin 19872 Mar 23 1994 date* -rwxr-xr-x 1 root bin 17412 Jul 20 12:06 dd* -rwxr-xr-x 1 root bin 13316 Jul 20 12:06 df* -rwxr-xr-x 1 root bin 66564 Jun 9 1994 dialog* -rwxr-xr-x 1 root bin 1752 Sep 17 04:25 dmesg* lrwxrwxrwx 1 root root 8 Dec 7 13:37 dnsdomainname -> hostname * -rwxr-xr-x 1 root bin 13316 Jul 20 12:06 du* -rwxr-xr-x 1 root bin 3312 Mar 23 1994 echo* -rwxr-xr-x 1 root bin 36684 May 4 1994 ed* -rwxr-xr-x 1 root bin 326 Mar 23 1994 false* --More--
The --More-- at the bottom of the screen tells you that theres more text to come. To go to the next screen of text, press the Spacebar. Every time you press the Spacebar, more displays another screen full of text. When the last screen with text has been displayed, more returns you to the Linux prompt.
Tip:
The more command can do many other things. For instance, to move back one screen at a time, press b for back. (Not all versions of more support the b option.) Another useful command is q for quit. This lets you leave immediately, without having to go through all the remaining screens of text. Ctrl+C does the same thing.While in more, press h for help. This lists the commands available within more.
Note:
Linux sometimes includes the command less instead of more. One difference you will notice is that, unlike more, less requires you to type q to return to the command line, even if youre at the end of the text to be displayed. This may seem cumbersome, but it prevents you from accidentally exiting the program by pressing the Spacebar once too often.The name less is a play on more. Originally, less was designed to have many features that more lacked. The version of more included in most Linux system has most of these features, however.
The Linux man program, discussed later, uses less to display text. Most other UNIX systems use more by default. Dont get confused. Just remember to press q to exit from less!
Another thing you can do in Linux is send output to a file instead of the screen. There are many different reasons why you might want to do this. Perhaps you want to save a snapshot of a commands output as it was at a certain time or maybe you want to save a commands output for further examination. You might also want to save the output from a command that takes a very long time to run, and so on.
To send output to a file, use the > symbol (found above the period on your keyboard). For instance, you can place the output of the ls -l /bin command into a file called output by typing ls -l /bin > output. Again, spaces around > are optional and not strictly necessary, but they do make the command much more readable.
If you now enter an ls or ls -l command, you will see that youve created a new file called output in your own directory.
Previous | Table of Contents | Next |