Raspberry Pi

How to Find Top Directories and Files in Terms of Disk Usage in Raspberry Pi

With devices such as Raspberry Pi, the disk space is always the major concern of any user. Usually, in the case of such devices, the users want to keep a check on the directories or files on the system and find out the files and directories information consuming the most space on the system.

If you are the one looking for a way to find out top directories and files in terms of usage, follow this article’s guidelines.

How to Find Top Directories and Files in Terms of Disk Usage in Raspberry Pi

There is no shortcut command in Linux to exactly display the top space-consuming directories in Raspberry Pi. But there are two useful commands which can serve the purpose, below we have listed such commands:

  • du Command
  • find Command

You can see the detailed discussion about the above-mentioned commands below.

1: du Command

The first command on our list to display the top space-consuming files and directories in Raspberry Pi is the du command. There are different modifiers which can be used with the du command, those modifiers are mentioned below:

  • du: It estimates file space usage by each file and directory.
  • du -s: It the total size for each argument.
  • du -m: Megabytes (size)
  • du -k: Kilobytes (size)

The du command displays the estimated space usage by each file and directory, the sort argument is piped with it to sort the files by their sizes, and the r modifier with sort command is used to reverse the sort order so that the directories with max space usage will display at the top of the list:

$ du -s * | sort -nr | head

To display the size of files in megabytes, just run the du command with the m modifier as mentioned below, the sort command is piped just for sorting the files from lower to higher size:

$ du -m | sort -n

Similarly, the k modifier can be used to display the sizes in kilobytes:

$ du -k | sort -n

Note: Remember that when sort -n is used the files with the larger sizes are present at the bottom and the lower size files are present at the top:

2: find Command

Last on our list is the find command which is basically used to find the list of the file along with the space occupied by each file. Piping this find command with sort -nr will sort the files in such a way that the files with larger sizes will display at the top and the head -10 is used so that I will get only the top 10 most space-consuming files because there are a lot of files in the system but our concern is the top space consuming files:

find . -printf '%s %p\n'| sort -nr | head -10

You can change the number “10” in the above command according to your choice.

Conclusion

To find the top directories and files on Raspberry Pi in terms of disk usage, the users can use the du, and find commands, which need to be piped with other commands such as sort or head to display the desired output. The complete syntax for each of these is discussed in the above guidelines. These commands are extremely useful for finding the top directories and files in the Raspberry Pi system.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.