Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the size of a directory and print it in bash?

How can I check the size of a specific directory in Linux(CentOS) and print it out?

I know I can use lvs and df(df -f "path to dir") but how can I get just a number like 78Gbs instead of the whole thing(avail, used, etc)

like image 398
dominique120 Avatar asked Dec 10 '25 05:12

dominique120


2 Answers

To find the size of a directory without any other info:

$ du -sh path/to/dir | awk '{print $1}'
118M

To get the size of a partition:

$ df -h /home | tail -n1 | awk '{print $2}'
14G

The tail gets rid of the header and awk prints just the size of the partition. On the other hand, if you want just the amount of space used on the partition:

$ df -h /home | tail -n1 | awk '{print $3}'
11G
like image 113
John1024 Avatar answered Dec 11 '25 23:12

John1024


do you mean du -sh ? Depending on your need you may use other options

like image 40
user3159253 Avatar answered Dec 11 '25 23:12

user3159253



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!