Linux ls Command: List Files and Directories

The ls command in Linux displays a list of files and subdirectories in a directory. Using ls -la, you can see detailed information including hidden items, permissions, owner, size, and modification time.

List the current directory or a specified path

Running the following command in the terminal displays the items in the current working directory. The current location can be checked with pwd.

pwd
ls

To view the contents of another directory, specify the path. You can use both absolute paths and relative paths based on the current location.

ls /var

By default, hidden items whose names start with a dot are excluded from the list. The name and display order depend on the system and locale settings. ls is a command to read the list and does not delete or modify files.

View hidden items and detailed information

-a includes names that start with a dot, and -l displays in detailed format. You can use both options together.

ls -a
ls -l
ls -la

ls -a also shows . for the current directory and .. for the parent directory. GNU ls -A displays hidden items but omits these two entries. To display sizes in human-readable units, add -h to the detailed list.

ls -lah

For example, it can be displayed like 1K, 2M. The output varies depending on the target files and system settings.

Reading a detailed list

The following is an example showing a regular file. Date format, names, and numbers vary depending on the actual environment.

-rw-r--r-- 1 user group 12 Jan 15 10:30 notes.txt
  • -rw-r--r--: The first character - is a regular file, and the rest indicate the permissions for the owner, group, and others. If it starts with d, it is a directory; if it starts with l, it is a symbolic link.
  • 1 user group: Number of hard links, owner, and owning group.
  • 12: File size (in bytes) in the basic detailed list. Using -h will change it to an easily readable unit.
  • Jan 15 10:30 notes.txt: By default, the last modification time and name. The output format depends on the locale and options.

The size entry of a directory is not the sum of the file sizes inside it. To check the disk usage of the entire directory, use a tool like du. The link target is usually shown in name -> target form in the detailed list.

Checking sorting and the directory itself

-t sorts by default from the newest modification time. Using -r together will reverse the order.

ls -lt
ls -ltr

To check the permissions and owner of the directory itself, not the list inside, use -d.

ls -ld /var

-R recursively lists subdirectories as well, so output can be large for large directory trees. Check GNU ls manual or man ls for detailed meanings of the options.

Narrowing the list with a name pattern

In Bash, the asterisk * is a name pattern that matches zero or more characters. The following command finds items in /etc whose names end with .conf.

ls -d /etc/*.conf

The shell first expands names matching the pattern and then passes them to ls. Since -d was entered, it shows the directory name itself even when a matching item is a directory. In typical Bash settings, hidden items starting with a dot do not automatically match. If there are no matching names, the result may vary depending on shell options, such as an error or the pattern being passed as is.

If the path contains spaces, quote the path like ls "my folder". Generally, if you quote even the pattern, the shell does not expand the asterisk. For details, see Bash file name expansion document.

Checking disk devices and errors

ls -l /dev/sd* can show a list that matches device files starting with the name sd. However, not all disks and partitions use this name. Other devices, such as NVMe, may be missing from the list, so it is more appropriate to use lsblk when examining block devices and layers.

lsblk

If the list does not appear or Permission denied is displayed, check the existence of the path and access permissions. If the output differs from what you expect, verify the shell alias as type ls and also review the option descriptions of the distribution and ls --help.