Linux ls Command: List Files and Directories
ls is a command in Linux used to check the list of files and directories. In addition to the basic list, you can choose options to display hidden files, permissions and owners, file size, modification time, and sorting order.
For everyday checking, ls -lah is convenient, but when safely handling filenames in scripts, it is better to use commands like find rather than the human-readable output of ls.
What is the ls command?
ls is short for list, and it lists information about specified files or the contents of a directory. In GNU/Linux distributions, the ls included in GNU Coreutils is generally used.
When run without arguments, it targets the current directory and, by default, does not display hidden items whose names start with a dot (.). If you give a directory name as an argument, it shows the contents inside that directory, not the directory itself.
Basic Syntax
ls [options]... [files or directories]...
To check the items in the current directory, execute as follows.
ls
You can also specify a particular directory or file.
ls /var/log ls /etc/hosts ls /etc /usr/local
If you specify multiple targets, each is processed in the order written on the command line. By default, directory contents are displayed in filename order according to the current locale's sorting rules.
Display hidden files
Show all items with -a
-a or --all includes hidden files and directories that start with a dot. It also shows . representing the current directory and .. representing the parent directory.
ls -a
Exclude. and.. with -A
-A or --almost-all shows hidden items but excludes . and ... The output is more concise when checking only actual configuration files.
ls -A
Display detailed information with ls -l
-l displays in long format the file type and permissions, number of hard links, owner, group, size, modification time, and name.
ls -l
A typical single line of output consists of the following order.
| Item | Meaning |
|---|---|
| Type and Permissions | The single character for the file type, and the read, write, and execute permissions for the owner, group, and others. |
| Number of Hard Links | The number of directory entries pointing to the file's inode. In directories, it has a slightly different meaning. |
| Owner | The name or user ID of the file's owning account. |
| Group | The name or group ID of the group the file belongs to. |
| Size | By default, the file size is in bytes. |
| Time | The default is the modification time (mtime) when the file data was last changed. |
| Name | This is the file name. For symbolic links, the target they point to may also be displayed. |
How to read permission strings
The first character of a string that looks like -rw-r--r-- indicates the file type, and the following nine characters are the permissions for the owner, group, and others, in three-character groups. r means read, w means write, and x means execute permission.
| Location | Example | Description |
|---|---|---|
| First character | -, d, l |
It is the type such as a regular file, directory, or symbolic link. |
| 2nd to 4th | rw- |
The owner's permissions. |
| 5th to 7th | r-- |
The group's permissions. |
| 8th to 10th | r-- |
This is the permission for other users. |
If a + or another symbol is attached after the permission string, it may indicate that there is additional security information such as an Access Control List (ACL). For detailed permission changes, use chmod, and for changing ownership, use chown.
Display as numeric ID with -n
To check the numeric ID instead of the username and group name, use -n. This is useful when account name lookup is slow or when comparing IDs with another system.
ls -ln
Display file size in a human-readable way
Display in human-readable units with -h
-h displays sizes in a long format using readable units like KiB, MiB, GiB. Use it together with -l or -s which have a size column.
ls -lh ls -lah
ls -lah is frequently used in interactive work because it shows hidden items in long format.
Display allocated block size with -s
-s shows the number of disk blocks allocated to each file. The logical size of the file and the actual disk allocation may differ due to sparse files, compression, and file system characteristics.
ls -sh ls -lsh
To sum up the total space occupied by an entire directory, du is more suitable than ls.
Changing the sort order
By default, ls sorts file names according to the current locale character sorting rules. It can be changed to sort by time, size, extension, or version depending on the purpose.
Sort by modification time with -t
ls -lt ls -lat
-t displays items starting with the most recently modified by default. This is useful for finding logs or recently created result files.
Sort by size with -S
ls -lhS
-S sorts by file size in descending order. The size displayed in directory listings is not the sum of the contents, so use du to compare disk usage.
Sorting in reverse and by version with -r, -v
ls -ltr ls -1v
-r reverses the current sort order. Therefore, ls -ltr shows entries from oldest to newest. -v performs a version sort that considers numeric parts, so file2 comes before file10.
Distinguishing directories and listing the directory itself
Appending type indicators with -F
-F or --classify appends type indicators such as / to directories, * to executable files, and @ to symbolic links.
ls -F
These indicators are not part of the actual file names. When determining the type in scripts, do not strip these characters; use file test operators or find instead.
Viewing the directory itself instead of its contents with -d
If you specify a directory name, it will show the contents by default. To see the permissions and times of the directory entry itself, use -d together.
ls -ld /var/log ls -d */
ls -d */ is convenient for checking the names of regular subdirectories in the current location, but it does not include hidden directories according to the shell's glob expansion rules.
Gathering directories first
ls --group-directories-first
In GNU ls, directories can be grouped before files. This option is a GNU extension, so you should check whether it is supported on other Unix-like systems.
Viewing subdirectories recursively
ls -R /etc
-R or --recursive follows subdirectories and continues to display their contents. If the tree is large, the output can be very long and many permission errors may occur.
To find files by a specific name or condition, it is more efficient to use find rather than scanning the entire list visually.
find /etc -type f -name '*.conf'
Choosing time types and display formats
Access Time and Status Change Time
ls -l by default shows the modification time (mtime). -u displays the access time (atime), and -c displays the metadata status change time (ctime) or uses it as a sorting criterion.
ls -lu ls -lc ls -ltu ls -ltc
Ctime is not the "creation time" of the file, but the last time the inode status, such as permissions, owner, or number of links, was changed.
Using a Consistent Date Format
With GNU ls, you can use --time-style to select a long date format.
ls -l --time-style=long-iso ls -l --time-style=full-iso ls -l --time-style='+%Y-%m-%d %H:%M:%S'
The output can be affected by the locale and TZ environment variable. For reports intended for human comparison, it is recommended to set the time zone and display format together.
Color and File Name Display Method
Using Color
ls --color=auto
--color=auto uses colors based on file type and extension when the output is a terminal. Many distributions apply this option by default as an alias for ls.
type ls alias ls
Colors are influenced by the LS_COLORS environment variable and dircolors settings. When saving to a file or passing to other commands, you can use --color=never to avoid including unnecessary control characters.
File names with spaces and special characters
ls may display file names containing spaces or control characters in quoted or escaped form depending on the terminal and options. You should not assume that the displayed string always matches the actual file name.
ls -Q ls --quoting-style=shell-escape
-Q surrounds file names with double quotes. --quoting-style is a GNU extension, useful for humans to identify special characters but not a replacement for a safe automatic processing format.
Frequently used combinations
| Command | Purpose |
|---|---|
ls -la |
Displays detailed information including hidden items. |
ls -lah |
Displays hidden items and detailed information in human-readable size units. |
ls -lhS |
Display detailed information starting with large files. |
ls -ltr |
Display in long format from oldest modification time. |
ls -ld */ |
Display information of subdirectories at the current location. |
ls -1 |
Display items one per line. |
ls -1v |
Sort line by line considering version numbers. |
ls -l --time-style=long-iso |
Display in a format that makes comparing dates and times easy. |
Summary of main options
| Option | Description |
|---|---|
-a, --all |
Display hidden items including . and ... |
-A, --almost-all |
Displays hidden items except for . and ... |
-l |
Displays in long format including permissions, owner, size, and time. |
-h, --human-readable |
Displays size in human-readable units. |
-d, --directory |
Displays the directory entry itself instead of the contents of the directory. |
-R, --recursive |
Displays recursively including subdirectories. |
-t |
Sorts by most recent modification time by default. |
-S |
Sorts by file size in descending order. |
-r, --reverse |
Reverses the current sort order. |
-v |
Sort in version order considering the numeric part of the filename. |
-F, --classify |
Append a character indicating the file type after the name. |
-1 |
Display only one item per line. |
Reasons not to parse the output of ls in scripts
Linux filenames can contain spaces, tabs, and newlines. ls changes columns, colors, and quoting to make it readable for humans in the terminal, so scripts that split by lines or spaces may handle files incorrectly.
When traversing files, use tools that support NUL character as a delimiter.
find . -maxdepth 1 -type f -print0 | while IFS= read -r -d '' file; do printf '%s\n' "$file" done
GNU ls also has the --zero option, but when configuring search criteria and subsequent processing, find -print0 is usually clearer. If you need exact metadata for a single file, you can use stat.
Common issues
Permission denied is displayed.
You may not have the necessary permissions for the directory you are trying to read the contents of. Check the execute permission of the path components and the read permission of the target directory with ls -ld path or namei -l path. Rather than habitually using sudo to bypass permissions, it is better to first determine if the access is really needed.
The sort order is different from expected.
The default name sorting is influenced by the LC_COLLATE locale. If reproducible byte order is required, you can apply LC_ALL=C to just that command.
LC_ALL=C ls
An "Argument list too long" error occurs.
In ls *, the asterisk is expanded by the shell into numerous file names before ls runs, not by ls itself. If it exceeds the operating system's argument size limit, the error occurs before the command starts. For a simple list, run ls alone, and for conditional searches or batch processing, use find with -exec or a NUL-separated pipeline.
FAQ
In ls -l, is the directory size the sum of internal files?
No. The size shown for directories in the long format is the size of the directory entry metadata itself, not the sum of internal file sizes. To check disk usage by the directory, use du -sh directory.
What is the difference between ls -a and ls -A?
-a shows all hidden items including . and ... -A shows hidden items but excludes these two special directory entries.
How can I see the most recently modified files first?
Using ls -lt displays items in long format starting with the most recently modified. To see from oldest to newest, add the reverse option and use ls -ltr.









