Chapter 3. Working with Files and Directories

Table of Contents
Getting information about files
Displaying information in a file
Creating and using directories
File security

Since ``everything'' on a unix system is a file, there are huge numbers of commands that affect files. Here we talk about some of the most important of these commands.

When you log in and start a command shell, your current working directory is your home directory. This special directory contains files that are used to customize your environment when you log in (see the Section called Changing .bashrc in Chapter 4 for more information on these startup files). It also contains any directories that you create and your own working files.

Getting information about files

The most fundamental file command is ls which is a mnemonic for ``list.'' This command displays the contents of a directory, i.e., the names of all the files and directories in it. Using the command without arguments or switches gives a brief multi-column listing of all the files in the current working directory:

Example 3-1. Basic ls command

$ ls
commands.html  environment.html  fs.png        jobs.html     start.html
CVS            #files.html#      index.html    kernel.html   style.css
edit           files.html        indx.html     links.html    template.html
edit.html      fs.dia            install.html  network.html  vi.html
emacs.html     fs.html           intro.html    shell.html

Here we see a listing of all the files in this directory, in case-insensitive alphabetical order, with regular files and subdirectories (CVS and edit) intermingled.

The most common switches used with the ls command are -land -a. The -l switch (for long), and the -a switch (for all), expand the information shown. As with many commands, the switches may be combined:

Example 3-2. A long file listing

$ ls -al
total 132
drwxrwxr-x    4 jbeck    jbeck        4096 Mar 25 12:55 .
drwxrwxr-x    4 jbeck    jbeck        4096 Mar 19 16:34 ..
-rw-rw-r--    1 jbeck    jbeck        5691 Mar 25 05:18 commands.html
drwxrwxr-x    2 jbeck    jbeck        4096 Mar 25 12:43 CVS
drwxrwxr-x    2 jbeck    jbeck        4096 Mar 23 17:36 edit
-rw-rw-r--    1 jbeck    jbeck         972 Mar 23 17:39 edit.html
-rw-rw-r--    1 jbeck    jbeck        1324 Mar 24 21:13 emacs.html
-rw-rw-r--    1 jbeck    jbeck        1119 Mar 23 17:39 environment.html
-rw-rw-r--    1 jbeck    jbeck        1819 Mar 25 12:54 #files.html#
-rw-rw-r--    1 jbeck    jbeck         724 Mar 23 17:39 files.html
-rw-rw-r--    1 jbeck    jbeck        2627 Mar 23 08:06 fs.dia
-rw-rw-r--    1 jbeck    jbeck        3042 Mar 24 17:47 fs.html
-rw-rw-r--    1 jbeck    jbeck        6563 Mar 23 08:06 fs.png
-rw-rw-r--    1 jbeck    jbeck           0 Mar 25 12:55 .htaccess
-rw-rw-r--    1 jbeck    jbeck        1700 Mar 23 17:39 index.html
-rw-rw-r--    1 jbeck    jbeck         372 Mar 23 17:39 indx.html
-rw-rw-r--    1 jbeck    jbeck        5722 Mar 25 05:18 install.html
-rw-rw-r--    1 jbeck    jbeck        1519 Mar 24 17:48 intro.html
-rw-rw-r--    1 jbeck    jbeck        8993 Mar 25 05:18 jobs.html
-rw-rw-r--    1 jbeck    jbeck        1653 Mar 24 17:44 kernel.html
-rw-rw-r--    1 jbeck    jbeck        1220 Mar 25 05:18 links.html
-rw-rw-r--    1 jbeck    jbeck        5797 Mar 25 12:43 network.html
-rw-rw-r--    1 jbeck    jbeck        2891 Mar 24 17:36 shell.html
-rw-rw-r--    1 jbeck    jbeck        1281 Mar 23 17:39 start.html
-rw-rw-r--    1 jbeck    jbeck         206 Mar 23 15:55 style.css
-rw-rw-r--    1 jbeck    jbeck         499 Mar 23 17:39 template.html
-rw-rw-r--    1 jbeck    jbeck        6334 Mar 25 05:18 vi.html

The -l switch is what causes the output to be listed in the columns. In order, the columns list the file type (first character), permissions (9 characters), number of hard links, owner name, group name, size in bytes, modification time, and file name. The -a switch means show all files, even the ``hidden'' ones whose names begin with the period character.

The output of ls may be sorted in many ways. For example, ls -ltr specifies a long-format listing, sorted by timestamp, reversed (i.e. oldest to most recent). It is also very common to specify arguments to ls using wildcards. The asterisk is used to specify 0 or more characters, and the question mark is used to specify exactly one character. For example:

Example 3-3. A file listing with wildcards

$ ls -l f?.*
-rw-rw-r--    1 jbeck    jbeck        2627 Mar 23 08:06 fs.dia
-rw-rw-r--    1 jbeck    jbeck        3042 Mar 24 17:47 fs.html
-rw-rw-r--    1 jbeck    jbeck        6563 Mar 23 08:06 fs.png

While the ls command can tell you a lot about files, it does not ``look inside them,'' so it can't really tell you what a file is for. Luckily, the file command can often help you figure out what kind of information is contained in a file:

Example 3-4. Using the file command

$ file fs.png edit
fs.png: PNG image data, 625 x 280, 8-bit/color RGB, non-interlaced
edit:   directory

This command was used with no switches and two arguments. The output says that the file fs.png is Portable Network Graphics image data, while edit is a subdirectory.