Find the size of a directory / folder
To find out the size of a directory or folder, we will use ‘du’ command. du is stands for disk usage.
The typical du command usage is given below:
du [OPTION]... [FILE] [directory]...
du [OPTION]... --files0-from=F
Let us type the ‘du’ command in the Terminal and see what it displays.
du
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 directory’s size, run:
du ostechnix/
Sample output:
36252 ostechnix/Swami Vivekananda (Chicago Speech)
452 ostechnix/MultiCD/plugins
44 ostechnix/MultiCD/.git/hooks
4 ostechnix/MultiCD/.git/branches
1012 ostechnix/MultiCD/.git/objects/pack
4 ostechnix/MultiCD/.git/objects/info
1020 ostechnix/MultiCD/.git/objects
8 ostechnix/MultiCD/.git/logs/refs/heads
8 ostechnix/MultiCD/.git/logs/refs/remotes/origin
12 ostechnix/MultiCD/.git/logs/refs/remotes
24 ostechnix/MultiCD/.git/logs/refs
32 ostechnix/MultiCD/.git/logs
8 ostechnix/MultiCD/.git/refs/heads
4 ostechnix/MultiCD/.git/refs/tags
8 ostechnix/MultiCD/.git/refs/remotes/origin
12 ostechnix/MultiCD/.git/refs/remotes
28 ostechnix/MultiCD/.git/refs
8 ostechnix/MultiCD/.git/info
1168 ostechnix/MultiCD/.git
140 ostechnix/MultiCD/maps
2706504 ostechnix/MultiCD
2832056 ostechnix/
As you see size of the directories is displayed in bits. Let us display it in human readable format.
To do so, add -h tag with du command as shown below.
du -h ostechnix/
Sample output:
36M ostechnix/Swami Vivekananda (Chicago Speech)
452K ostechnix/MultiCD/plugins
44K ostechnix/MultiCD/.git/hooks
4.0K ostechnix/MultiCD/.git/branches
1012K ostechnix/MultiCD/.git/objects/pack
4.0K ostechnix/MultiCD/.git/objects/info
1020K ostechnix/MultiCD/.git/objects
8.0K ostechnix/MultiCD/.git/logs/refs/heads
8.0K ostechnix/MultiCD/.git/logs/refs/remotes/origin
12K ostechnix/MultiCD/.git/logs/refs/remotes
24K ostechnix/MultiCD/.git/logs/refs
32K ostechnix/MultiCD/.git/logs
8.0K ostechnix/MultiCD/.git/refs/heads
4.0K ostechnix/MultiCD/.git/refs/tags
8.0K ostechnix/MultiCD/.git/refs/remotes/origin
12K ostechnix/MultiCD/.git/refs/remotes
28K ostechnix/MultiCD/.git/refs
8.0K ostechnix/MultiCD/.git/info
1.2M ostechnix/MultiCD/.git
140K ostechnix/MultiCD/maps
2.6G ostechnix/MultiCD
2.8G ostechnix/
Now, the output is more clear. And the size of the directories is shown in Kilobytes, Megabytes, and Gigabytes.
Also, you can display the disk usages size only in KB, MB, or GB. To do so, use -k for kilobytes, -m for megabytes
du -k ostechnix/
du -m ostechnix/
As you see in the above outputs, du command only displays the disk usage of folders. So, what about the files? To display the disk usage of files and directories, use -a flag.
du -ah ostechnix/
Sample output:
Now, you will see the disk usage of all files and folders in human readable form.
You can also display the size of multiple directories at once as shown below.
du -h directory1 directory2
If you want to check the total disk space used by a particular directory, use the -s flag.
du -sh ostechnix
Sample output:
2.8G ostechnix
Similarly, to display the total disk space used by multiple directories, for example ostechnix and /etc, run:
du -sh ostechnix /home/sk/
Sample output:
2.8G ostechnix
279G /home/sk/
To display the grand total of directories, add -c flag with du -sh command.
du -csh ostechnix /home/sk/
Sample output:
2.8G ostechnix
279G /home/sk/
281G total
For more details about ‘du’ command, check the man pages.
man du
No comments:
Post a Comment