How to find the % of disk space used by a directory using du command in linux.
Advertisement
Answer
You need to use a combination of df (file system) and du (file space usage). Just one of these commands won’t do.
JavaScript
x
#!/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?
JavaScript
/home/user/Downloads is 12.00%