The ls command is used to list files and directories in Linux. It provides various options to display additional information, sort output, and customize listings.
ls [OPTIONS] [FILE...]OPTIONS: Flags to modify the command's behavior.FILE: Specific files or directories to list. Defaults to the current directory if omitted.
| Option | Description |
|---|---|
-l |
Long listing format with detailed information. |
-a |
Show all files, including hidden files (starting with .). |
-h |
Human-readable sizes (e.g., KB, MB, GB) when used with -l. |
-R |
Recursively list subdirectories. |
-t |
Sort files by modification time, newest first. |
-S |
Sort files by size, largest first. |
-i |
Display inode numbers for files and directories. |
-d |
List directories themselves, not their contents. |
-F |
Append indicator symbols to entries (/ for directories, etc.). |
-1 |
Display one entry per line. |
-r |
Reverse the order of the sort. |
--color |
Colorize the output based on file type and permissions. |
-A |
List all files except . and ... |
-v |
Natural sort of version numbers. |
-G |
Suppress group information in long listing. |
You can combine options to refine output. For example, ls -lh shows a long listing with human-readable file sizes.
Here are some practical examples demonstrating how to use the ls command:
lsDescription: Lists files and directories in the current directory.
ls -lDescription: Lists files with detailed information such as permissions, owner, size, and modification date.
Example 3: List Hidden Files
ls -aDescription: Lists all files, including hidden files, in the current directory.
ls -lhDescription: Lists files with sizes displayed in a human-readable format (e.g., KB, MB).
ls -RDescription: Recursively lists all files and directories, including those in subdirectories.
ls -ltDescription: Lists files sorted by modification time, with the newest files first.
ls -lSDescription: Lists files sorted by size, with the largest files first.
ls -iDescription: Lists files with their inode numbers, useful for identifying files in the filesystem.
ls -d */Description: Lists only directories in the current directory.
ls --color=autoDescription: Displays files with color-coded output for better readability.
ls -lrDescription: Lists files in reverse order, e.g., oldest files first if used with -t.
ls -1Description: Displays one file per line, useful for piping into other commands.
- 0: Success
- 1: Minor issues (e.g., a file was not found)
- 2: Serious issues (e.g., command syntax error)
This cheat sheet provides a quick reference for the ls command, highlighting essential options and practical examples. Use it to efficiently navigate and manage files and directories in your Linux environment.