![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Complete Idiot's Guide to Linux
STDOUTMost of the programs you have used so far send their results to the standard output stream, STDOUT, which is usually wired to your console. Any time that you see something printed on the console (your screen), more than likely it was sent to the STDOUT stream. Lets experiment a little with redirecting STDOUT to a file. Redirecting to a File: > To redirect output to a file, you use the greater than symbol (>) and provide a filename to receive the output: [alberto@digital alberto]$ ls -l > /tmp/listing [alberto@digital alberto]$ cat /tmp/listing total 2 -rw-rw-r-- 1 alberto alberto 388 Jul 29 19:03 Xrootenv.0 -rw-rw-r-- 1 alberto alberto 0 Aug 5 20:16 file drwxrwsr-x 2 alberto groupfil 1024 Aug 4 22:13 groupfiles In this example I did just that. I asked for a listing of my files, but instead of getting the listing displayed on my screen, I redirected the output of the ls command to the file /tmp/listing. To prove that it worked, I then sent the contents of the /tmp/listing file to my screen using the cat command you learned in Chapter 12, Working with Files on the Shell. This technique is useful for creating a list of the files in a directory.
Now that you know about redirection, let me show you a neat trick: Lets use the cat command as a minimal text editor. We can do this by issuing the cat command and then redirecting the output to a file, like this: [alberto@digital alberto]$ cat > /tmp/message.txt This is a test. Until I type a Ctrl+D, I can type all that I want. I can even enter new lines. -- Alberto [alberto@digital alberto]$ cat /tmp/message.txt This is a test. Until I type a Ctrl+d, I can type all that I want. I can even enter new lines. -- Alberto [alberto@digital alberto]$ With redirection, you can use cat as a simple editor. You can edit the current line you are working on, as the shell wont grab the characters until you hit Enter. To finish entering text, just press Ctrl+D. Appending a File: >> Redirecting is very useful, but sometimes you dont want to replace the files previous contents. Instead, you might want to add information to the end of a file. Maybe you want to maintain a list of all your favorite Web sites. In such a case, adding a new entry should not replace the entire file but augment it. Instead of using a single >, you use a >> to tell the shell to append new output instead of overwriting. If the file doesnt exist, it is created, like this: [alberto@digital alberto]$ cat > /tmp/websites www.caldera.com www.redhat.com [alberto@digital alberto]$ cat >> /tmp/websites www.mcp.com [alberto@digital alberto]$ cat /tmp/websites www.caldera.com www.redhat.com www.mcp.com Why couldnt I have used append (>>) for the first cat? Because I enabled the noclobber feature, the >> command would have resulted in an error. Appending to files that dont exist produces an error when noclobber is set. (An alternative would have been to issue a touch /tmp/websites before using cat.)
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |