UNIX Unbounded: A Beginning Approach

Chapter 7: The UNIX File System Continued


Objectives:

This chapter continues the discussion of the UNIX file system. The objectives important to this chapter are:

  • using the pg command
  • input and output redirection
  • further uses of the cat command
  • using the cp, mv, ln and wc commands
  • metacharacters in UNIX commands
Concepts:

The cat command can be used to read a file, write a file and copy a file. One of its shortcomings, however, is that it has no reverse gear, so we should look at the next command, pg, the page command.

To use the pg command, type

 pg filename

with filename being the name of the file you want to read. (This is not an editing command.) The command can be varied in several ways. A useful one is to enter
 pg -25 filename

which would display 25 lines on the screen for each page. You can use any integer in place of 25, but limit yourself to the number of lines your screen can display at once.

The concept of redirection comes up in most operating system. In UNIX we can redirect both the input and the output of programs. Output redirection is sending the output of a program someplace other than the default place. The general syntax for the two output redirection operators is:

 command > filename
 command >> filename

The single greater-than sign sends the output of the command to the file named on the command line. If the file does not exist, it is created; if it does exist, it is overwritten. The double greater-than sign will do the same thing, except it appends to the end of an existing file. This procedure works for most UNIX commands.

The single input redirection operator works in a similar way. A command like this:

 command < filename

will use the contents of the named file as for input to the specified command. The double less-than sign does not append, however. It is a signal to the command to take input from the normal place and to stop taking input when a specified tag is encountered. This will be explained in more detail in Chapter 10. (For those who want more now, skip ahead.)

The author discusses further details on file printing. The most useful commands in his list may be the -l and -w options, which specify a page length in lines and width in characters.

The author discusses four shell commands that the reader may find useful.

  • cp - this command copies files
  • MV - this command moves or renames files
  • ln - this command makes links, which are alternate names for files
  • wc - this one counts the lines, words and characters in a file
The cp command can be used to copy file in the way you might expect:
 cp filename newname 

If not copying a file from or into the current directories, the command can include path names:
 cp /directory/filename /another_directory/newname 

If the filename to copy to is actually the name of a directory, the file is copied into that directory, keeping its original file name. This command will overwrite files without asking for permission. To get it to ask, you must use the -i option, for interactive copying.

The MV command is like copy, except that it erases the original file. If we leave directories out of the picture, the command

 MV filename newname 

would simply rename the existing file. With paths specified, it is also relocated.

The ln command is used to call the same file by two or more names. In this example

 ln filename newname 

newname is established as an alias, or link, to filename. Either name can be used in a program command to affect the single file that still exists. The link is not a copy, it is simply another name that the same file goes by. The wc command is useful for getting statistics on text files. It can be used with no options by entering
 wc filename

This will return the number of lines in the file, the number of words in the file, the number of characters in the file, and the file name. You can limit the response by using the standard hyphen followed by the first letter of what you want (l,w or c). You can ask for one, two or all three values, however, the default is all three, so it is silly to ask for all three in an option.

The next topic is metacharacters, which are like wild cards. I have already posted a discussion of them in the Chapter 4 notes for the Shell Programming class. Please refer to that document for details.