How to find the % of disk space used by a directory using du command in linux.
Asked
Active
Viewed 110 times
-3
-
yes. but couldn't figure out how to find "%". – Vishnu Nov 26 '16 at 14:24
-
1Take a look at the `df` command instead. – arkascha Nov 26 '16 at 14:29
-
df command would give me the disk free and usage of a partition.. but I want the amount of space used by this particular directory. – Vishnu Nov 26 '16 at 14:34
2 Answers
2
You need to use a combination of df (file system) and du (file space usage). Just one of these commands won't do.
#!/usr/bin/env bash
mydir="/home/user/Downloads"
totalSize=$(du -s -k $mydir | cut -f1)
fileSystemSize=$(df --output=size $mydir | tail -1)
pct=$(echo "scale=2;($totalSize/$fileSystemSize)*100" | bc -l )
echo "$mydir is $pct"%""
Is this what you are looking for?
/home/user/Downloads is 12.00%

NinjaGaiden
- 3,046
- 6
- 28
- 49