Sometimes we need to get the size of a directory on the command line. For example, If you are running low on disk space, you can use the du
command to identify which directories are taking up the most space on your system.
This can help you free up space by deleting unnecessary files or moving them to a different location.
Similarly, If you are running a website or application that generates large amounts of data, you may want to monitor the growth of your directories over time.
By regularly checking the size of your directories, you can identify trends and plan for future storage needs.
In this article, we’ll see how to get the size of a directory on the command line.
How to get the Size of a Directory via Command Line?
To get the size of a directory via command line, you can use the du
command (which stands for “disk usage”). Here are the steps:
Open the terminal or command prompt on your system.
Navigate to the directory whose size you want to get. You can use the cd
command to change the directory, for example:
cd /path/to/directory
Once you are in the directory, run the du
command followed by the -sh
option.
The -s
option tells du
to display only a total for the specified directory, and the -h
option tells it to display the size in a human-readable format (e.g. “KB”, “MB”, “GB”):
du -sh .
Press Enter to execute the command. The terminal should display the total size of the directory in a human-readable format, for example
2.5G .
In this example, the directory size is 2.5 gigabytes. The “.” represents the current directory.
If you want to check the size of a different directory, replace “.” with the path to that directory.
How to Get the Size of Directory in Linux Using tree Command?
The tree
command is a useful tool for displaying the directory structure of a file system in a hierarchical format.
To get the size of a directory using the tree
command in Linux, you can use the -h
and -s
options.
First, open the terminal on your Linux system.
Install the tree
command if it’s not already installed. You can do this by running the following command:
sudo apt-get install tree
This command is for Ubuntu and Debian-based systems. If you are using a different Linux distribution, use the appropriate package manager to install the tree
command.
Next, navigate to the directory whose size you want to get. You can use the cd
command to change the directory, for example:
cd /path/to/directory
Once you are in the directory, run the tree
command followed by the -h
and -s
options.
The -h
option tells tree
to display the size in a human-readable format, and the -s
option tells it to print the size of each file and directory.
tree -hs
Now execute the command. The terminal should display the directory structure of the current directory, along with the size of each file and directory in a human-readable format.
For example:
/path/to/directory
├── 1.5M file1.txt
├── 3.2G dir1
│ ├── 2.1M file2.txt
│ ├── 1.9G sub-dir1
│ └── 1.1G sub-dir2
├── 4.8M dir2
└── 3.2G dir3
4 directories, 4 files
In this example, the size of each file and directory is displayed in a human-readable format, and the -s
the option also prints the total size of the directory.
How to Find the Size of a Linux Directory Using ncdu Command?
The ncdu
command is a useful tool for finding the size of a directory in Linux. Let’s find the size of a Linux directory using the ncdu
command.
First, Install the ncdu
command if it’s not already installed. You can do this by running the following command:
sudo apt-get install ncdu
This command is for Ubuntu and Debian-based systems. If you are using a different Linux distribution, use the appropriate package manager to install the ncdu
command.
Now, we’ll navigate to the directory whose size we want to find. Simply, use the cd
command to change the directory, for example:
cd /path/to/directory
Once we are in the directory, run the ncdu
command. This will start the ncdu
program and display a list of all directories and files in the current directory:
ncdu
The terminal should display a list of all directories and files in the current directory, along with their sizes.
Additionally, to view the size of a specific directory or file, we can use the arrow keys to navigate to the directory or file. The ncdu
command will display the size of the selected directory or file:
ncdu 1.14.2 ~ Use the arrow keys to navigate, press ? for help
--- /path/to/directory ------------------------------------------------------------
2.1GiB [##########] dir1
565.7MiB [## ] dir2
453.5MiB [# ] dir3
176.5MiB [ ] file1.txt
12.4MiB [ ] file2.txt
11.2MiB [ ] file3.txt
In this example, the ncdu
command displays the size of each directory and file in the current directory.
To view the size of a specific directory, use the arrow keys to navigate to the directory and press Enter. The `ncdu
` command will display the size of the selected directory:
ncdu /tmp
Finally, exit the ncdu
program by pressing the q
key.
Conclusion
In Linux, there are various commands you can use to get the size of a directory.
The du
command is a common tool for finding the disk usage of files and directories, while the tree
command can be used to view the size of all directory contents in a hierarchical format.
Additionally, the ncdu
command provides an interactive way to find the size of a directory and its contents. Each of these commands can be helpful in different scenarios, depending on your needs.
By using these tools, you can quickly and easily find the size of directories on your Linux system.