File Management  «Prev  Next»
Lesson 2 More options for listing files
Objective List files using the ls command.

More Options for listing Files using Unix

You already should be familiar with two of the ls command’s more useful options:
  1. -l, which displays the listing in long format
  2. -F, which flags directory names with a trailing slash (/).
This lesson will introduce three more ls options: -a, -d, and -R.

Listing hidden files

Normally, the ls command does not display files that start with a dot (.).
These hidden files typically contain preference settings for certain UNIX commands. If you want to list all files, including hidden files, use ls -a. Every directory contains at least two hidden files: the . and .. directories. They are shorthand for the current and parent directories respectively. When you specify a relative pathname using . or .., you are referencing these hidden directories.

Listing a Directory Tree

Directories often contain other directories, which in turn can contain their own subdirectories, and so on down the file hierarchy. UNIX lets you display a directory tree all at once by using the ls -R command. The -R option produces a recursive listing [1]. That is, ls descends each subdirectory, listing the contents until the bottom of the hierarchy is reached.


Listing a directory name

Normally, when you specify a directory name with ls, you see the contents of the directory.
But if you want to display the directory itself, use the -d option. The following Slide Show shows how to use the -a, -R, and -d options.

1) ls-F command marks directories with a trailing slash (/)
1) The ls-F command marks directories with a trailing slash (/)

2) When you add the -a option, ls lists hidden files.
2) When you add the -a option, ls lists hidden files.

3) Use the -R option to produce a recursive listing (a directory tree). To start from your current directoy, do not supply a directory name.
3) Use the -R option to produce a recursive listing (a directory tree). To start from your current directoy, do not supply a directory name.

4) Suppose you want to check permissions on the images directory. This command does the wrong thing. It lists the contents of the directory instead of the directory itself.
4) Suppose you want to check permissions on the images directory. This command does the wrong thing. It lists the contents of the directory instead of the directory itself.

5) To list the right information, add the -d option to show only the directory name.
5) To list the right information, add the -d option to show only the directory name.

Using options with ls in Unix

dir dir [options] [file]
List directory contents. dir is equivalent to the command
ls -C -b 
(list files in columns, sorted vertically, special characters escaped) and it takes the same arguments as ls. This is an alternate invocation of the ls command and is provided for the convenience of those converting from Microsoft Windows and the DOS shell.
In the next lesson, copying directories by using the -r option of the cp command will be discussed.


[1] recursive listing: A recursive listing of a directory is one that repeatedly displays all subdirectories down the hierarchy, until the last level of the directory tree is reached.