- How to Get the Size of a Directory in Linux
- Option 1: Display the Size of a Directory Using the du Command
- Option 2: Get Size of Directory in Linux Using tree Command
- Option 3: Find the Size of a Linux Directory Using ncdu Command
- Checking Directory Size with du Command in Linux
- Using du command to get directory size in Linux
- Show disk size in human-readable format
- Show the sizes of the files as well
- Bonus Tip: Solving the mystery of 4 KB
- Show only the total size of the directory in Linux
- Show the disk usage by multiple directories
- Show the grand total of all the directory sizes
- Don’t show the sizes of the subdirectories
- Exclude certain type of files while calculating disk size
- Bonus Tip: Finding the biggest subdirectory
- How To Find The Size Of A Directory In Linux
- Find the size of a directory in Linux
- Conclusion
How to Get the Size of a Directory in Linux
Home » SysAdmin » How to Get the Size of a Directory in Linux
Many users run Linux from the command line. However, the command line — sometimes known as the terminal — doesn’t have an intuitive interface for checking disk space in Linux.
This guide shows you how to find the size of a specific directory in Linux from the command line.
- A system running Linux
- A command line / terminal window (available by clicking Search, then typing terminal)
- A user account with sudo or root privileges
Note: In Linux, a directory is the equivalent of a folder in Windows. A directory may have directories inside (called subdirectories), or it may only contain files.
Option 1: Display the Size of a Directory Using the du Command
The du command stands for disk usage. This command is included by default in most Linux distributions.
You can display the size of your current directory by typing du in the command line:
The system should display a list of the contents of your home directory, with a number to the left. That number is the size of the object in kilobytes.
You can add the -h option to make the output more readable:
Each entry will start with a number and a letter. The number is the amount of space used, and the letter (usually K, M, or G) indicates Kilobytes, Megabytes, or Gigabytes. For example:
To find the size of a specific directory different from your current working directory. The du command allows you to specify a directory to examine:
This displays the size of the contents of the /var directory. You may see some entries with an error, as in the image below.
This happens when your user account does not have permission to access a particular directory. Use the sudo or su command to get access privileges:
Note: Some versions of Linux don’t enable sudo by default. You can use the su command to switch to the root user account instead.
To display total disk usage of a particular directory, use the -c command:
Options can be combined. If you wanted to repeat the previous command in human-readable format, enter the following:
You can limit the scan to a certain level of subdirectory by using the max-depth option. For example, to scan only the size of the top directory, use —max-depth=0 :
If you wanted to list only the top directory and the first layer of subdirectories, change —max-depth=1 :
If you run into trouble or want to explore more options for the du command, enter the following command to display the help file:
Option 2: Get Size of Directory in Linux Using tree Command
By default, the tree command is not included in some versions of Linux. To install it, enter the following:
- For Debian / Ubuntu
- For CentOS / RedHat
The tree command displays a visual representation of your directories. It uses lines to indicate which subdirectories belong where, and it uses colors to indicate directories and files.
tree can also be used with options. To display a human-readable size of the current directory’s subdirectories, enter the following:
Like the du command, tree can target a specific directory:
This command takes a few moments since the /var directory has many entries.
The tree command also has a help file, which you can access by entering:
Option 3: Find the Size of a Linux Directory Using ncdu Command
The ncdu tool stands for NCurses Disk Usage. Like the tree command, it is not installed by default on some versions of Linux. To install it, enter the following:
- For Debian / Ubuntu
- For CentOS / RedHat
The ncdu utility is an interactive display of your disk usage. For example, enter the following:
In the upper left corner, it displays the current directory being scanned. A column on the left displays the numerical size, a graph of #- signs to indicate the relative size, and the file or directory.
Use the up and down arrows to select different lines. The right arrow will browse into a directory, and the left arrow will take you back.
ncdu can be used to target a specific directory, for example:
For help, press the ? key inside the ncdu interface. To quit, press the letter q .
Note: Learn how to move directories in Linux using the GUI or system commands.
You now have three different options to find the size of a directory in Linux operating systems.
If you want to learn more about directories in Linux, read our article how to rename directories in Linux.
Источник
Checking Directory Size with du Command in Linux
Knowing the size of a file is easy in Linux. All you have to do is to use the -l and -h option with the ls command and it will show you the file size along with file permissions and file timestamps. Here’s a sample output:
You would notice something strange. The ls command shows the size of all the directories as 4 KB. That cannot be correct, can it be? Of course not.
The size of a folder or directory in Linux can be found using the du command. du here stands for disk usage. I’ll explain the logic behind the 4.0K size for the directories later in this tutorial. For the moment, let’s focus on getting the directory size.
If you want to check the directory size in Linux, you can use this command:
This will give you the total size of the said directory in human-readable format, i.e. KB, MB or GB.
Using du command to get directory size in Linux
I am going to show you various examples of the du command that you can use to check the directory size and the disk utilization.
The syntax for the du command is pretty simple.
Let’s see how to use the du command to get the file and directory size information in Linux.
Here’s the structure of the “tutorials” directory I am going to use in this tutorial:
Now if I run the du command in the “tutorials” directory, it will show the sizes of all the subdirectories and then sum the sizes of all the subdirectories and the files at the bottom of it.
This is the output for the tutorials directory.
Show disk size in human-readable format
Now the problem with the above output of the du command is that you don’t know if the 100 is 100KB or 100MB or 100GB. Don’t worry, you can change this behavior and display the directory size in a human readable format with the option -h.
Here’s the much easier to read output now:
You can use -m option for MB and -k option for KB instead of -h. But even if the size is less than 1 MB, it will always show the size as 1MB. This is why using -h option is always a better option.
Show the sizes of the files as well
Did you notice that the tutorials directory has several files but they don’t show up in the du command output? It’s because though the file size are counted in the total sum of the directory size, the files are not displayed by default.
To display the size of files along with the directories, you can use the -a option. It would be better if you combine it with -h option to get the sizes in human readable format.
Now the output will show the files along with the directories:
Bonus Tip: Solving the mystery of 4 KB
Note that the sizes may seem like that have been rounded off. I mean all the sizes are in the multiple of 4K. In fact, apart from the empty files, all the files are of at least 4 KB in size. Is that a coincidence? Not really.
Even if the text of the file is in bytes, the minimum file size is 4K because that’s the minimum block size of the filesystem. Irrespective of the size of the text on the file, it will be allotted at least one block of 4KB memory on the disk.
And since the memory blocks are 4KB in size, the sizes you’ll see will always be in the multiple of 4KB.
Now, you probably already know that everything is file in UNIX/Linux. A directory is essentially a file that has the information about all the location of all the files it ‘contains’.
So, when you use the ls command, it treats the directory as a file and shows its size which is one memory block and thus the size displayed is 4KB.
Show only the total size of the directory in Linux
If you find the output of the du command too verbose and would like to see just the total size of the directory in a human readable format, you can use the sum option -s.
Now the output will be just one line showing the total size of the directory:
Show the disk usage by multiple directories
It’s not that you are restricted to check the size of only one directory at a time. You can specify multiple directories in the du command.
For example, I am going to use the -sh options to show the total size of two directories here.
The output will show the size of both the directories individually:
Show the grand total of all the directory sizes
In the above example, you saw the total sizes of both the directories individually. You can use the option -c to show a grand total for the sum of all the directories in the output.
As you can see in the output, it sums up the sizes and gives you the grand total:
Don’t show the sizes of the subdirectories
What if you want to check the sizes of the all the directories in the current folder? By default, du command will go into the subdirectories of all the directories and the output becomes difficult to figure out specially if you have too many nested directories.
What you can do is to define the depth level to check while showing the sizes for the subdirectories.
So, if you want to see the sizes of the directories in the current folder, you can set the depth to 1 like this:
Now the output will show the subdirectories only in the current directory. It won’t go further than this.
If you cannot remember the –max-depth, you can use its short form -d flag:
Exclude certain type of files while calculating disk size
The du command gives you option to exclude certain type of files. You can use regex along with the –exclude option.
For example, to calculate the sum of all the files excluding files with extension txt, this command can be used:
And now if you see the output, the total size of the directory would have been reduced:
Bonus Tip: Finding the biggest subdirectory
You can combine the output of the du command with the sort command to sort the directories by the order of their size.
This will show the directories in the reverse order of their size i.e. the biggest directory on the top.
Of course the top one is the directory itself but the second one gives you the biggest subdirectory.
You can further combine with the head command or the tail command to get the x largest file or smallest files/directories.
Did you find it useful?
I have tried to explain all the essential usage of the du command in Linux. But as always, there are many more options available for the command that you can find in its man page. If you just wanted to find out the size of a directory in Linux, this tutorial should give you enough information. If you want to check disk space in Linux, use the df command.
Did you like the tutorial? Did it help you? Let me know in the comments. If you have some other cool tip about du command, why not share it with us?
Источник
How To Find The Size Of A Directory In Linux
This brief tutorial explains how to find the size of a directory in Linux operating systems. Finding the size of files and directories in graphical mode is very easy! All we have to do is just right click on the file or directory, and choose the properties option from the context menu. However, it is equally important to know how to check the size of a directory from CLI mode as well.
Find the size of a directory in Linux
We can get the directory size using ‘du’ command in Linux and Unix-like operating systems. The du command will estimate and summarize file and directory space usage.
For those wondering, du stands for disk usage.
The typical syntax of du command is given below:
Now, allow me to show you how to find directory size in Linux from command line using du command with examples.
1. Display current directory size
Enter ‘du’ command without any options to display the size of the current directory and its sub-directories.
sample output:
Find the size of current directory in Linux
As you see in the above output, du command displays the disk usage of my current directory along with its sub-directories.
2. Display size of a specific directory
To display a particular directory’s size, for example ostechnix, run:
Sample output:
You can also display the size of multiple directories in one go like below:
3. Display directory size in human-readable format
By default, du displays the size in bytes . We can also display the size in «human readable format» (i.e. auto-selecting the appropriate unit for each size), rather than the standard block size.
To do so, add -h tag with du command as shown below.
Sample output:
Now you see the size of the directories in Kilobytes, Megabytes and Gigabytes, which is very clear and easy to understand.
4. Display directory size in a specific format
We can also display the disk usage size only in KB, or MB, or GB.
To do so, use -k for kilobytes, -m for megabytes
5. Display grand total size of directories
We can display just the total human-readable size of the current working directory using -s and -h flags.
Here, -s flag indicates summary.
Sample output:
If you want to check the total disk space used by a particular directory, run:
We can also display the size of multiple directories at once as shown below.
To get the grand total of the combined directories in human-readable format, for example
/Music , add -c flag:
Here, -c refers the cumulative total.
Sample output:
To display only the grand total of the given directory including all the sub-directories, use ‘grep’ command with ‘du’ command like below.
6. Display sizes of a directory and subdirectories, up to N levels deep
A directory may contain large number of sub-directories. You may want to list the sizes of a directory and any subdirectories, only up to N levels deep.
The following command displays the human-readable sizes of the given directory and its sub-directories, up to 2 level deep:
7. Sort directories based on size
To find out which sub-directories consume how much disk size and sort them by their size, use this command:
The largest sub-directories will be displayed on the top. You can increase the directory depth level by increasing the value of —max-depth parameter.
8. Find the size of both files and directories
As you may noticed in the all above outputs, du command only displayed the disk usage of directories. But, what about the files?
To display the disk usage of all items including files and directories, use -a flag.
Now, you will see the disk usage of all files and folders in human readable format.
Sample output:
Display disk usage of files and folders in human readable format in Linux
9. Exclude certian type of files
The following command will display the size of the current directory including its sub-directories, but it will exclude the size of all .mp4 files.
Tip: Can we find the biggest or smallest directories/files? Of course, yes! Check the following guide.
For more details about ‘du’ command, check the man pages.
Suggested Read:
Conclusion
In this guide, we looked at how to find the total size of directory in Linux using du command with examples. As you can see, getting folder or directory size in Linux is not a big deal.
Источник