Monday, December 1, 2008

Shell Prompt Basics 02

11.9.2. Appending Standard Output

You can use output redirection to add new information to the end of an existing file. Similar to when you used the symbol, you tell your shell to send the information somewhere other than standard output.

 

However, when you use, you are adding information to a file, rather than replacing the contents of a file entirely.

 

The best explanation is a demonstration. Take two files which have already been created (sneakers.txt and home.txt) and join them by using the append output symbol. You want to add the information in home.txt to the information already in sneakers.txt, so type:

 

cat home.txt

 

sneakers.txt

 

Now check the file using the command cat sneakers.txt. The final output shows the contents of home.txt at the end of the file.

 

cat sneakers.txt

buy some sneakers

then go to the coffee shop

then buy some coffee

bring the coffee home

take off shoes

put on sneakers

make some coffee

relax!

 

The command you typed told the system to append the output from the file home.txt to the file sneakers.txt.

 

By appending the output, you save yourself time (and a bit of disk clutter) by using existing files, rather than creating a new file. Compare the results of the files sneakers.txt and saturday.txt now, and you will see that they are identical. To make your comparison, type:

 

cat sneakers.txt; cat saturday.txt

 

The contents of both files will be displayed  -- first sneakers.txt, then saturday.txt (as shown in Figure 11-7).

 

Figure 11-7. Stringing Commands and Comparing Files

 

11.9.3. Redirecting Standard Input

Not only can you redirect standard output, you can perform the same type of redirection with standard input. When you use the redirect standard input symbol, you are telling the shell that you want a file to be read as input for a command.

Use a file you have already created to demonstrate this idea.

 

Just type:

cat

sneakers.txt

 

Because you used the less-than symbol ( ) to separate the cat command from the file, the output of sneakers.txt was read by cat.

 

11.10. Pipes and Pagers

In Linux, pipes connect the standard output of one command to the standard input of another command. Consider the ls command that was discussed earlier. There are plenty of options available with ls, but what if the contents of a directory scroll by too quickly for you to view them? View the contents of the /etc directory.

 

ls -al /etc

 

How do you get a closer look at the output before it moves off the screen?

One way is to pipe the output to a utility called less. less is a pager utility that allows you to view information one page (or screen) at a time.

Use the vertical bar (|) to pipe the commands.

 

ls -al /etc | less

 

Now you can view the contents of /etc one screen at a time. To move forward a screen, press [Space]; to move back a screen, press [B]; to quit, press [Q]. Alternatively, you can use the arrow keys to navigate with less.

 

Tip

To read startup messages more closely, at a shell prompt, type dmesg | less. You will be able to read the file one screen at a time. Use the arrow keys to navigate the file.

Pipes can also be used to print only certain lines from a file.

Type:

grep coffee sneakers.txt | lpr

 

This will print every line in the sneakers.txt file that mentions the word "coffee" (read more about grep in Section 11.11.3).

 

11.10.1. The more Command

The main difference between more and less is that less lets you move backward and forward using the arrow keys, while more uses the [Spacebar] and the [B] key for forward and backward navigation. List the contents of the /etc directory using ls and more.

ls -al /etc | more

 

Figure 11-8. Piping Output of ls to more

 

Use the [Spacebar] to move forward through the pages. Press [q] to exit.

 

11.11. More Commands for Reading Text Files

You have already been introduced to several basic shell prompt commands for reading files in text editors. Here are a few more.

 

11.11.1. The head Command

You can use the head command to look at the beginning of a file. The command is:

 

head

 

filename head can be a useful command, but because it is limited to the first several lines, you will not see how long the file actually is.

 

By default, you can only read the first ten lines of a file. You can change the number of lines displayed by specifying a number option, as shown in the following command:

head -20

 

filename

 

11.11.2. The tail Command

The reverse of head is tail. Using tail, you can view the last ten lines of a file.

 

11.11.3. The grep Command

The grep command is useful for finding specific character strings in a file. For example, if you want to find every reference made to "coffee" in the file sneakers.txt, you would type:

 

grep coffee sneakers.txt

 

You would see every line in that file where the word "coffee" is found.

 

Tip

Unless otherwise specified, grep searches are case sensitive. That means that searching for Coffee is different than searching for coffee. So among grep's options is -i, which allows you to make a case-insensitive search through a file. Read the grep man page for more about this command.

 

11.11.4. I/O Redirection and Pipes

You can use pipes and output redirection when you want to store and/or print information to read at a later time.

 

You can, for example, use grep to search for particular contents of a file, then have those results either saved as a file or sent to a printer.

To print the information about references to "coffee" in sneakers.txt, for example, just type:

 

grep coffee sneakers.txt | lpr

 

 

11.11.5. Wildcards and Regular Expressions

What if you forget the name of the file you are looking for? Using wildcards or regular expressions, you can perform actions on a file or files without knowing the complete filename. Just fill out what you know, then substitute the remainder with a wildcard. Wildcards are special symbols that you can substitute for letters, numbers, and symbols that make finding particular directories and files easier than examining long directory listings to find what you are searching for.

 

Tip

To read more about wildcards and regular expressions, take a look at the bash man page (man bash).

Remember that you can save the file to a text file by typing man bash | col –b bash.txt.

 

Then, you can open and read the file with less or pico (pico bash.txt). If you want to print the file, be aware that it is quite long. We know the file is called "sneak____.txt," so type:

 

ls sneak*.txt and there is the name of the file:

 

sneakers.txt

 

You will probably use the asterisk (*) most frequently when you are searching. The asterisk will search out everything that matches the pattern you are looking for. So even by typing:

 

ls *.txt

or:

ls sn*

 

You would find sneakers.txt and any other files whose names begin with sn or ends with .txt. It helps to narrow your search as much as possible. One way to narrow a search is to use the question mark symbol (?). Like the asterisk, using ? can help locate a file matching a search pattern. In this case, though, ? is useful for matching a single character, so if you were searching for sneaker?.txt, you would get sneakers.txt as a result, and/or sneakerz.txt, if there were such a filename.

 

Regular expressions are more complex than the straightforward asterisk or question mark. When an asterisk, for example, just happens to be part of a filename, as might be the case if the file sneakers.txt was called sneak*.txt, that is when regular expressions can be useful.

 

Using the backslash (\), you can specify that you do not want to search out everything by using the asterisk, but you are instead looking for a file with an asterisk in the name.

 

If the file is called sneak*.txt, type:

sneak\*.txt

 

Here is a brief list of wildcards and regular expressions:

 

Ø      *  -- Matches all characters

Ø      ?  -- Matches one character in a string (such as sneaker? .txt)

Ø      \*  -- Matches the * character

Ø      \?  -- Matches the ? character

Ø      \)  -- Matches the ) character

No comments: