<span>ls</span> is one of the most commonly used commands in Linux, used to list files and subdirectories in a directory. It is an abbreviation for “list”.
๐งพ Basic Syntax
ls [options] [filename or directory name]
If no directory is specified, <span>ls</span> will list the contents of the current directory by default.
๐ Common Options
| Option | Meaning |
|---|---|
<span>-l</span> |
Display detailed information (permissions, number of links, owner, group, size, modification time, filename) |
<span>-a</span> |
Show all files, including hidden files that start with <span>.</span> |
<span>-h</span> |
Used with <span>-l</span> to display file sizes in a human-readable format (e.g., KB, MB) |
<span>-t</span> |
Sort by modification time (newest first) |
<span>-r</span> |
Reverse sort |
<span>-S</span> |
Sort by file size |
<span>-R</span> |
Recursively display contents of subdirectories |
<span>-i</span> |
Display the inode number of files |
<span>-d</span> |
When listing directories, only show the directory itself, not its contents (commonly used with wildcards) |
<span>--color</span> |
Color display for different types of files (most systems enable alias <span>ls --color=auto</span> by default) |
๐งช Example Usage
1. List files and directories in the current directory
ls
2. List detailed information
ls -l
Example output:
-rw-r--r-- 1 user user 4096 Aug 13 22:00 file.txt
drwxr-xr-x 2 user user 4096 Aug 13 22:00 dir/
3. Show hidden files
ls -a
4. Show detailed information and display sizes in a human-readable format
ls -lh
Example output:
-rw-r--r-- 1 user user 18K Aug 13 22:00 file.txt
drwxr-xr-x 2 user user 4.0K Aug 13 22:00 dir/
5. Sort by file size (largest first)
ls -lhS
6. Recursively display directory contents
ls -R
7. View attributes of the directory itself, not its contents
ls -ld /path/to/dir
๐ฏ Advanced Tips
Display specific types of files:
ls *.txt
List all <span>.txt</span> files.
List files and sort by time:
ls -lt
Display inode numbers (for debugging or viewing filesystem information):
ls -i
๐ Tips
<span>ll</span>is usually an alias for<span>ls -l</span>, depending on your shell configuration.<span>ls</span>typically does not show hidden files that start with<span>.</span>unless the<span>-a</span>option is added.<span>ls</span>does not recursively enter subdirectories unless the<span>-R</span>option is used.