Linux Terminal Command: ls (List Directory Contents)
The ls command is one of the very first commands you learn when entering the Linux terminal. It lists directory contents, displaying files, folders, symlinks, and sockets.
While typing ls seems simple, under the hood lies a powerful directory inspector capable of custom sorting, displaying file permissions, and revealing hidden system files.
Concept & Explanation
When you execute ls, it queries the operating system’s filesystem directory index and returns the names of the files located within that directory.
By default, the output is sorted alphabetically and formatted into columns. However, by appending flags, you can instruct ls to print detailed file metadata (permissions, owner, size, timestamps) which are read from the file’s inode (index node).
Anatomy of ls -l Output
When you run ls -l (long format), each line displays detailed metadata about a single file. Here is how to read it:
drwxr-xr-x 5 jehb staff 160B Jul 2 21:00 projects/
┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ │ │ └─ File Name
│ │ │ │ │ │ └─────────────── Modification Time
│ │ │ │ │ └───────────────────── File Size
│ │ │ │ └──────────────────────────── Group Owner
│ │ │ └────────────────────────────────── User Owner
│ │ └───────────────────────────────────── Number of Hard Links
│ └──────────────────────────────────────── File Permissions (rwx)
└───────────────────────────────────────────────── File Type (d = directory)
1. File Type Indicator (The first character)
-: Regular filed: Directoryl: Symbolic link (points to another file)c: Character special file (unbuffered hardware access)b: Block special file (buffered hardware access, e.g., hard drives)s: Local socketp: Named pipe (FIFO)
2. Permissions (Next 9 characters)
Divided into three blocks of three: User (Owner), Group, and Others.
r: Read permissionw: Write permissionx: Execute permission (for scripts or folders)-: Access denied
For example, rwxr-xr-x means the owner has read, write, and execute permissions, while group members and others can only read and execute.
1. Interactive Examples (Everyday Commands)
A. View Hidden Files (Dotfiles)
Files starting with a dot (like .bashrc or .git) are hidden by default to keep directories clean. To show them:
ls -a
B. Sorting by File Size
To find the largest files in a directory immediately:
ls -lS
- Add
-hto make the sizes human-readable (e.g.,4.2Ginstead of4509715622bytes):
ls -lhS
C. Sorting by Modification Date
To see the most recently modified files at the top of the list:
ls -lt
- To reverse the order (showing newest files at the bottom so they are visible right above your prompt):
ls -ltr
2. Power-User Examples (Advanced Techniques)
A. Recursive Directory Tree Listing
To list all files in the current folder, all files in subfolders, and so on:
ls -R
B. Appending Format Indicators
To quickly differentiate files, directories, and symlinks without long listing, use the -F flag:
ls -F
- Directories get a trailing
/ - Executable files get a trailing
* - Symbolic links get a trailing
@
C. Customizing Colors (LS_COLORS)
The terminal colorizes outputs based on the LS_COLORS environment variable. You can verify your settings using:
echo $LS_COLORS
To enable color-coding automatically, make sure your alias is defined with:
alias ls='ls --color=auto'
⚙️ Warning & Common Pitfalls
[!CAUTION] Do not parse the output of
lsin shell scripts.A common beginner mistake is using
lsin loops to process files:# BAD PATTERN - DO NOT USE for file in $(ls *.txt); do mv "$file" "${file%.txt}.bak" doneIf any file contains spaces or newlines (e.g.
My Report.txt), the shell will split the file name into two separate arguments (MyandReport.txt), causing your script to fail or delete the wrong files.The Safe Alternative: Use shell glob expansion:
# GOOD PATTERN - Safe for spaces and special characters for file in *.txt; do [ -e "$file" ] || continue # Handle empty matches mv "$file" "${file%.txt}.bak" done
🔗 Related Commands
Here are some related posts on cli_tty1 you might want to check out: