ls ‐ #files #navigation - five4nets/Linux-Knowledgebase GitHub Wiki
ls
Command
Tutorial: Using the Linux The ls
command in Linux is used to list files and directories in a specified location. It’s one of the most commonly used commands for navigating and managing files in a terminal. This tutorial covers the basics of ls
, its options, and practical examples.
Table of Contents
ls
Command?
What is the The ls
command (short for "list") displays the contents of a directory. By default, it lists files and directories in the current working directory. You can customize its output using various options to show details like file permissions, sizes, or hidden files.
Basic Syntax
ls [options] [directory]
options
: Flags that modify the command’s behavior (e.g.,-l
for long format).directory
: The path to the directory you want to list (optional; defaults to the current directory).
Common Options
Here are some frequently used ls
options:
Option | Description |
---|---|
-l |
Long format: Shows detailed information (permissions, owner, size, modification time). |
-a |
All: Lists all files, including hidden files (those starting with . ). |
-h |
Human-readable: Displays file sizes in a readable format (e.g., KB, MB). Use with -l . |
-t |
Sort by modification time (newest first). |
-r |
Reverse: Reverses the sort order. |
-R |
Recursive: Lists contents of directories recursively. |
--color |
Adds color to output to distinguish file types (often enabled by default). |
Examples
1. Basic Listing
List files and directories in the current directory:
ls
Output (example):
documents photos videos
2. Long Format Listing
Show detailed information about files:
ls -l
Output (example):
drwxr-xr-x 2 user user 4096 Oct 10 12:00 documents
-rw-r--r-- 1 user user 1024 Oct 9 15:30 file.txt
drwxr-xr-x 3 user user 4096 Oct 8 10:00 photos
Explanation:
drwxr-xr-x
: File permissions and type (d
for directory).user
: Owner and group.4096
: File size in bytes.Oct 10 12:00
: Last modification time.
3. Show Hidden Files
List all files, including hidden ones:
ls -a
Output (example):
. .. .bashrc documents photos videos
Explanation:
.
(current directory) and..
(parent directory) are always present..bashrc
is a hidden configuration file.
4. Human-Readable File Sizes
Display file sizes in a readable format:
ls -lh
Output (example):
drwxr-xr-x 2 user user 4.0K Oct 10 12:00 documents
-rw-r--r-- 1 user user 1.0K Oct 9 15:30 file.txt
drwxr-xr-x 3 user user 4.0K Oct 8 10:00 photos
5. Sort by Modification Time
List files sorted by modification time (newest first):
ls -lt
Output (example):
drwxr-xr-x 2 user user 4096 Oct 10 12:00 documents
-rw-r--r-- 1 user user 1024 Oct 9 15:30 file.txt
drwxr-xr-x 3 user user 4096 Oct 8 10:00 photos
6. Reverse Sort Order
Reverse the default sort order (alphabetical):
ls -r
Output (example):
videos photos documents
7. Recursive Listing
List all files and subdirectories recursively:
ls -R
Output (example):
.:
documents photos videos
./documents:
report.pdf notes.txt
./photos:
vacation.jpg family.png
./videos:
movie.mp4
8. Combine Options
Combine multiple options for customized output:
ls -lah
Output (example):
drwxr-xr-x 5 user user 4.0K Oct 10 12:00 .
drwxr-xr-x 3 user user 4.0K Oct 8 09:00 ..
-rw-r--r-- 1 user user 220 Oct 1 10:00 .bashrc
drwxr-xr-x 2 user user 4.0K Oct 10 12:00 documents
drwxr-xr-x 3 user user 4.0K Oct 8 10:00 photos
drwxr-xr-x 2 user user 4.0K Oct 7 14:00 videos
Explanation: Shows all files (-a
), in long format (-l
), with human-readable sizes (-h
).
9. List Specific Directory
List contents of a specific directory:
ls /home/user/documents
Output (example):
report.pdf notes.txt