How to find out the most important directories and files (hard drive space) in Linux

s2 0

As a Linux administrator, you need to regularly check which files and folders are taking up more space. It is very important to find and get rid of unnecessary junk from your hard drive.

This short tutorial describes how to find the largest files and folders in the Linux file system using du (disk usage) and the find command. If you want to learn more about these two commands, check out the articles below.

  • Learn 10 useful disk usage (du) commands in Linux
  • Master the ‘Find’ command with these 35 practical examples

How to find the largest files and directories in Linux

Run the following command to find the largest directories under. to find out /home partition.

# du -a /home | sort -n -r | head -n 5
Find the largest directories in Linux

The above command shows the 5 largest directories of my. at /Homeland partition.

Find the largest directories in Linux

To see the largest directories in the current working directory, do the following:

# du -a | sort -n -r | head -n 5
Find only the largest directories
Find only the largest directories

Let’s break down the command and see what each parameter says.

  1. du Command: Estimation of file space.
  2. a : Displays all files and folders.
  3. sort Command: Sort lines of text files.
  4. -n : Compare by the numeric value of the string.
  5. -r : Reverse the comparison result.
  6. head : Output of the first part of the files.
  7. -n : Print the first ‘n’ lines. (In our case we have displayed the first 5 lines).

Some of you would like to see the above result in a human readable format. i.e. you might want the largest files in. Show KB, MB, or GB.

# du -hs * | sort -rh | head -5
Find top directory sizes in Linux
Find top directory sizes in Linux

The above command shows the top directories that are taking up more space. If you think some directories are not important, you can simply delete some subdirectories or delete the entire folder to free up space.

To view the largest folders / files including subdirectories, do the following:

# du -Sh | sort -rh | head -5
Find largest folders and subdirectories
Find largest folders and subdirectories

Find out the meaning of each option with the command above:

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

Find out only the top file sizes

If you only want to see the largest file sizes, run the following command:

# find -type f -exec du -Sh {} + | sort -rh | head -n 5
Find top file sizes in Linux
Find top file sizes in Linux

To find the largest files in a specific location, just enter the path next to the find Command:

# find /home/tecmint/Downloads/ -type f -exec du -Sh {} + | sort -rh | head -n 5
OR
# find /home/tecmint/Downloads/ -type f -printf "%s %pn" | sort -rn | head -n 5
Find the top file size in a specific location
Find the top file size in a specific location

The above command shows the largest file /home/tecmint/Downloads Directory.

That’s all for now. Finding the largest files and folders is no big deal. Even an inexperienced administrator can find them easily. If you find this tutorial useful, please share it on your social networks and support TecMint.

Tags: