Bash List (ls)
Viewing files and directories in your system
📂 What is the ls Command?
The ls command lists files and directories in your current location. It's one of the most frequently used commands, helping you see what's available in any folder quickly and efficiently.
# Basic ls command
ls
Output:
Documents Downloads Pictures Music Videos file.txt
Common ls Options
ls -l
Long format with details
ls -l
ls -a
Show all files including hidden
ls -a
ls -h
Human-readable file sizes
ls -lh
ls -R
List recursively (subdirectories)
ls -R
🔹 Basic ls Usage
The simplest ls form displays files and directories in compact format without additional details. It shows only visible items by default, hiding system files starting with dots. This basic view provides quick directory content checks and is ideal for routine file navigation. The command's simplicity makes it one of the most frequently used tools in terminal environments.
# List files in current directory
ls
# List files in specific directory
ls /home/user/Documents
# List files in parent directory
ls ..
# List files in home directory
ls ~
Output:
file1.txt file2.txt folder1 folder2 report.pdf notes.txt project Desktop Documents Downloads Desktop Documents Downloads Pictures Music
🔹 Long Format Listing (ls -l)
The -l option provides detailed file information including permissions, ownership, size, and modification dates. Each line represents one file with columns showing different attributes in structured layout. This format is essential for understanding file properties, managing permissions, and troubleshooting system issues. It's indispensable for system administrators and power users needing comprehensive file metadata.
# Long format listing
ls -l
# Long format with human-readable sizes
ls -lh
# Long format for specific file
ls -l file.txt
Output:
total 24 drwxr-xr-x 2 user user 4096 Jan 15 10:30 Documents -rw-r--r-- 1 user user 125 Jan 15 10:30 file.txt drwxr-xr-x 2 user user 4.0K Jan 15 10:30 Downloads -rw-r--r-- 1 user user 2.5M Jan 15 10:30 video.mp4
🔹 Showing Hidden Files (ls -a)
The -a option reveals hidden files (starting with dots) along with regular files. These hidden items typically include configuration files and system files crucial for application settings and environment configurations. Displaying hidden files is essential when troubleshooting system issues, modifying application behavior, or performing comprehensive system audits and maintenance.
# Show all files including hidden
ls -a
# Show all files in long format
ls -la
# Show all files except . and ..
ls -A
Output:
. .. .bashrc .config Documents Downloads .hidden_file total 32 drwxr-xr-x 5 user user 4096 Jan 15 10:30 . drwxr-xr-x 3 root root 4096 Jan 10 09:00 .. -rw-r--r-- 1 user user 220 Jan 10 09:00 .bashrc drwxr-xr-x 2 user user 4096 Jan 15 10:30 Documents
🔹 Sorting and Filtering
The ls command offers various sorting options to organize output by time, size, or extension. Options like -t (time sort), -S (size sort), and -r (reverse order) help quickly locate recently modified files, largest files, or organize items alphabetically. These sorting capabilities enhance file management efficiency and simplify locating specific files in crowded directories.
# Sort by modification time (newest first)
ls -lt
# Sort by size (largest first)
ls -lS
# Reverse sort order
ls -lr
# Sort by extension
ls -lX
# List only directories
ls -d */
Output:
-rw-r--r-- 1 user user 125 Jan 15 11:45 recent.txt -rw-r--r-- 1 user user 100 Jan 15 10:30 older.txt -rw-r--r-- 1 user user 5.2M Jan 15 10:30 large.zip -rw-r--r-- 1 user user 125 Jan 15 10:30 small.txt Documents/ Downloads/ Pictures/
🔹 Recursive Listing (ls -R)
The -R option displays contents of the current directory and all subdirectories recursively. This powerful feature shows complete directory tree structures at once, revealing nested folder contents without manual navigation. It's particularly useful for understanding project organization, inventorying directory contents, and locating files deep within complex directory hierarchies.
# List all files recursively
ls -R
# Recursive with long format
ls -lR
# Recursive for specific directory
ls -R /home/user/Documents
Output:
.: Documents Downloads file.txt ./Documents: report.pdf notes.txt projects ./Documents/projects: project1 project2 ./Downloads: image.png video.mp4