Monday, December 1, 2008

Shell Prompt Basics 01

11.5. View Directory Contents with ls

Now that you know how to change directories, it is time to learn how to view the contents of these directories. Using the ls command, you can display the contents of your current directory. Many options are available with the ls command. The ls command, by itself, will not show you all the files in your directory. Some files are hidden files (also called dot files) and can only be seen with an additional option specified to the ls command.

 

Tip

To see all the options of the ls command, you can read the man page by typing man ls at a shell prompt. If you want to print the man page, at the prompt type man ls | col -b | lpr.

Type the command ls -a. Now you will see files that begin with dots.

 

Figure 11-3. ls with the -a Option

 

Hidden files are mostly configuration files which set preferences in programs, window managers, shells, and more. The reason they are hidden is to help prevent any accidental tampering by the user.

 

When you are searching for something in a directory, you are not usually looking for these configuration files, so keep them hidden to help avoid some screen clutter when viewing directories at the shell prompt.

 

Viewing all the files using the ls -a command can give you plenty of detail, but you can view still more information, simply by adding more than one option. If you want to see the size of a file or directory, when it was created and more, just add the long option (-l) to the ls -a command. This command shows the file creation date, its size, ownership, permissions, and more. You do not have to be in the directory whose contents you want to view to use the ls command.

 

For example, to see what is in the /etc directory from your home directory,

type:

ls -al /etc

 

Figure 11-4. Sample ls Output for the /etc Directory

 

The following is a short list of some options commonly used with ls. Remember, you can view the full list by reading the ls man page (man ls).

 

Ø      -a  -- all. Lists all the files in the directory, including the hidden files (.filename). The .. and . at the top of your list refer to the parent directory and the current directory, respectively.

Ø      -l --long. Lists details about contents, including permissions (modes), owner, group, size, creation date, whether the file is a link to somewhere else on the system and where its link points.

Ø      -F  -- file type. Adds a symbol to the end of each listing. These symbols include / to indicate a directory; @ to indicate a symbolic link to another file; and * to indicate an executable file.

Ø      -r  -- reverse. Lists the contents of the directory from back to front.

Ø      -R  -- recursive. This option lists the contents of all directories below the current directory recursively.

Ø      -S  -- size. Sorts files by their size.

 

11.6. Locating Files and Directories

There will be times when you know a file or directory exists but you will not know where to find it. Searching for a file or directory can be easier with the locate command. With locate, you will see every file or directory whose name contains the search criterion. For example, if you want to search for all files with the word finger in the name, type:

 

locate finger

 

The locate command uses a database to locate files and directories that have the word finger in the file or directory name. The search results could include a file called finger.txt, a file called pointerfinger.

 

txt, a directory named fingerthumbnails, and so on. To learn more about locate,

read the locate man page (type man locate at a shell prompt).

 

The locate command works very quickly, as long as the database is up to date. That database is automatically updated on a nightly basis through a cron job. cron is a small program that runs in the background, performing various tasks (such as updating the locate database) at regularly scheduled intervals.

 

Tip

Cron is a daemon that executes tasks at regularly scheduled intervals. To read the cron man page, type man cron at the shell prompt. cron periodically updates the slocate database, which is used to catalog file locations. Switching between operating systems and shutting down your machine at the end of the day can interfere with

the automatic database update run by cron. To update the database manually, log in as root (type su at a shell prompt and then your root password) and type the command updatedb. After a few minutes, the slocate database that is used by the locate command will be current.

 

Note

You can run anacron to have your system execute commands periodically, with a frequency specified in days. Unlike cron, it does not assume that the machine is running continuously. Hence, it can be used on machines that are not running 24 hours a day, to control daily, weekly, and monthly jobs that are usually controlled by cron. Refer to the man page on anacron (type man anacron at the command line) for more information.

 

11.7. Printing From The Command Line

Printing is not an involved process whether you click on a button in a GUI or type commands from the command line. This section explains how to print, cancel, and view print jobs from the command line, assuming you have a properly configured printer connected to your system. Refer to Chapter 7 for more information about setting up your printer. The lpr command, followed by a filename, sends that specified file to the print queue.

 

For example, lpr foo.txt prints the foo.txt file.

 

To view the jobs waiting in the print queue, type lpq at the command line. Type lpq, and you will see information similar to this:

 

active root 389 foo.txt

 

In this example, 389 is the job number.

 

You can cancel jobs in the queue by typing lprm followed by the print job number displayed when you use the lpq command. To cancel the foo.txt print job, type lprm 389 and press [Enter].

 

11.8. Clearing and Resetting the Terminal

After even one ls command in a shell prompt, the terminal window you are working in can begin to look crowded. You can always exit from the terminal window and open a new one, but there is a quicker and easier way to clear the contents displayed in the terminal.

 

Try typing the command clear at the shell prompt. The clear command does just what it implies: it clears the terminal window. Sometimes, you may accidentally open a program file or some other non-text file in a terminal window. Once you close the file, you could find that the text you are typing does not match the output on the monitor. In such cases, type reset to return the terminal window to its default values.

 

11.9. Manipulating Files with cat

Red Hat Linux has a utility which can help you keep short lists, gather lists together, and even show you information about your system. The utility is called cat, short for concatenate, which means to combine files. The command cat will also display the contents of an entire file on the screen (for example, type cat filename.txt). If the file is fairly long, it will quickly scroll past you on the screen.  To prevent this, use the cat filename.txt | less command.

 

Using the pipe (|) and the less command together displays the file one page at a time. You can then use the up and down arrow keys to move backward and forward through the pages. For more on using pipes to combine two separate functions, see Section 11.10.

 

11.9.1. Using Redirection

Redirection means causing the shell to change what it considers to be standard input or where the standard output should be going.

To redirect standard output, use the symbol. Placing after the cat command (or after any utility or application that writes to standard output) will direct its output to the filename following the symbol.

 

For example, using cat by itself simply outputs whatever you input to the screen as if it were repeating the line you just typed. The following example shows cat repeating every line that is entered:

 

[sam@halloween sam]$cat

buy some sneakers

buy some sneakers

then go to the coffee shop

then go to the coffee shop

then buy some coffee

then buy some coffee

 

To redirect the output of cat to a file, type the following at a shell prompt (pressing the [Enter] key takes you to the next blank line):

 

cat

sneakers.txt

 

 

Figure 11-5. Redirecting Output to a File

 

Press [Enter] to go to an empty line and use the [Ctrl]-[D] keys to quit cat.

Do you notice anything different in Figure 11-5? There are no repeated entries. That is because the standard output from cat was redirected. That redirection was to a brand new file you made called sneakers.txt.

 

You can find the file in the directory you were in when you started cat (type ls if you want to see it listed). As you learned earlier, you can then use cat to read the file. At the prompt, type:

cat sneakers.txt

 

Caution

Be careful when you redirect the output to a file, because you can easily overwrite an existing file! Make sure the name of the file you are creating does not match the name of a pre-existing file, unless you want to replace it. Use output redirection again for another file and call it home.txt.

 

For this example, type the following:

cat

 

home.txt

bring the coffee home

take off shoes

put on sneakers

make some coffee

relax!

 

Now, on an empty line, use the [Ctrl]-[D] key combination again to quit cat.

Next, use cat to join home.txt with sneakers.txt and redirect the output of both files to a brand new file called saturday.txt (you will find an example in Figure 11-6).

 

Type the following:

cat sneakers.txt home.txt

 

saturday.txt

 

Figure 11-6. Joining Files and Redirecting Output

 

You can see that cat has added home.txt where sneakers.txt ended.

No comments: