본문으로 건너뛰기

[Linux] du / ncdu 활용

du --max-depth 로 경로별 디스크 사용량을 확인하는 방법과, 방향키로 탐색하며 사용량을 볼 수 있는 ncdu 도구의 설치와 사용법을 정리합니다.

요약

  • du -h --max-depth=1 을 사용하면 / 하위 1단계 디렉토리별 사용량만 간단히 확인할 수 있습니다.
  • ncdu 를 사용하면 경로를 옮겨 다시 명령을 실행하지 않고 방향키와 Enter 로 depth 별 사용량을 탐색할 수 있습니다.
  • ncdu 는 EPEL repository 를 활성화한 뒤 yum install ncdu 로 설치합니다.

du

현재 경로의 사용률을 확인 하는 명령입니다.

[opc@instance-20201011-1438 TEST]$ tree
.
├── big1
│   ├── 1g_bigfiles.img
│   ├── 1g_bigfiles1.img
│   └── 1g_bigfiles2.img
├── big2
│   └── 1g_bigfiles6.img
├── big3
├── big4
├── dummyfile1
├── dummyfile10
├── dummyfile2
├── dummyfile3
├── dummyfile4
├── dummyfile5
├── dummyfile6
├── dummyfile7
├── dummyfile8
└── dummyfile9

4 directories, 14 files

위와 같은 구조를 가지고 있는 경로에서 du 명령을 사용할 경우,

[opc@instance-20201011-1438 TEST]$ du -h
3.0G    ./big1
1.0G    ./big2
0       ./big3
0       ./big4
4.1G    .

위와 같이 각 디렉토리 및 현재 경로의 사용량을 확인 할 수 있습니다.

유용한 du 옵션

/ 하위의 특정 경로가 사용량이 높은지 확인 하려면 아래 옵션을 추가하면 됩니다.
-d, --max-depth=N print the total for a directory (or file, with --all)

No option 일 경우

[opc@instance-20201011-1438 /]$ du -h / 
16K     /boot/efi
0       /boot/grub2
241M    /boot
0       /dev/oracleoci
0       /dev/dri
0       /dev/snd
0       /dev/mqueue
0       /dev/hugepages
0       /dev/disk/by-partlabel
0       /dev/disk/by-uuid
0       /dev/disk/by-partuuid
0       /dev/disk/by-path
0       /dev/disk/by-id
0       /dev/disk
0       /dev/block
0       /dev/bsg
0       /dev/char
0       /dev/mapper
0       /dev/net
0       /dev/pts
168K    /dev/shm
...

위와 같이 / 하위 모든 depth 에 대해 출력합니다.

--max-depth 사용할 경우

[opc@instance-20201011-1438 /]$ du -h / --max-depth=1 2>/dev/null
241M    /boot
168K    /dev
0       /proc
56M     /run
0       /sys
23M     /etc
2.4G    /var
4.0K    /root
1.1M    /tmp
2.4G    /usr

위와 같이 depth 1 에 해당되는 디렉토리 경로 사용량을 출력합니다.

ncdu tool

위와 같이 du --max-depth 명령을 사용하면 원하는 정보 수집이 가능하지만 depth 1 에 대해 확인 후 depth 2 level 확인이 필요한 경우,
특정 경로로 이동 후 다시 명령을 수행해야되는 불편함이 있습니다.

그리하여 ncdu 와 같은 Tool 을 이용하면 좀 더 편리하게 depth 별로 사용량 확인이 가능합니다.

ncdu 설치

해당 Package 를 설치하기 위해서는 epel repository 가 활성화 되어 있어야 됩니다.

[opc@instance-20201011-1438 /]$ sudo yum install ncdu
Loaded plugins: langpacks, ulninfo
Resolving Dependencies
--> Running transaction check
---> Package ncdu.x86_64 0:1.15.1-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================= 
Package               Arch                    Version                         Repository                           Size 
=========================================================================================================================
Installing:
 ncdu                  x86_64                  1.15.1-1.el7                    ol7_developer_EPEL                   52 k 
...

ncdu 사용

사용량 확인이 필요한 경로에서 ncdu 명령을 수행하면 아래와 같이 출력이 되며, 방향키와 enter 를 이용하여 사용량 확인이 가능합니다.

ncdu 1.15.1 ~ Use the arrow keys to navigate, press ? for help
--- /home/opc/TEST ------------------------------------------------------------------------------------------------------                         
                         /..
    3.0 GiB [##########] /big1
    1.0 GiB [###       ] /big2
e   0.0   B [          ] /big4
e   0.0   B [          ] /big3
    0.0   B [          ]  dummyfile9
    0.0   B [          ]  dummyfile8
    0.0   B [          ]  dummyfile7
    0.0   B [          ]  dummyfile6
    0.0   B [          ]  dummyfile5
    0.0   B [          ]  dummyfile4
    0.0   B [          ]  dummyfile3
    0.0   B [          ]  dummyfile2
    0.0   B [          ]  dummyfile10
    0.0   B [          ]  dummyfile1









 Total disk usage:   4.0 GiB  Apparent size:   4.0 GiB  Items: 18
ncdu 1.15.1 ~ Use the arrow keys to navigate, press ? for help
--- /home/opc/TEST/big1 -------------------------------------------------------------------------------------------------
                         /..
    1.0 GiB [##########]  1g_bigfiles2.img
    1.0 GiB [##########]  1g_bigfiles1.img
    1.0 GiB [##########]  1g_bigfiles.img




















 Total disk usage:   3.0 GiB  Apparent size:   3.0 GiB  Items: 3

[참고] ncdu 기본 사용법

? 를 입력하면 기본 사용법을 확인 할 수 있습니다.

┌───ncdu help─────────────────1:Keys───2:Format───3:About──┐
│                                                          │
│       up, k  Move cursor up                              │
│     down, j  Move cursor down                            │
│ right/enter  Open selected directory                     │
│  left, <, h  Open parent directory                       │
│           n  Sort by name (ascending/descending)         │
│           s  Sort by size (ascending/descending)         │
│           C  Sort by items (ascending/descending)        │
│           M  Sort by mtime (-e flag)                     │
│           d  Delete selected file or directory           │
│           t  Toggle dirs before files when sorting       │
│                        -- more --                        │
│                                         Press q to close │
└──────────────────────────────────────────────────────────┘

자주 묻는 질문

du 명령에서 특정 깊이까지만 사용량을 보려면 어떻게 하나요?

-d, --max-depth=N 옵션을 사용합니다. 예를 들어 du -h / --max-depth=1 2>/dev/null 은 / 바로 아래 디렉토리별 사용량만 출력합니다.

ncdu 는 du 와 비교해 어떤 점이 편리한가요?

depth 1 확인 후 depth 2 를 보려고 해당 경로로 이동해 다시 명령을 실행할 필요가 없습니다. 방향키와 Enter 로 디렉토리를 오가며 사용량을 바로 확인할 수 있습니다.

ncdu 는 어떻게 설치하나요?

EPEL repository 가 활성화되어 있어야 하며, sudo yum install ncdu 로 설치합니다.

ncdu 에서 정렬이나 삭제 같은 단축키는 어떻게 확인하나요?

? 를 입력하면 도움말이 표시됩니다. 예를 들어 s 는 크기순 정렬, n 은 이름순 정렬, d 는 선택한 파일이나 디렉토리 삭제입니다.