What is ls
?
The ls
command stands for “list”. It is used in Linux/Unix to display the contents of a directory.
Basic Syntax
-
options
: Optional flags to modify the output -
path
: The directory to list (default is the current directory)
Step-by-Step Usage
Step 1: List Files in Current Directory
This shows files and directories in the current directory (excluding hidden ones).
Step 2: Show Hidden Files
Hidden files start with a dot (.
), like .env
or .git
.
Output includes files like .bashrc
, .gitignore
.
Step 3: Long Listing Format (Detailed Info)
Shows:
-
File type and permissions
-
Number of links
-
Owner
-
Group
-
Size
-
Date/time
-
Filename
Example:
Step 4: Combine Options (e.g., Long + Hidden)
Shows all files with detailed info.
Step 5: Human-Readable Sizes
File sizes are shown in KB/MB/GB instead of bytes.
Step 6: Sort by Time (Newest First)
Lists files sorted by modification time (latest first).
Step 7: Reverse Order
Lists in reverse alphabetical or time order (if combined with -t
).
Step 8: List Files in a Specific Directory
List contents of the /etc
directory.
Step 9: Recursively List All Files and Sub-directories
Shows the entire directory tree starting from the current directory.
Step 10: Colored Output
Most modern distros support ls --color
or just color by default.
Colors differentiate file types (directories, links, executables).
Common ls
Options Summary
Option | Description |
---|---|
-a | Include hidden files |
-l | Long listing format |
-h | Human-readable sizes (use with -l ) |
-R | Recursive list |
-t | Sort by time (newest first) |
-r | Reverse sort order |
-S | Sort by file size |
-d | List directories themselves, not contents |
Examples
1. List files in /var/log
sorted by size:
2. Show hidden files with details in home:
3. List only directories:
Pro Tips
-
Combine options freely like
ls -lhtr
-
Use
alias ll='ls -lh'
for quicker long-listing
Practice Challenge
Try running:
To explore how /etc
is structured recursively with all files and directories, including hidden ones.