Linux Command: ls
List directory contents and file information
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.
Syntax
ls [OPTION]... [FILE]...Key Options
-lโ Use long listing format showing permissions, owner, size, and date-aโ Show all files including hidden files (starting with .)-hโ Human-readable file sizes (KB, MB, GB)-Rโ List subdirectories recursively-tโ Sort by modification time, newest first-Sโ Sort by file size, largest first
Examples
List all files with details
ls -la /home/userOutput: 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
d
List files sorted by size
ls -lhS /var/logOutput: -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
List only directories
ls -d */Output: Documents/ Downloads/ Music/ Pictures/ Videos/
Pro Tips
- Add "alias ll='ls -lah'" to your .bashrc for a quick detailed listing command.
- Most modern distros have ls aliased to "ls --color=auto". Blue = directory, green = executable, cyan = symlink, red = archive.
- For directories with thousands of files, ls can be slow. Use "ls -f" to skip sorting, or "find" for better performance.
Learn more: Full ls reference โ
Related Resources