Displaying contents of a directory
Use the ls command to display the contents of a directory.
The ls command writes to standard output the contents of each specified Directory or the name of each specified File, along with any other information you ask for with the flags. If you do not specify a File or Directory, the ls command displays the contents of the current directory.
By default, the ls command displays all information in alphabetic order by file name. If the command is executed by a user with root authority, it uses the -A flag by default, listing all entries except dot (.) and dot dot (..). To show all entries for files, including those that begin with a dot (.), use the ls -a command.
- List one entry per line, using the -l flag.
- List entries in multiple columns, by specifying either the -C or -x flag. The -C flag is the default format when output is to a tty.
- List entries in a comma-separated series by specifying the -m flag.
To determine the number of character positions in the output line, the ls command uses the $COLUMNS environment variable. If this variable is not set, the command reads the terminfo file. If the ls command cannot determine the number of character positions by either of these methods, it uses a default value of 80.
The information displayed with the -e and -l flags is interpreted as follows:
The first character of each entry may be one of the following:
|d||Entry is a directory.|
|b||Entry is a block special file.|
|c||Entry is a character special file.|
|l||Entry is a symbolic link.|
|p||Entry is a first-in, first-out (FIFO) pipe special file.|
|s||Entry is a local socket.|
|-||Entry is an ordinary file.|
The next nine characters are divided into three sets of three characters each. The first three characters show the file or directory owner's permission. The next three characters show the permission of the other users in the group. The last three characters show the permission of anyone else with access to the file. The three characters in each set show read, write, and execute permission of the file. Execute permission of a directory lets you search a directory for a specified file.
Permissions are indicated as follows:
|r||Read permission granted|
|t||Only the directory owner or the file owner can delete or rename a file within that directory, even if others have write permission to the directory.|
|w||Write (edit) permission granted|
|x||Execute (search) permission granted|
|-||Corresponding permission not granted.|
The information displayed with the -e flag is the same as with the -l flag, except for the addition of an 11th character, interpreted as follows:
|+||Indicates a file has extended security information. For example, the file might have extended ACL, TCB, or TP attributes in the mode.|
|-||Indicates a file does not have extended security information.|
When the size of the files in a directory are listed, the ls command displays a total count of blocks, including indirect blocks.
- To list all files in the current directory, type the following:
This lists all files, including
- dot (.)
- dot dot (..)
- Other files whose names might or might not begin with a dot (.)
- To display detailed information, type the following:
This displays a long listing with detailed information about chap1 and .profile.
ls -l chap1 .profile
- To display detailed information about a directory, type the following:
This displays a long listing for the directories . and manual, and for the file manual/chap1. Without the -d flag, this would list the files in the . and manual directories instead of the detailed information about the directories themselves.
ls -d -l . manual manual/chap1