ls Command
Beginner File Management man(1)List directory contents and file information
👁 12 views
📅 Updated: Mar 15, 2026
SYNTAX
ls [OPTION]... [FILE]...
What Does ls Do?
The ls command is one of the most frequently used commands in Linux. It lists the contents of a directory, showing files, subdirectories, and their attributes. By default, ls shows the names of files and directories in the current working directory.
When used with various options, ls can display detailed information including file permissions, ownership, size, and modification dates. It supports color output to distinguish between file types, and can sort output by name, size, date, or extension.
The ls command is essential for navigating the filesystem and understanding what files exist in any given directory. System administrators use it constantly for verifying file deployments, checking permissions, and monitoring directory contents.
When used with various options, ls can display detailed information including file permissions, ownership, size, and modification dates. It supports color output to distinguish between file types, and can sort output by name, size, date, or extension.
The ls command is essential for navigating the filesystem and understanding what files exist in any given directory. System administrators use it constantly for verifying file deployments, checking permissions, and monitoring directory contents.
Options & Flags
| Option | Description | Example |
|---|---|---|
| -l | Use long listing format showing permissions, owner, size, and date | ls -l /home |
| -a | Show all files including hidden files (starting with .) | ls -a |
| -h | Human-readable file sizes (KB, MB, GB) | ls -lh |
| -R | List subdirectories recursively | ls -R /var/log |
| -t | Sort by modification time, newest first | ls -lt |
| -S | Sort by file size, largest first | ls -lS |
| -r | Reverse the order of sort | ls -ltr |
| -d | List directories themselves, not their contents | ls -d */ |
| -i | Print the inode number of each file | ls -li |
| --color | Colorize the output (auto, always, never) | ls --color=auto |
Practical Examples
#1 List all files with details
Shows all files including hidden ones, with permissions, owner, group, size, and modification date.
$ ls -la /home/user
Output:
total 48
drwxr-xr-x 6 user user 4096 Mar 14 10:30 .
drwxr-xr-x 3 root root 4096 Jan 15 09:00 ..
-rw-r--r-- 1 user user 220 Jan 15 09:00 .bash_logout
-rw-r--r-- 1 user user 3526 Jan 15 09:00 .bashrc
drwxr-xr-x 2 user user 4096 Mar 14 10:30 Documents
-rw-r--r-- 1 user user 807 Jan 15 09:00 .profile
#2 List files sorted by size
Shows files in /var/log sorted by size (largest first) with human-readable sizes.
$ ls -lhS /var/log
Output:
-rw-r----- 1 syslog adm 15M Mar 14 12:00 syslog
-rw-r----- 1 syslog adm 5.2M Mar 14 12:00 auth.log
-rw-r----- 1 syslog adm 2.1M Mar 14 12:00 kern.log
#3 List only directories
Shows only directories in the current location, not their contents.
$ ls -d */
Output:
Documents/ Downloads/ Music/ Pictures/ Videos/
#4 List files modified today
Shows the 20 most recently modified files, newest first.
$ ls -lt --time=mtime | head -20
Output:
total 128
-rw-r--r-- 1 user user 4096 Mar 14 12:30 report.pdf
-rw-r--r-- 1 user user 2048 Mar 14 11:15 notes.txt
#5 List files with inode numbers
Shows inode numbers, useful for identifying hard links.
$ ls -li
Output:
262144 -rw-r--r-- 2 user user 1024 Mar 14 10:00 file1.txt
262144 -rw-r--r-- 2 user user 1024 Mar 14 10:00 file1_link.txt
#6 Recursive listing with depth
Lists all files and subdirectories recursively under /etc/nginx/.
$ ls -R /etc/nginx/
Output:
/etc/nginx/:
conf.d mime.types nginx.conf sites-available sites-enabled
/etc/nginx/conf.d:
default.conf
#7 List files with specific pattern
Lists only files matching the .log extension with full details.
$ ls -la *.log
Output:
-rw-r--r-- 1 user user 15360 Mar 14 12:00 access.log
-rw-r--r-- 1 user user 4096 Mar 14 12:00 error.log
#8 Count files in a directory
Counts the number of files and directories (one per line output).
$ ls -1 | wc -l
Output:
42
#9 List files sorted by extension
Sorts files alphabetically by their extension.
$ ls -lX
Output:
-rw-r--r-- 1 user user 2048 Mar 14 config.json
-rw-r--r-- 1 user user 4096 Mar 14 style.css
-rw-r--r-- 1 user user 8192 Mar 14 index.html
#10 List with file type indicators
Appends indicators: / for directories, * for executables, @ for symlinks.
$ ls -F
Output:
bin/ config.json deploy.sh* logs@ README.md
Tips & Best Practices
Alias for convenience: Add "alias ll='ls -lah'" to your .bashrc for a quick detailed listing command.
Color output: Most modern distros have ls aliased to "ls --color=auto". Blue = directory, green = executable, cyan = symlink, red = archive.
Large directories: For directories with thousands of files, ls can be slow. Use "ls -f" to skip sorting, or "find" for better performance.
Hidden files only: Use "ls -d .*" to list ONLY hidden files without showing all files.
Frequently Asked Questions
What is the difference between ls -a and ls -A?
ls -a shows ALL files including . (current directory) and .. (parent directory). ls -A shows hidden files but excludes . and .. entries.
How do I see file sizes in a human-readable format?
Use ls -lh. This displays sizes in KB, MB, or GB instead of raw bytes. For example, 4096 becomes 4.0K.
How can I list files recursively?
Use ls -R to recursively list all subdirectories. For a tree-like view, install and use the "tree" command instead.
What do the permission letters mean in ls -l?
r = read, w = write, x = execute. The 10-character string shows: file type (d/-/l), owner permissions (rwx), group permissions (rwx), other permissions (rwx).
Related Commands
More File Management Commands
Master Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books →