-3

How to find the % of disk space used by a directory using du command in linux.

Vishnu
  • 479
  • 1
  • 3
  • 14

2 Answers2

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
0

If you have access to install packages I recommend ncdu - NCurses Disk Usage

MosheZada
  • 2,189
  • 1
  • 14
  • 17