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 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 executed 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 displayed with the -U flag is the same as with the -l flag, except for the addition of an 11th character interpreted as follows:
Item Description
E Indicates a file has extended attributes (EA) information. The EA of a file is displayed by using the getea command.
- Indicates a file does not have extended attributes information.
e Indicates a file is encrypted.

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

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

If the first character is:

Item Description
d The entry is a directory.
b The entry is a block special file.
c The entry is a character special 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, respectively, 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:

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 (see the chmod command for the meaning of this mode). The indications of set-ID and 01000 bit of the mode are capitalized (S and T, respectively) 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 displayed with the -e flag is the same as with the -l flag, except for the addition of an 11th character interpreted as follows:

Item Description
+ Indicates a file has extended security information. For example, the file may 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 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.

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 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 using the -l flag to get the status of a directory.
-e Displays the mode (including security information), number of links, owner, group, size (in bytes), time of last modification, and name of each file. If the file is a special file, the size 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, 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 size, and extent allocation flag information. You must 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 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 executed, 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 referencing a file of type directory is specified on the command line, the ls command shall evaluate the file information and file type to be those of the file referenced by the link, and not the link itself; however, the ls command shall write the name of the link itself and not the file 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 size for -s option and the per-directory block count written for -l, -n, -g, and -o options to 1024 bytes.
-L Lists the file or directory contents that the link references. This 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 (Lower case L) Displays the mode, number of links, owner, group, size (in bytes), and time of last modification for each file. If the file is a special file, the size 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 where as files modified within six months the time field is shown as month date time 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 executing the mkpasswd command. This is helpful when a directory contains files 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 determining the status of a file.

Note: If both the -L and -N options are used, the last one will dominate. Also, any time a symbolic link is given that includes a / (slash) as the final character, the link will automatically be 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 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 reverse alphabetic or the oldest first, as appropriate.
-R Lists all subdirectories recursively.
-s Gives size in kilobytes (including indirect blocks) for each entry.
-S Sorts with the primary key being file size (in decreasing order) and the secondary key being 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 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, size (in bytes), time of last modification, and name of each file. If the file is a special file, the size 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, 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 user names when used with other flags that display user names. The upper limit is determined by the max_logname ODM attribute in the PdAt and CuAt object classes. If a user name is greater than the max_logname attribute, it will be truncated to the number of characters as specified by the max_logname attribute, less one character.
-1 Forces output into one-entry-per-line format. This 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 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 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 occupied by files in units of 1024 bytes.
  • Additionally, if necessary, the number of file system blocks 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 which represents number of units in the directory.

Exit Status

This command returns the following exit values:

Item Description
0 All files were written successfully.
>0 An error occurred.

Security

Attention RBAC users and Trusted AIX users: This command can perform privileged operations. Only privileged users can run privileged operations. For more information about authorizations and privileges, see Privileged Command Database in Security. For a list of privileges and the authorizations associated with this command, see the lssecattr command or the getcmdattr subcommand.

Examples

  1. To list all files in the current directory, type:
    ls -a
    This lists all files, including . (dot), .. (dot-dot), and other files with names beginning with a dot.
  2. To display detailed information, type:
    ls   -l chap1  .profile
    This displays a long listing with detailed information about chap1 and .profile.
  3. To display detailed information about a directory, type:
    ls   -d   -l  .  manual  manual/chap1
    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.
  4. To list the files in order of modification time, type:
    ls   -l -t
    This 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, type:
    ls  -lX .profile
    This 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, type:
    ls -U
    • For releases AIX® 5.3 and earlier:

      Example output:

        -rwSr-x---+   1 root     system           28 Apr 29 03:23 only_aixc
        -rwSr-x---E   1 root     system            4 Apr 29 03:23 only_aixc_ea
        -rw-r--r--E   1 root     system            4 Apr 29 03:23 only_ea
        ----------+   1 root     system          265 Apr 29 03:23 only_nfs4
        ----------E   1 root     system           64 Apr 29 03:23 only_nfs4_ea
        -rw-r--r---   1 root     system            4 Apr 29 03:23 only_regular
    • For releases AIX 6.1 and later:

      Example output:

        -rwSr-x---+    1 root     system           28 Apr 29 03:23 only_aixc
        -rwSr-x---E    1 root     system            4 Apr 29 03:23 only_aixc_ea
        -rw-r--r--E    1 root     system            4 Apr 29 03:23 only_ea
        ----------+    1 root     system          265 Apr 29 03:23 only_nfs4
        ----------E    1 root     system           64 Apr 29 03:23 only_nfs4_ea
        -rw-r--r---    1 root     system            4 Apr 29 03:23 only_regular
        -rwxrwxr-xe    2 root     system          256 May 25 16:27 encry_ex 
  7. To display information about the number of files system blocks in units of 512 bytes for the files in the current directory, type:
    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, type:
     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

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.