Home > Articles

Working with Files in a Shell

  • Print
  • + Share This
This chapter is from the book

In UNIX there are two basic types of files: ordinary and special. An ordinary file contains data, text, or program instructions. Almost all of the files on a UNIX system are ordinary files. This chapter covers operations on ordinary files.

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

Both ordinary and special files are stored in directories. Directories are similar to folders in the Mac OS or Windows, and they are covered in detail in Chapter 4, "Working with Directories."

In this chapter, we will examine ordinary files, concentrating on the following topics:

  • Listing files

  • File contents

  • Manipulating files

Listing Files

We'll start by using the ls (short for list) command to list the contents of the current directory:

$ ls

The output will be similar to the following:

Desktop     Icon      Music      Sites
Documents    Library     Pictures    Temporary Items
Downloads    Movies     Public

We can tell 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, we can specify the -F option to ls. An option is an argument that starts with the hyphen or dash character, '-'.

The following example illustrates the use of the -F option of ls:

$ ls -F

Now the output for the directory is slightly different:

Desktop/    Icon      Music/     Sites/
Documents/   Library/    Pictures/    Temporary Items/
Downloads/   Movies/     Public/

As you can see, some of the items now have a / at the end, incicating each of these items is a directory. The other items, such as icon, 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 -F options, check the UNIX manual page for the ls command. You can do this as follows:

$ man ls

Options Are Case Sensitive

The options that can be specified to a command, such as ls, are case sensitive. When specifying an option, you need to make sure that you have specified the correct case for the option. For example, the output from the -F option to ls is different from the output produced when the -f option is specified.

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. Although external tools, such as the awk language covered in Chapter 17, "Filtering Text with awk," can be used to deal with multiple words on a line, it is much easier to manipulate the output when each file is listed on a separate line. You can modify the output of ls to this format by using the -1 option. For example,

$ ls -1

produces the following listing:

Temporary Items

Hidden Files

In the examples you have seen thus far, the output has listed only the visible files and directories. You can also use ls to list invisible or hidden files and directories. An invisible or hidden file is one whose first character is a dot or period (.). Many programs, including the shell, use such files to store configuration information. Some common examples of invisible files include

  • .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 resembles this:

.          .FBCLockFolder   Icon        Public
..         .ssh        Library       Sites
.CFUserTextEncoding Desktop       Movies       Temporary Items
.DS_Store      Documents      Music
.FBCIndex      Downloads      Pictures

As you can see, this directory contains several 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:

./            .ssh/           Movies/
../            Desktop/         Music/
.CFUserTextEncoding    Documents/        Pictures/
.DS_Store         Downloads/        Public/
.FBCIndex         Icon?           Sites/
.FBCLockFolder/      Library/         Temporary Items/

With the file type information, you see that there are two invisible directories (. and ..). These directories are special entries present in all directories. The first one, ., represents the current directory, whereas the second one, .., represents the parent directory. These concepts are discussed in greater detail in Chapter 4.

Option Grouping

In the previous example, you specified the options to ls separately. You could have grouped the options together, as follows:

$ ls -aF
$ ls -Fa

Both of these commands are equivalent to the following command:

$ ls -a -F

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

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

All permutations of the options -1, -a, and -F produce the same output:

Temporary Items/
  • + Share This
  • 🔖 Save To Your Account