Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
List the Size of Each File and Directory and Sort by Size in Linux
Listing and sorting files and directories by size is essential for system administration, disk space management, and file organization. Linux provides multiple command-line tools and GUI methods to accomplish this task efficiently. This guide covers various approaches to list files and directories sorted by their size.
Command Line Methods
Using the ls Command
The ls command is the most common tool for listing directory contents. By default, it sorts entries alphabetically, but you can modify this behavior with specific options.
Basic listing with detailed information:
ls -la
This displays all files (including hidden ones) with detailed information like permissions, size, and modification date.
Sort by size (largest first):
ls -laS
total 180 -rw------- 1 user user 18171 Mar 6 18:03 .bash_history -rw-r--r-- 1 user user 11517 Feb 25 23:35 data.pcap -rw------- 1 user user 10422 Feb 12 13:46 .viminfo drwxr-xr-x 4 user user 4096 Feb 18 23:38 Documents drwxr-xr-x 2 user user 4096 Feb 20 13:09 Downloads -rw-rw-r-- 1 user user 724 Feb 27 21:25 example.txt -rwxrwxr-x 1 user user 88 Mar 3 18:00 script.sh
Sort by size (smallest first):
ls -laSr
Human-readable sizes:
ls -laSh
total 172K -rw------- 1 user user 18K Mar 6 18:03 .bash_history -rw-r--r-- 1 user user 12K Feb 25 23:35 data.pcap -rw------- 1 user user 11K Feb 12 13:46 .viminfo drwxr-xr-x 4 user user 4.0K Feb 18 23:38 Documents -rw-rw-r-- 1 user user 724 Feb 27 21:25 example.txt -rwxrwxr-x 1 user user 88 Mar 3 18:00 script.sh
Exclude directories from listing:
ls -laSh | grep -v '^d'
Using the du Command
The du (disk usage) command shows the actual disk space used by files and directories, which is more accurate for space management.
Sort all files and directories by size:
du -ah | sort -h
4.0K ./config/settings.conf 8.0K ./logs/error.log 12K ./data/backup.tar 24K ./scripts/ 48K ./documents/report.pdf
Limit depth and sort by size:
du -ah --max-depth=1 | sort -hr
Sort only directories:
du -h | sort -hr
Using the find Command
The find command offers more flexibility for locating and sorting files based on various criteria.
Find and sort files by size:
find . -type f -ls | sort -k7 -nr
Find and sort directories only:
find . -type d -ls | sort -k7 -nr
Common Options Summary
| Option | Command | Description |
|---|---|---|
| -S | ls | Sort by size (largest first) |
| -r | ls, sort | Reverse order (smallest first) |
| -h | ls, du | Human-readable sizes (K, M, G) |
| -a | ls, du | Include hidden files |
| --max-depth=N | du | Limit directory traversal depth |
GUI Method
Linux file managers provide an intuitive way to sort files and directories by size. In most file managers:
Open the file manager and navigate to your desired directory
Click on the column headers or use the view menu
Select "Sort by Size" to arrange files from largest to smallest
Click again to reverse the order (smallest to largest)
Practical Examples
Find largest files in home directory:
find ~ -type f -exec du -h {} + | sort -hr | head -10
Show directory sizes sorted by usage:
du -sh */ | sort -hr
List files larger than 100MB:
find . -type f -size +100M -exec ls -lh {} + | sort -k5 -hr
Conclusion
Linux offers multiple powerful methods to list and sort files by size using command-line tools like ls, du, and find, each with specific advantages. The ls command is ideal for quick directory listings, du provides accurate disk usage information, and find offers advanced filtering capabilities. GUI file managers provide an accessible alternative for users preferring graphical interfaces.
