Check how extensive a directory is in Linux
You probably get a headache from underlining how much a directory consumes your disk space. Therefore, this post is for you to get rid of that.
We will be using the du command to gain insight into disk usage. du command stands for Disk Usage and it’s an ideal solution for checking a directory size. It offers various options for you to customize based on your needs.
So now I am going to show how we can put the du command into practice.
du command
Without specifying any options for du, du takes a file or directory as an argument, but if none is mentioned it will use the current directory as an argument.
In the above example, I was listing the folders inside the alphabets directory which contained folders A, B, and C directory respectively, and the total size of the alphabets folder is 16Kilobytes.
du -a
To list files or directories inside the subdirectories, you can use option -a or — all
du -a alphabets/
du -sh
To print only a total size of a directory, you can use the option -s or — summarize. In addition to this, if you want to make it more readable, you can use -h or — human-readable
du -sh alphabets/
du — exclude=PATTERN
You may also want to exclude some files in the directory, so you can do it by using an option— exclude= PATTERN.
du --exclude="*.txt" -a alphabets/
In the above example, all files that ended with an extension .txt had been ignored.
If you want to know more about the du command, you can simply type
man du
or
du --help
Then it will show you many more options that are available for further customizations.
To conclude, du is a really powerful CLI that can drive your decision on what to clean up your server effectively. Thanks for you reading.