Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)

Seeing the Contents of Directories

We normally think of a directory as "containing" files, but in reality a directory is just a special kind of file. A directory contains a list of entries. Each entry is the name of a file or another directorythis is an important concept. In Unix, the only place where a file's name is stored is in the directory. Filenames are not stored inside the files themselves .

When you rename a file, you are changing the entry in a directory, but not changing the file. And when you remove a file, you are removing the file's name from a directory.

The operating system removes the file if there are no other directory entries for that file. This becomes very important when trying to understand hard links , which are described below in the section "About Links (the Unix Version of Aliases)." (Hard links are a way of having more than one directory entry that refers to the same actual file.)

To list the contents of the current directory:

To list the contents of any directory:

Hidden files

Unix normally does not show you files whose names begin with a . (a period, or dot in Unix-speak). These dot files are typically configuration files that are used by programs such as your shell when they start up, and thus they are something like the preferences files that littered your System Folder in preOS X versions of the Mac OS. See the sidebar "More About Hidden Files" for details.

To see hidden files:

Getting more information from ls

The -l option to ls gives the "long" form of its output, listing one line for each entry and giving information about the file's size , date of last change, and other information.

See the section "Getting Information About Files and Directories," later in this chapter, for more on the -l option.

Sorting the output of ls

The default for ls is to sort its output alphabetically . By using the -t option you can cause the output to be sorted according to the time the file was last changed.

To sort the list by time instead of name:

To reverse the sort order:

Tip

To list only files matching a pattern:

See the entry on command-line wildcards in Chapter 2 or pipe the output of ls tHRough grep (review Chapter 4 or man grep ).

Sometimes you will want to list everything in a directory, including what's in any subdirectories, and any subdirectories of those directories, and so on. This is known as a recursive listing. There is an easy way to do this:

To recursively list the contents of a directory:

Категории