ls (List Directory Contents)
The ls command is used to list the contents of a directory. It allows you to see the files and subdirectories within your current or specified path.
1. Basic Usage
ls [options] [path]
- If no path is provided, it defaults to the current working directory.
2. Key Options
| Option | Description |
|---|---|
-l |
Long format. Displays detailed information like permissions, owners, and size. |
-a |
All. Shows hidden files (those starting with a dot .). |
-h |
Human-readable. Shows file sizes in K, M, G for easier reading. |
-t |
Time. Sorts the output by the last modification time. |
-r |
Reverse. Reverses the sorting order. |
-S |
Size. Sorts files by size, largest first. |
3. Practical Examples
① The "Must-Know" Combo (Detailed + Hidden + Human-readable)
ls -alh
② View Recently Modified Files First
ls -lt
③ View Files Sorted by Size (Largest first)
ls -lSh
4. [Tip] Decoding Permissions (with -l)
A string like -rw-r--r-- can be broken down:
* First Char: Type (-: Regular file, d: Directory)
* rwx: Read(r), Write(w), Execute(x) permissions.
* Groups: Owner permissions / Group permissions / Other users' permissions.