ls
| Since: | All Linux | |
|---|---|---|
| macOS(2001 Cheetah) | ||
| Bash 1.0(1989) |
The ls command displays the contents of a directory — a list of files and subdirectories. It is one of the most commonly used Bash commands, and you can combine options to view detailed information such as file sizes and permissions.
Syntax
ls [options] [directory or file]
Options
| Option | Description |
|---|---|
| -l | Displays detailed information (permissions, owner, size, and modification date) one entry per line. |
| -a | Shows all files, including hidden files that start with a dot (.). |
| -h | Displays file sizes in human-readable units (K, M, G). Use together with -l. |
| -R | Recursively lists the contents of subdirectories. |
| -t | Sorts entries by modification time, newest first. |
| -r | Reverses the sort order. Combined with -t, this shows oldest entries first. |
| -1 | Lists one file per line. |
Sample Code
The following examples use this directory structure.
Lists the contents of the current directory.
ls Documents README.md script.sh
Displays detailed information for each entry.
ls -l total 48 drwxr-xr-x 3 user staff 96 Mar 5 10:00 Documents -rw-r--r-- 1 user staff 1200 Mar 5 09:45 README.md -rwxr-xr-x 1 user staff 2800 Mar 4 18:30 script.sh
Shows detailed information including hidden files.
ls -la total 56 drwxr-xr-x 5 user staff 160 Mar 5 10:00 . drwxr-xr-x 8 user staff 256 Mar 5 09:00 .. -rw-r--r-- 1 user staff 50 Mar 4 12:00 .hidden drwxr-xr-x 3 user staff 96 Mar 5 10:00 Documents -rw-r--r-- 1 user staff 1200 Mar 5 09:45 README.md -rwxr-xr-x 1 user staff 2800 Mar 4 18:30 script.sh
Displays detailed information with human-readable file sizes.
ls -lh total 48K drwxr-xr-x 3 user staff 96B Mar 5 10:00 Documents -rw-r--r-- 1 user staff 1.2K Mar 5 09:45 README.md -rwxr-xr-x 1 user staff 2.8K Mar 4 18:30 script.sh
Sorts entries by modification time, newest first.
ls -lt total 48 drwxr-xr-x 3 user staff 96 Mar 5 10:00 Documents -rw-r--r-- 1 user staff 1200 Mar 5 09:45 README.md -rwxr-xr-x 1 user staff 2800 Mar 4 18:30 script.sh
Recursively lists subdirectory contents as well.
ls -R .: Documents README.md script.sh ./Documents: report.txt
Lists only files with a specific extension (using a wildcard).
ls *.sh script.sh
Common Mistakes
Common Mistake 1: Parsing ls output in scripts breaks on unusual filenames
The output of ls is for human display. Filenames with spaces, newlines, or special characters can break scripts that parse ls output.
for f in $(ls); do
echo "$f"
done
(filenames with spaces are split into multiple words)
Use glob patterns or find instead.
for f in *; do echo "$f"; done
Common Mistake 2: Dot files are hidden without -a
Files and directories starting with . are hidden from the default ls output.
ls Documents README.md script.sh ls -a . .. .bashrc .gitignore Documents README.md script.sh
Use -a to include hidden files in the listing.
Notes
The string at the beginning of each ls -l output line (e.g., drwxr-xr-x) represents the file permissions. A leading d means it is a directory; a leading - means it is a regular file. For information on changing permissions, see chmod / chown / chgrp.
For more detailed file information such as inode numbers and timestamps, use stat / file / du / df.
If you find any errors or copyright issues, please contact us.