Introduction
Hello! In this article, we’ll learn about the ls command.
ls stands for “List”, and it’s a basic command used to display a list of files and directories. Along with cd, it’s one of the most frequently used commands when working with Linux.
What is the ls Command?
The ls command is an external command that displays information about files and directories contained in a specified directory (or the current directory). It can show not only file names but also detailed information such as permissions, owners, sizes, and modification times.
Basic Syntax
|
|
Main Options
| Option | Description |
|---|---|
-l |
Display detailed information (long format) |
-a |
Show hidden files (files starting with .) |
-A |
Show hidden files except . and .. |
-h |
Display file sizes in human-readable format (use with -l) |
-t |
Sort by modification time, newest first |
-r |
Reverse the sort order |
-R |
List subdirectories recursively |
-S |
Sort by file size, largest first |
-1 |
List one file per line |
--color |
Colorize the output (auto/always/never) |
Usage Examples
Example 1: Basic Usage
|
|
Result:
|
|
Files and directories in the current directory are displayed.
Example 2: Display Detailed Information
|
|
Result:
|
|
Column meanings:
- Column 1: Permissions (
d= directory,-= file) - Column 2: Number of hard links
- Column 3: Owner
- Column 4: Group
- Column 5: Size (bytes)
- Columns 6-8: Last modification time
- Column 9: File name
Example 3: Show Hidden Files
|
|
Result:
|
|
Hidden files starting with . are also displayed.
Example 4: Detailed Info + Hidden Files + Human-Readable Sizes
|
|
Result:
|
|
File sizes are displayed in readable format like 1.2K or 4.0K. Super convenient!
Example 5: Sort by Modification Time
|
|
Result:
|
|
Recently modified files appear at the top.
Example 6: Sort by Size
|
|
Result:
|
|
Largest files are shown first. Great for finding files that are taking up disk space!
Example 7: List Subdirectories Recursively
|
|
Result:
|
|
Contents of all subdirectories are also displayed.
Example 8: List Specific Directory
|
|
Result:
|
|
Shows the contents of the specified directory (here /etc).
Tips & Notes
- Default sort order: Files are listed in alphabetical order by default
- Color display: Most distributions color-code directories (blue), executables (green), etc.
- Combining options:
-l -a -hcan be written as-lah - Using wildcards: You can display only files matching a specific pattern like
ls *.txt1 2 3ls *.txt # Files ending with .txt ls test* # Files starting with test ls [abc]* # Files starting with a, b, or c - Using aliases: Setting
alias ll='ls -lah'lets you shorten frequently used options - Reading permissions: Understanding
drwxr-xr-x- 1st character: File type (
d=directory,-=regular file,l=symbolic link) - 2nd-4th characters: Owner permissions (
rwx=read, write, execute) - 5th-7th characters: Group permissions
- 8th-10th characters: Other users’ permissions
- 1st character: File type (
Practical Usage
Find Recently Modified Files
|
|
Shows the 5 most recently modified files.
Find Large Files
|
|
Displays the top 10 largest files.
Show Only Directories
|
|
Lists only directories in the current directory.
Show Only Hidden Files
|
|
Displays only hidden files starting with ..
Show Inode Numbers
|
|
Also displays inode numbers for each file. Useful for checking hard links!
Summary
In this article, we learned about the ls command.
Key Points:
lsis a basic command for listing files and directories-lfor detailed info,-afor hidden files,-hfor readable sizes-tfor time-based sorting,-Sfor size-based sorting- Options can be combined (like
-lah) - Wildcards let you display specific files only
The ls command is an essential tool for exploring the file system. Try different options to find the usage style that works best for you!
Keep learning Linux commands!