📂 LS Command Guide

Master File Listing in Linux - 20 Comprehensive Examples

What is the LS Command?

ls (list) is one of the most fundamental and frequently used commands in Linux and Unix-based systems. It lists directory contents, displays file information, and provides various formatting options. Understanding ls and its numerous flags is essential for efficient file system navigation and management.

Basic Listing Commands
Example 1

Simple Directory Listing

ls
Desktop Documents Downloads Music Pictures Videos
Purpose: List files and directories in the current directory.
Details: Shows only names of visible files (non-hidden) in alphabetical order.
Example 2

Long Format Listing

ls -l
drwxr-xr-x 2 user user 4096 Oct 17 10:30 Documents -rw-r--r-- 1 user user 2048 Oct 17 09:15 file.txt drwxr-xr-x 5 user user 4096 Oct 16 14:20 Pictures
Purpose: Display detailed file information.
Columns show: permissions, links, owner, group, size, date, name
Reading Permissions: First character (d=directory, -=file, l=link), then 9 chars for owner/group/others (r=read, w=write, x=execute)
Example 3

Show Hidden Files

ls -a
. .. .bashrc .config Desktop Documents .hidden_file
Purpose: List all files including hidden ones.
Flag: -a shows files starting with dot (.)
Note: . represents current directory, .. represents parent directory
Example 4

Human-Readable File Sizes

ls -lh
drwxr-xr-x 2 user user 4.0K Oct 17 10:30 Documents -rw-r--r-- 1 user user 2.0M Oct 17 09:15 large_file.zip -rw-r--r-- 1 user user 150K Oct 17 11:00 image.jpg
Purpose: Display file sizes in human-readable format.
Flag: -h converts bytes to KB, MB, GB, etc.
Combination: -l for long format + -h for readable sizes
Sorting and Ordering
Example 5

Sort by Modification Time

ls -lt
-rw-r--r-- 1 user user 2048 Oct 17 11:30 newest.txt -rw-r--r-- 1 user user 1024 Oct 17 10:15 recent.log -rw-r--r-- 1 user user 3072 Oct 16 09:00 older.dat
Purpose: Sort files by modification time (newest first).
Flag: -t sorts by time instead of alphabetically.
Use Case: Find most recently modified files quickly
Example 6

Reverse Sort Order

ls -ltr
-rw-r--r-- 1 user user 3072 Oct 16 09:00 older.dat -rw-r--r-- 1 user user 1024 Oct 17 10:15 recent.log -rw-r--r-- 1 user user 2048 Oct 17 11:30 newest.txt
Purpose: Reverse the sort order (oldest first).
Flag: -r reverses any sort order.
Combined: -ltr = long format, time-sorted, reversed
Pro Tip: Great for finding oldest files or seeing chronological order
Example 7

Sort by File Size

ls -lhS
-rw-r--r-- 1 user user 50M Oct 17 10:00 huge.zip -rw-r--r-- 1 user user 5.2M Oct 17 11:00 large.mp4 -rw-r--r-- 1 user user 128K Oct 17 09:00 small.txt
Purpose: Sort by file size (largest first).
Flag: -S (capital S) sorts by size.
Use Case: Find large files consuming disk space
Example 8

Sort by Extension

ls -lX
-rw-r--r-- 1 user user 1024 Oct 17 10:00 script.sh -rw-r--r-- 1 user user 2048 Oct 17 11:00 document.txt -rw-r--r-- 1 user user 3072 Oct 17 09:00 archive.zip
Purpose: Group files by extension type.
Flag: -X (capital X) sorts by extension.
Benefit: Organize view by file types
Advanced Filtering and Display
Example 9

Recursive Directory Listing

ls -R
.: Documents Pictures ./Documents: report.pdf notes.txt ./Pictures: vacation.jpg family.png
Purpose: List all files in subdirectories recursively.
Flag: -R (capital R) shows entire directory tree.
Warning: Can produce very long output in large directories
Example 10

List Only Directories

ls -d */
Desktop/ Documents/ Downloads/ Music/ Pictures/
Purpose: Show only directories, not files.
Flag: -d with pattern */ lists directories.
Alternative: ls -l | grep ^d
Example 11

Show Inode Numbers

ls -i
1234567 Documents 1234568 file.txt 1234569 Pictures
Purpose: Display inode numbers for each file.
Flag: -i shows the index number (inode).
Use Case: Identify hard links, troubleshoot filesystem issues
What's an Inode? Unique identifier for each file/directory on the filesystem
Example 12

Color-Coded Output

ls --color=auto
Purpose: Display files with colors based on type.
Flag: --color=auto enables color coding.
Colors:
  • Blue = Directories
  • Green = Executable files
  • Red = Archives/compressed files
  • Cyan = Symbolic links
Note: Usually aliased by default in most distributions
Special Display Formats
Example 13

One File Per Line

ls -1
Desktop Documents Downloads Music Pictures
Purpose: Force one entry per line (useful for scripting).
Flag: -1 (number one) shows single column.
Use Case: Pipe to other commands, count files with ls -1 | wc -l
Example 14

Comma-Separated List

ls -m
Desktop, Documents, Downloads, Music, Pictures, Videos
Purpose: Display entries separated by commas.
Flag: -m creates comma-delimited list.
Use Case: Compact display, easy to read long lists
Example 15

Show File Type Indicators

ls -F
Desktop/ Documents/ script.sh* link@ file.txt
Purpose: Append indicators to show file types.
Flag: -F adds symbols after names.
Indicators:
  • / = Directory
  • * = Executable
  • @ = Symbolic link
  • | = FIFO/pipe
  • = = Socket
Example 16

Display with Quotes

ls -Q
"Documents" "file with spaces.txt" "Pictures"
Purpose: Enclose entry names in double quotes.
Flag: -Q adds quotes around names.
Use Case: Safer for scripting with files containing spaces
Time and Date Options
Example 17

Show Access Time

ls -lu
-rw-r--r-- 1 user user 2048 Oct 17 14:30 accessed.txt -rw-r--r-- 1 user user 1024 Oct 16 10:15 old.txt
Purpose: Show last access time instead of modification time.
Flag: -u uses access time with -l.
Use Case: Find files that haven't been read recently
Example 18

Show Full Timestamp

ls -l --full-time
-rw-r--r-- 1 user user 2048 2025-10-17 10:30:45.123456789 +0000 file.txt
Purpose: Display complete timestamp with nanoseconds.
Flag: --full-time shows detailed time info.
Use Case: Precise timestamp comparison, forensics
Example 19

Sort by Change Time

ls -lc
Purpose: Sort by status change time (ctime).
Flag: -c uses status change time.
Difference:
  • mtime (-l): Content modification
  • atime (-u): Last accessed
  • ctime (-c): Metadata/permission changes
Example 20

Advanced Combined Example

ls -lahtR --color=auto
Purpose: Ultimate comprehensive listing.
Combined Flags:
  • -l: Long format
  • -a: Show hidden files
  • -h: Human-readable sizes
  • -t: Sort by time
  • -R: Recursive
  • --color: Colored output
Pro Tip: Create an alias: alias ll='ls -laht --color=auto'
Practical Real-World Examples
Bonus 1

Find Largest Files

ls -lhS | head -10
Purpose: List the 10 largest files in current directory.
Combination: Sort by size, pipe to head command
Bonus 2

Count Files and Directories

ls -l | wc -l
Purpose: Count total entries in directory.
Note: Subtract 1 from result (total line)
Bonus 3

List with Wildcards

ls -lh *.txt
Purpose: List only files matching pattern.
Examples: *.pdf, file*, ?.txt
Bonus 4

Show Total Disk Usage

ls -lh --block-size=M
Purpose: Display sizes in specific units.
Options: K (KB), M (MB), G (GB), T (TB)