How to Find Out Top Directories and Files (Disk Space) in Linux

As a Linux administrator, you must check time to time which files and folders are eating more disk space. It is very necessary to find the unnecessary junks and free up them from your hard disk to avoid overrunning the disk usage.

This tutorial describes how to find the largest files and folders in the Linux file system using du and find command. If you want to learn more about these two commands in

Run the following command to find out top biggest directories in /home folder.

du -a /home | sort -n -r | head -n 5

The command above shows top 5 directories taking up the most of space.

Let us break down the command and see what each parameter has.

  1. du command: Estimate file space usage.
  2. a : Displays all files and folders.
  3. sort command : Sort lines of text files.
  4. -n : Compare according to string numerical value.
  5. -r : Reverse the result of comparisons.
  6. head : Output the first part of files.
  7. -n : Print the first ‘n’ lines. (In our case, We displayed first 5 lines).

The following command will list direct sub directories in current directory eating up more space in your machine. To view full path to largest directories see the next command.

du -hs * | sort -rh | head -5

The following will show biggest files and folders under current directory in human readable format.

du -hsx * | sort -rh | head -10

The following will show the full path to most spacing taking directories within your current working directory.

du -Sh | sort -rh | head -5

The command above is very useful to find the actual location of the directory eating up most of the space in your machine. You can simply go and explore the listed directories and delete them if they are not required.

Find out the meaning of each options using in above command:

  1. du command: Estimate file space usage.
  2. -h : Print sizes in human readable format (e.g., 10MB).
  3. -S : Do not include size of subdirectories.
  4. -s : Display only a total for each argument.
  5. sort command : sort lines of text files.
  6. -r : Reverse the result of comparisons.
  7. -h : Compare human readable numbers (e.g., 2K, 1G).
  8. head : Output the first part of files.

Find Out Top File Sizes Only

If you want to display the biggest file sizes only, then run the following command:

find -type f -exec du -Sh {} + | sort -rh | head -n 5

To find the largest files in a particular location, just include the path besides the find command:

find /home -type f -exec du -Sh {} + | sort -rh | head -n 5

OR

find /home -type f -printf "%s %p\n" | sort -rn | head -n 5

Filter files by File name

The following command will provide files under home directory which end with “log” tail. For example yoursite.php.error.log

Find files greater than [ > size]

The following command will list all the files which are greater than 5MB and ends with “log” in its name

find /home -name "*.log" -type f -size +5M -exec du -Sh {} + | sort -rh | head -n 5

Also read:

10 Useful du (Disk Usage) Commands to Find Disk Usage of Files and Directories

2 thoughts on “How to Find Out Top Directories and Files (Disk Space) in Linux

Leave a Reply