This brief tutorial explains how to find the size of a directory in Unix-like operating systems. Finding size of files and directories in GUI mode is 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 find the size of a directory from CLI mode. Doing stuffs from command line is easier and faster. Allow me show you how to find a directory’s size in Linux in command line.
Find the size of a directory
To find out the size of a directory, we will use ‘du’ command. du stands for disk usage.
The typical syntax of du command is given below:
Let us type the ‘du’ command in the Terminal and see what it displays.
sample output:
As you see above, du command displays the disk usage of the directories along with its sub-directories in the current directory.
To display a particular directory’s size, for example ostechnix, run:
Sample output:
As you can see in the above output, the size of the directories is displayed in bits. We can also display the size in human readable format.
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.
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
To find out which sub-directories consume how much disk 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.
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.
Sample output:
Now, you will see the disk usage of all files and folders in human readable form.
We can also display the size of multiple directories at once as shown below.
If you want to check the total disk space used by a particular directory, use the -s flag.
Here, -s flag indicates summary.
Sample output:
Similarly, to display the total disk space used by multiple directories, for example ostechnix and /etc, run:
Sample output:
To display the grand total of directories, add -c flag with du -sh command.
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.
You might want to exclude certain 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.
Can we find the biggest or smallest directories or files? Of course, yes! Check the following guide.
For more details about ‘du’ command, check the man pages.
0 comments:
Post a Comment