How to check the size of a directory and print it

2019-07-17 01:41发布

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)

4条回答
Fickle 薄情
2楼-- · 2019-07-17 01:53

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
查看更多
劫难
3楼-- · 2019-07-17 02:00

What you're looking for is the du command.

To get the grand total of the arguement(s):

du -c

Same as above, but only print the summary:

`du -sc`

If wanted, you can throw in a -h in there too with the same effect as with dh

查看更多
做个烂人
4楼-- · 2019-07-17 02:03

Try du instead of df. Check du manpage!

du -hs /directory
查看更多
不美不萌又怎样
5楼-- · 2019-07-17 02:05

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

查看更多
登录 后发表回答