ls Command

Purpose

Displays the contents of a directory.

Syntax

To display contents of directory or name of file
ls [ -1 ] [ -A ] [ -C ] [ -E ] [ -F ] [-H  | -L ] [ -N ] [ -R ] [ -S ] [ -X] [ -a ] [ -b ] [ -c ] [ -d ] [ -e ] [ -f ] [ -g ] [ -i ] [-k] [ -l ] [ -m ] [ -n ] [ -o ] [ -p ] [ -q ] [ -r ] [ -s ] [ -t ] [ -u ] [ -U ] [ -x ] [ File ... ]
To display contents of directory
ls -f [ -C ] [ -d ] [ -i ] [ -m ] [ -s ] [ -X] [ -x ] [ -1 ] [ -U ] [ Directory ... ]

Description

The ls command writes to standard output the contents of each specified Directory parameter or the name of each specified File parameter, along with any other information you ask for with the flags. If you do not specify a File or Directory parameter, the ls command displays the contents of the current directory.

Specifying more than one of the options in the mutually exclusive pairs is not considered an error. The last option that is specified in each pair determines the output format.

By default, the ls command displays all information in alphabetic order by file name. The collating sequence is determined by the LANG or LC_COLLATE environment variable.

When the ls command displays the contents of a directory, it does not show entries for files whose names begin with a . (dot) unless you use the -a or -A flag. If the command is run by root, it uses the -A flag by default.

There are three main ways to format the output:

  • List one entry per line.
  • 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. The ls command displays single column output if file or directory names are too long.
  • 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 gets the current column value of the display. If the ls command cannot determine the number of character positions by either of these methods, it uses a default value of 80.

The mode that is displayed with the -U flag is the same as with the -l flag, except for the addition of an 11th character that is interpreted as follows:
Table 1. -U flag 11th character interpretation
Item Description
E Indicates that a file has extended attributes (EA) information. The EA of a file is displayed by using the getea command.
- Indicates that a file does not have extended attributes information.
e Indicates that a file is encrypted.

Encryption takes precedence over the presence of the Access Control Lists (ACLs) and other EAs.

The mode that is displayed with the -e and -l flags is interpreted as follows:

Table 2. The -e and -l flags mode interpretation
First character of mode Description
d The entry is a directory.
b The entry is a special block file.
c The entry is a special character file.
l The entry is a symbolic link, and either the -N flag was specified or the symbolic link did not point to an existing file.
p The entry is a first-in, first-out (FIFO) special file.
s The entry is a local socket.
- The entry is an ordinary file.

The next nine characters are divided into three sets of three characters each. The first set of three characters show the owner's permission. The next set of three characters show the permission of the other users in the group. The last set of three characters shows the permission of anyone else with access to the file. The three characters in each set indicate read, write, and execute permission of the file. Execute permission of a directory allows searching a directory for a specified file.

Permissions are indicated as follows:

Table 3. Permissions
Item Description
r Read
w Write (edit)
x Execute (search)
- Corresponding permission not granted

The group-execute permission character is s if the file has set-group-ID mode. The user-execute permission character is s if the file has set-user-ID mode. The last character of the mode (usually x or -) is T if the 01000 (octal) bit of the mode is set. The indications of set-ID and 01000 bit of the mode are capitalized (S and T) if the corresponding execute permission is not set. The mode t indicates that the sticky bit is on for the file or the directory.

The mode that is displayed with the -e flag is the same as with the -l flag, except for the addition of an 11th character that is interpreted as follows.

Table 4. -e flag 11th character interpretation
Item Description
+ Indicates that a file has extended security information. For example, the file might have extended ACL, TCB, or TP attributes in the mode.

The access control information (ACL) of a file is displayed by using the aclget command. The value of the TCB and TP attributes are displayed by using the chtcb command.

- Indicates that a file does not have extended security information.

When the capacity of the files in a directory is listed, the ls command displays a total count of blocks, including indirect blocks.

Flags

Table 5. Flags
Item Description
-A Lists all entries except . (dot) and .. (dot-dot).
-a Lists all entries in the directory, including the entries that begin with a . (dot).
-b Displays nonprintable characters in an octal (\nnn) notation.
-c Uses the time of last modification of the i-node for either sorting (when used with the -t flag) or for displaying (when used with the -l flag). This flag has no effect if it is not used with either the -t or -l flag, or both.
-C Sorts output vertically in a multicolumn format. This option is the default method when output is to a terminal.
-d Displays only the information for the directory named. Directories are treated like files, which is helpful when you use the -l flag to get the status of a directory.
-e Displays the mode (including security information), number of links, owner, group, capacity (in bytes), time of last modification, and name of each file. If the file is a special file, the capacity field contains the major and minor device numbers. If the file is a symbolic link, the path name of the linked-to file is printed preceded by a -> (minus and greater than) sign. The attributes of the symbolic link are displayed.
-E Lists extent attributes of a file that exists in Vxfs file systems, such as space reservation, fixed extent capacity, and extent allocation flag information. Specify the -l flag with this flag; otherwise, the ls command ignores the -E flag and completes the execution.
-f Lists the name in each slot for each directory that is specified in the Directory parameter. This flag turns off the -l, -t, -s, and -r flags, and turns on the -a flag. The order of the listing is the order in which entries appear in the directory.
-F Puts a / (slash) after each file name if the file is a directory, an * (asterisk) if the file can be run, an = (equal sign) if the file is a socket, a | (pipe) sign if the file is a FIFO, and an @ for a symbolic link. Symbolic links that are named as operands are not followed unless you have specified the -H or -L flag.
-g Displays the same information as the -l flag, except the -g flag suppresses display of the owner and symbolic link information.
-H If a symbolic link that refers a file of type directory is specified on the command line, the ls command evaluates the file information and file type to be those of the file that are referenced by the link, and not the link itself; however, the ls command writes the name of the link itself and not the file that is referenced by the link.
-i Displays the i-node number in the first column of the report for each file. If the file system has an internal snapshot, the .snapshot directory and all its contents do not have unique i-node numbers.
-k Sets the block capacity for -s option and the per-directory block count that is written for -l, -n, -g, and -o options to 1024 bytes.
-L Lists the file or directory contents that the link references. This option is the default action. Symbolic links are followed. If the -l option is used, the -N option becomes the default, and no symbolic links are followed. When the -l option is used, only the -L option can override the -N default.
-l (Lowercase L) Displays the mode, number of links, owner, group, capacity (in bytes), and time of last modification for each file. If the file is a special file, the capacity field contains the major and minor device numbers. If the time of last modification is greater than six months ago, the time field is shown in the format month date year whereas files modified within six months the time field is shown as month date year format.

If the file is a symbolic link, the path name of the linked-to file is printed preceded by a ->. The attributes of the symbolic link are displayed. The -n, -g, and -o flag overrides the -l flag.

Notes:
  1. A symbolically linked file is followed by an arrow and the contents of the symbolic link.
  2. The performance of the ls command when used with the -l option can be improved by using the mkpasswd command. This option is helpful when a directory contains files that are owned by different users, such as the /tmp directory.
-m Uses stream output format (a comma-separated series).
-n Displays the same information as the -l flag, except that the -n flag displays the user and the group IDs instead of the user and group names.
-N Does not follow symbolic links when you determine the status of a file.
Note: If both the -L and -N options are used, the last one dominates. Also, anytime that a symbolic link is given that includes a / (slash) as the final character, the link is automatically followed regardless of any options used.
-o Displays the same information as the -l flag, except the -o flag suppresses display of the group and symbolic link information.
-p Puts a slash after each file name if that file is a directory. This option is useful when you pipe the output of the ls command to the pr command, as follows.
ls  -p |  pr  -5  -t  -w80
-q Displays nonprintable characters in file names as a ? (question mark).
-r Reverses the order of the sort, giving the reverse alphabetic order or the oldest first.
-R Lists all subdirectories recursively.
-s Gives capacity in kilobytes (including indirect blocks) for each entry.
-S Sorts with the primary key as file capacity (in decreasing order) and the secondary key as file name in the collating sequence (in increasing order).
-t Sorts by time of last modification (latest first) instead of by name. For a symbolic link, the time that is used as the sort key is that of the symbolic link itself.
-U Displays similar information as the -l flag. Displays the mode (including security information, named extended attribute information and encryption information), number of links, owner, group, capacity (in bytes), time of last modification, and name of each file. If the file is a special file, the capacity field contains the major and minor device numbers. If the file is a symbolic link, the path name of the linked-to file is printed preceded by a -> (minus and greater than) sign. The attributes of the symbolic link are displayed.
-u Uses the time of the last access, instead of the time of the last modification, for either sorting (when used with the -t flag) or for displaying (when used with the -l flag). This flag has no effect if it is not used with either the -t or -l flag, or both.
-x Sorts output horizontally in a multi-column format.
-X Prints long usernames when used with other flags that display usernames. The higher limit is determined by the max_logname ODM attribute in the PdAt and CuAt object classes. If a username is greater than the max_logname attribute, it is truncated to the number of characters as specified by the max_logname attribute, less one character.
-1 Forces output into a one-entry-per-line format. This option is the default when the output is not directed to a terminal.
Notes:
  • If any of the -l, -n, -s, -g, or -o flag is specified, each file that is present in the directory is preceded by a status line that indicates the number of file system blocks that are occupied by files.
  • If the -k flag is not specified along with any of the -l, -n, -s, -g, or -o flag, the status line indicates the number of file system blocks that are occupied by files in units of 512 bytes.
  • If the -k flag is specified along with any of the -l, -n, -s, -g, or -o flag, the status line indicates the number of file system blocks that are occupied by files in units of 1024 bytes.
  • Also, if necessary, the number of file system blocks that are occupied by files in the directory is rounded off to the next integral number of units.
  • In the POSIX locale environment, the total %u\n is the output format that represents number of units in the directory.

Exit Status

This command returns the following exit values:

Table 6. Exit status
Item Description
0 All files were written successfully.
0 An error occurred.

Examples

  1. To list all files in the current directory, enter the following command:
    ls -a

    This command lists all files, including . (dot), .. (dot-dot), and other files with names that begin with a dot.

  2. To display detailed information, enter the following command:
    ls -l chap1 .profile

    This command displays a long listing with detailed information about chap1 and .profile.

  3. To display detailed information about a directory, enter the following command:
    ls -d -l . manual manual/chap1

    This command displays a long listing for the directories . and manual, and for the file manual/chap1. Without the -d flag, this command lists the files in the . and manual directories instead of the detailed information about the directories themselves.

  4. To list the files in order of modification time, enter the following command:
    ls -l -t

    This command displays a long listing of the files that were modified most recently, followed by the older files.

  5. To display detailed information with expanded user and group name, enter the following command:
    ls -lX .profile

    This command displays a long listing with detailed information about .profile.

  6. To display data about whether extended attributes are set for the files in the current directory, enter the following command:
    ls -U
  7. To display information about the number of files system blocks in units of 512 bytes for the files in the current directory, enter the following command:
    ls -l
    Example output:
    total 16
    -rw-r--r--    1 root     system           22 Feb 05 05:29 sample1
    -rw-r--r--    1 root     system           12 Feb 05 05:29 sample2
  8. To display information about the number of files system blocks in units of 1024 bytes for the files in the current directory, enter the following command:
     ls -lk
    Example output:
    total 8
    -rw-r--r--    1 root     system           22 Feb 05 05:29 sample1
    -rw-r--r--    1 root     system           12 Feb 05 05:29 sample2

Files

Table 7. Files
Item Description
/usr/bin/ls Contains the ls command.
/etc/passwd Contains user IDs.
/etc/group Contains group IDs.
/usr/share/lib/terminfo/* Contains terminal information.