Psychz - Medha
Votes: 0Posted On: Jun 21, 2021 05:12:19
Over time, your disk drive may get cluttered with many unnecessary files taking up large amounts of disk space. Usually, Linux systems run out of disk space due to large log or backup files on the filesystem such as /tmp/ or /var/ or /home/. We will see how to use commands to identify large files and directories on Ubuntu 18 Linux system.
We will be using a combination of three commands using pipes. Let us see how
Steps to find Largest Directories in Linux
du: Estimate file space usage.
sort: Sort lines of text files or given input data.
head: Output the first part of files, i.e., to display the first # largest files. # - you can pass a value in number
find: Search file.
How to find out top Directories and files in Linux
Type the following command at the shell prompt to find out the top 10 largest file/directories:
# du -a /var | sort -n -r | head -n 10
Sample outputs:
3129868 /var
2633128 /var/log
2580580 /var/log/journal
2580576 /var/log/journal/148ccab1fd69e38429968ea6df486546
365440 /var/lib
235332 /var/lib/apt
235288 /var/lib/apt/lists
129716 /var/cache
122884 /var/log/journal/148ccab1fd69e38429968ea6df486546/system@dab35793633c410d96ea039f49e41a9a-00000000002286b8-0005c4abe3087a49.journal
122884 /var/log/journal/148ccab1fd69e38429968ea6df486546/system@dab35793633c410d96ea039f49e41a9a-000000000020a1de-0005c49843f2ffd7.journal
For more readable output, use the following combinations of commands.
$ du -hsx * | sort -rh | head -10
Output
2.6G log
357M lib
127M cache
1.5M backups
28K spool
20K www
20K tmp
4.0K snap
4.0K opt
4.0K mail
-h : display sizes in a human-readable format (e.g., 1K, 10M, 5G)
-s : show only a total for each argument (summary)
-x : skip directories on different file systems
-r : reverse the result of comparisons
-h : compare human readable numbers
head -10 : show the first 10 lines.
We hope this information helps you in improving your overall experience with Linux operating system.