Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to list the directory content in Linux?
In the Linux operating system, there are two primary commands available to list directory contents: ls (list) and dir (directory). Both commands serve similar purposes but differ in their output formatting and default behavior.
The ls Command
ls (list) is the most commonly used command to list directory contents in Linux systems. By default, it displays the contents of the current directory with colored output for better readability. The ls command is also available in EFI (Extensible Firmware Interface) shell environments.
Syntax
$ ls [OPTION]... [FILE]...
Common ls Options
| Option | Description |
|---|---|
| -a, --all | Display all files including hidden files (starting with .) |
| -l | Display long listing format with permissions, owner, size, date |
| -h, --human-readable | Display file sizes in human-readable format (K, M, G) |
| -R, --recursive | List subdirectories recursively |
| -t | Sort by modification time, newest first |
| -S | Sort by file size, largest first |
| -d, --directory | List directories themselves, not their contents |
| --color | Colorize output (auto, always, never) |
Examples
Basic directory listing:
$ ls
Desktop Documents Downloads Pictures Videos
Long listing format with detailed information:
$ ls -l
total 20 drwxr-xr-x 2 user user 4096 Dec 26 10:30 Desktop drwxr-xr-x 3 user user 4096 Dec 26 11:15 Documents -rw-r--r-- 1 user user 156 Dec 26 09:45 readme.txt
List all files including hidden ones:
$ ls -la
The dir Command
dir (directory) is an alternative command for listing directory contents. Unlike ls, the dir command produces uncolored output by default and displays files in a single column format. This command is familiar to Windows users and provides similar functionality to the DOS dir command.
Syntax
$ dir [OPTION]... [FILE]...
Key Differences from ls
| Feature | ls Command | dir Command |
|---|---|---|
| Output Format | Multi-column, colored by default | Single column, no color by default |
| Default Sorting | Alphabetical, horizontal | Alphabetical, vertical |
| Color Output | Enabled by default | Disabled by default |
| Usage Popularity | Most commonly used in Linux | Less common, Windows-familiar |
Example
Basic directory listing with dir:
$ dir
Desktop Documents Downloads Pictures Videos readme.txt
Practical Usage Tips
Use
ls -lato see all files with detailed permissions and ownership informationUse
ls -lhto display file sizes in human-readable format (KB, MB, GB)Use
ls -tto sort files by modification time for finding recently changed filesUse
ls -Rto recursively list all subdirectories and their contents
Conclusion
Both ls and dir commands serve the same fundamental purpose of listing directory contents in Linux. The ls command is more commonly used due to its colored output and flexible formatting options, while dir provides a simpler, uncolored alternative familiar to Windows users.
