How to Get the Size of a Directory in Linux?
Introduction
Do you often find it difficult to find your valuable items in a messy room? Imagine being lost in a pile of files and folders without a proper directory. Every system needs a directory to store its valuables digitally. This leads to better storage management. Moreover, signs like unusual file sizes can easily indicate malware. The user can freely manage the backup with advanced storage requirements. Knowing the size of a directory in advance offers numerous advantages and benefits.
There are different ways to check the size of a directory in Linux system. In this article, you will learn about the default du command in Linux. Additionally, we have also included the process of ncdu and tree commands to get the Linux folder size.
Option 1: Display the Size of a Directory Using the du Command
The very first method to get the size of a directory in Linux is through the Du command in Linux. Follow the below steps.
- Go to the applications or system tool menu and start the terminal window.
- Now you need to navigate to the directory; use the below command to do so.
cd Documents
In the above command, it was assumed that the directory was
located documents folder.
- When you have reached the correct directory, simply run the du command in the following way.
du -sh
The term ‘du’ is used for disk usage command, and ‘sh’ is used for
human-readable format.
- The command will push the terminal to display the details of the directory, like the size and the content. Further, the size of the file will be presented in KB, GB, TB, etc. The result will be something like the below content.
25M Solutions
You can see in the above example that the file size is mentioned as ’25M’ where ‘M’ stand for megabyte. Further, the name of the directory on display is ‘solutions.’
Option 2: Get the Size of the Directory in Linux using Tree Command
Do you know the tree command is used to present the contents of a directory in a pictorial way? Yes, the tree command is very useful when it comes to structured presentation. Also, this command is not the default command to get the size of a directory in Linux. Take a look at the following steps to use this method.
- The first requirement is to install the command using the terminal window.
- So, open the terminal window through the applications menu.
- Run the below command if you are using Ubuntu or Debian-based systems.
sudo apt-get update
sudo apt-get install tree
- If you are using CentOS or Redhat, then use the below command to install the tree command.
sudo yum install tree
sudo yum install tree
- Now simply navigate to the desired directory with the below command.
cd Documents
You can change the term ‘documents’ with the correct folder name.
- Now all you have to do is to run the tree command.
tree -d -h
The directory’s content will be displayed in a tree-like structure, and you can easily find out the Linux folder size.
Also Read: How to Check the Kernel Version in Linux Using the Command Line?
Option 3: Find the Size of a Linux Directory Using ncdu Command
This is the most powerful command in all three methods used to detect the size of a directory in Linux. The basic step to use this command is to install it on your Linux system through the terminal window.
- Open the terminal window by pressing Ctrl+Alt+T and enter the below command on your Ubuntu or Debian-based system.
sudo apt-get update
sudo apt-get install ncdu
- If you are a user of CentOS or RedHat, then run the following command to complete the installation of the ncdu command.
sudo yum install ncdu
- Now following the same process like the du and tree command, we will navigate to the desired directory. Also, you can replace the term ‘document’ with the correct name of the directory.
cd Documents
- Now that you have reached the desired directory, simply run the ncdu command in the terminal window to check the Linux folder size.
ncdu
- Something like the below content will be displayed on your system’s screen. You can easily navigate through the output using the arrow keys.
1.4GiB [##########] /path/to/directory
1.2GiB [####### ] subdirectory1
70MiB [## ] file1
30MiB [# ] file2
100MiB [### ] subdirectory2
60MiB [## ] file3
40MiB [## ] file4
- Just press the letter ‘q’ when you are done with the process to exit the ncdu command.
Also Read: Learn to use SCP Command in Linux (with Examples)
Conclusion
Overall, knowing the size of a directory in Linux is crucial for effective storage management, backup planning, and system performance analysis. The ncdu command, in particular, offers advanced features and user-friendliness, making it a popular choice for this purpose.
Remember that the size of a directory may vary based on its content, so it’s essential to regularly check and manage your directories to optimize disk space usage on your Linux system.