Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Hour 3
Working with Files

In UNIX there are three basic types of files:

  Ordinary Files
  Directories
  Special Files

An ordinary file is a file on the system that contains data, text, or program instructions. In this chapter, you look at working with ordinary files.


Directories, covered in Chapter 4, “Working with Directories,” store both special and ordinary files. For users familiar with Windows or Mac OS, UNIX directories are equivalent to folders.

Special files are covered in Chapter 5, “Manipulating File Attributes.” Some special files provide access to hardware such as hard drives, CD-ROM drives, modems, and Ethernet adapters. Other special files are similar to aliases or shortcuts and enable you to access a single file using different names.

Listing Files

First, list the files and directories stored in the current directory. Use the following command:

$ ls

Here’s a sample directory listing:

bin           hosts         lib           res.03
ch07          hw1           pub           test_results
ch07.bak      hw2           res.01        users
docs          hw3           res.02        work

This output indicates that several items are in the current directory, but this output does not tell us whether these items are files or directories. To find out which of the items are files and which are directories, specify the -F option to ls:

$ ls -F

Now the output for the directory is slightly different:

bin/           hosts          lib/           res.03
ch07           hw1            pub/           test_results
ch07.bak       hw2            res.01         users
docs/          hw3            res.02         work/

As you can see, some of the items now have a / at the end: each of these items is a directory. The other items, such as hw1, have no character appended to them. This indicates that they are ordinary files.

When the -F option is specified to ls, it appends a character indicating the file type of each of the items it lists. The exact character depends on your version of ls. For ordinary files, no character is appended. For special files, a character such as !, @, or # is appended to the filename.

For more information on the exact characters your version of ls appends to the end of a filename when the -F option is specified, please check the UNIX manual page for the ls command. You can do this as follows:

$ man ls

So far, you have seen ls list more than one file on a line. Although this is fine for humans reading the output, it is hard to manipulate in a shell script. Shell scripts are geared toward dealing with lines of text, not the individual words on a line. Without using external tools, such as the awk language covered in Chapter 17, “Filtering Text Using awk,” it is hard to deal with the words on a line.

In a shell script it is much easier to manipulate the output when each file is listed on a separate line. Fortunately ls supports the -1 option to do this. For example,

$ ls -1

produces the following listing:

bin
ch07
ch07.bak
docs
hosts
hw1
hw2
hw3
lib
pub
res.01
res.02
res.03
test_results
users
work

Hidden Files


So far you have used ls to list visible files and directories, but ls can also list invisible or hidden files and directories. An invisible file is one whose first character is the dot or period character (.). UNIX programs (including the shell) use most of these files to store configuration information. Some common examples of hidden files include the files
  .profile, the Bourne shell (sh) initialization script
  .kshrc, the Korn shell (ksh) initialization script
  .cshrc, the C shell (csh) initialization script
  .rhosts, the remote shell configuration file

All files that do not start with the . character are considered visible.

To list invisible files, specify the -a option to ls:

$ ls -a

The directory listing now looks like this:

.             .profile      docs          lib           test_results
..            .rhosts       hosts         pub           users
.emacs        bin           hw1           res.01        work
.exrc         ch07          hw2           res.02
.kshrc        ch07.bak      hw3           res.03

As you can see, this directory contains many invisible files.

Notice that in this output, the file type information is missing. To get the file type information, specify the -F and the -a options as follows:

$ ls -a -F

The output changes to the following:

./             .profile       docs/          lib/           test_results
../            .rhosts        hosts          pub/           users
.emacs         bin/           hw1            res.01         work/
.exrc          ch07           hw2            res.02
.kshrc         ch07.bak       hw3            res.03

With the file type information you see that there are two hidden directories (. and ..). These two directories are special entries that are present in all directories. The first one, ., represents the current directory. The second one, .., represents the parent directory. We discuss these concepts in greater detail in section “The Directory Tree” of Chapter 4.

Option Grouping

In the previous example, the command that you used specified the options to ls separately. These options can also be grouped together. For example, the commands

$ ls -aF
$ ls -Fa

are the same as the command

$ ls -a -F

As you can see, the order of the options does not matter to ls. As an example of option grouping, consider the equivalent following commands:

ls -1 -a -F
ls -1aF
ls -a1F
ls -Fa1

Any combination of the options -1, -a, and -F produces identical output:

./
../
.emacs
.exrc
.kshrc
.profile
.rhosts
bin/
ch07
ch07.bak
docs/
hosts
hw1
hw2
hw3
lib/
pub/
res.01
res.02
res.03
test_results
users
work/


Previous Table of Contents Next