LoginSignup
3
2

More than 1 year has passed since last update.

Linux: ディスク容量不足の調査に活用できるコマンド

Posted at

ディスク容量不足の調査について、活用できるコマンドをメモしておきます。
なお、権限不足でディレクトリ内を確認できないことがあるため、rootで実行することを推奨します。

実施環境:
Linux
[root@testhost ~]# uname -a
Linux testhost 4.18.0-147.8.1.el8_1.x86_64 #1 SMP Thu Apr 9 13:49:54 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[root@testhost ~]# echo $SHELL
/bin/bash

1. 現在のディスク使用率を確認する

次のコマンドを実行することで、指定したディレクトリの所属するディスクの使用率が確認できます。

Linux
df -h ディレクトリパス

以下、実行例です。

Linux
[root@testhost ~]# df -h /var/log
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/c1_testhost-var  4.0G  1.3G  2.8G  32% /var

ディレクトリ/var/logは/varでディスクが切られているため、/varにマウントされているディスクについての情報が表示されます。
Sizeが総容量、Usedが使用容量、Availが空き容量、Use%が使用率です。

2. サイズの大きいファイルを抽出する

次のコマンドを実行することで、指定したディレクトリ配下のファイル及びディレクトリをサイズの降順で表示できます。

Linux
du -ah ディレクトリパス | sort -hr | head -表示個数

以下、実行例です。
上位3個を表示しています。

Linux
[root@testhost ~]# du -ah /var/log | sort -hr | head -3
14M     /var/log
6.0M    /var/log/anaconda
3.1M    /var/log/anaconda/journal.log

duコマンドのオプション"-a"を外すと、ディレクトリのみが表示されます。

Linux
[root@testhost ~]# du -h /var/log | sort -hr | head -3
14M     /var/log
6.0M    /var/log/anaconda
700K    /var/log/sa

特定のファイルやディレクトリを除外する場合は、grepを挟むとよいです。

Linux
[root@testhost ~]# du -ah /var/log | grep -v /var/log/anaconda | sort -hr | head -3
14M     /var/log
812K    /var/log/dnf.log
700K    /var/log/sa

3. 長期間更新されていないファイルを抽出する

次のコマンドを使用することで、長期間更新されていないファイルを検索できます。

Linux
find ディレクトリパス -mtime +日数 -size +サイズ -type f | xargs ls -hl

以下、実行例です。
「更新日時が7日前より古く、サイズが1MBより大きいファイル」を検索しています。

Linux
[root@testhost ~]# date
Sat Jun 5 16:40:19 JST 2021
[root@testhost ~]# find /var/log -mtime +7 -size +1M -type f | xargs ls -hl
-rw-------. 1 root root 3.1M Jun 13  2020 /var/log/anaconda/journal.log
-rw-------. 1 root root 1.7M Jun 13  2020 /var/log/anaconda/lvm.log

こちらも項番2と同様に、grepコマンドでファイルやディレクトリの除外ができます。

Linux
[root@testhost ~]# date
Sat Jun 5 19:01:04 JST 2021
[root@testhost ~]# find /var/log -mtime +7 -size +100k -type f | grep -v /var/log/anaconda | xargs ls -hl
-rw------- 1 root root 248K May 25  17:23 /var/log/messages.20210525.gz
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2