본문 바로가기
Linux

[linux] cmd: disk usage and disk free: du and df

by ds31x 2024. 1. 24.

du

file들의 집합 또는 directory가 disk얼마나 사용하고 있는지를 표시 (recursive하게 동작)

-s 또는 -d option 을 반드시 사용해야 한다.
디스크를 차지하는 모든 요소들을 순회하는 방식으로 동작하기 때문임.

du -sh [directory]
  • -h : --human-readable 사람이 읽기 쉬운 형태로 출력.
    • -k : 출력 숫자의 단위를 1kb (kilo-bytes) 로 지정한다.
    • -m : 출력 숫자의 단위를 1mb (mega-bytes) 로 지정한다.
  • -s : --summarize 전체 용량만 출력.
  • -d : --max-depth 어느 깊이까지 내려갈지를 정함.
  • -a : --all 모든 파일과 모든 하위디렉토리 출력.
  • -c : --total 마지막 줄에 합계를 출력.
  • -x : --one-file-system 현재 파일시스템의 파일 사용량만 출력한다.

용량이 큰 10개 순으로 출력

du -ckx | sort -n -r | head

root(/)에서 부터 아래 5계층까지에서 1024kB 이상의 directory만 확인

sudo du -k -x -d 5 / | awk '$1 >=1024{print}'

df

현재 storage에서 free 영역이 얼마나 되는지에 대한 정보를 출력해줌.

df -hP
  • -P: POSIX output format
  • -h: Human readable (e.g. 1K, 243M, 2G)
    • -k : 1024 byte blocks (default)
    • -m : 1M-byte blocks
  • -T : print file system type
  • -a : Show all file systems.

아래의 예제는

  • 현재 사용중인 storage 용량(~ used)과
  • 사용가능한 용량(free),
  • 전체 용량(total)을
  • GiB로 확인함.
df -P |grep -v ^Filesystem | awk '{total += $2}{used += $3}{free += $4} END {print used/1024/1024 " / " total/1024/1024 "(free:"free/1024/1024 ") GiB" }'
728x90