システム管理者の場合は、ファイルシステムが占める合計ディスク容量を認識して、不要なファイルやディレクトリを特定し、それらを削除してディスク容量を解放できるようにする必要があります。 duコマンドは「DiskUsage」の略で、システム内のファイルとディレクトリのディスク使用量の情報を確認するために使用できます。
このガイドでは、Linuxでファイルとディレクトリのサイズを確認する方法を説明します。
前提条件
- Atlantic.Netクラウドプラットフォーム上のLinuxサーバー
- サーバーで構成されているrootパスワード
ステップ1-Atlantic.Netクラウドサーバーを作成する
まず、Atlantic.Netクラウドサーバーにログインします。 2GB以上のRAMを搭載したオペレーティングシステムとしてUbuntu、CentOS、またはRocky Linuxを選択して、新しいサーバーを作成します。 SSH経由でクラウドサーバーに接続し、ページの上部で強調表示されているクレデンシャルを使用してログインします。
サーバーにログインしたら、次のコマンドを実行して、ベースシステムを最新の利用可能なパッケージで更新します。
apt-get update -y
または
dnf update -y
ステップ2–基本構文
duコマンドの基本的な構文を以下に示します。
du [OPTION]... [FILE]...
duコマンドで使用可能なすべてのオプションを表示するには、次のコマンドを実行します。
du --help
次の画面が表示されます。
ステップ3–ディレクトリのディスク使用状況の概要を表示する
オプションを指定せずにduコマンドを実行すると、ディレクトリのディスク使用量の概要が表示されます。
たとえば、ディレクトリ/ optのディスク使用量を確認するには、次のコマンドを実行します。
du /opt/
これにより、ディスク使用量の概要が最初の列にキロバイト単位で出力されます。
440 /opt/google/earth/pro/shaders 8804 /opt/google/earth/pro/resources/gdal 16 /opt/google/earth/pro/resources/flightsim/aircraft 12 /opt/google/earth/pro/resources/flightsim/hud 56 /opt/google/earth/pro/resources/flightsim/controller 12 /opt/google/earth/pro/resources/flightsim/keyboard 8 /opt/google/earth/pro/resources/flightsim/planet 112 /opt/google/earth/pro/resources/flightsim 12660 /opt/google/earth/pro/resources 15220 /opt/google/earth/pro/lang 1180 /opt/google/earth/pro/plugins/imageformats 468 /opt/google/earth/pro/plugins/bearer 396 /opt/google/earth/pro/plugins/platforms 80 /opt/google/earth/pro/plugins/printsupport 72 /opt/google/earth/pro/plugins/audio 18892 /opt/google/earth/pro/plugins 244252 /opt/google/earth/pro 244256 /opt/google/earth 2828 /opt/google/chrome/swiftshader 10140 /opt/google/chrome/WidevineCdm/_platform_specific/linux_x64 10144 /opt/google/chrome/WidevineCdm/_platform_specific 10156 /opt/google/chrome/WidevineCdm 20 /opt/google/chrome/cron
ステップ3–ディレクトリの合計サイズを表示する
-cオプションを指定してduコマンドを使用すると、各ファイルのディスク使用量の概要を合計サイズで表示できます。
du -c /opt
次の出力が表示されます。
440 /opt/google/earth/pro/shaders 8804 /opt/google/earth/pro/resources/gdal 16 /opt/google/earth/pro/resources/flightsim/aircraft 12 /opt/google/earth/pro/resources/flightsim/hud 56 /opt/google/earth/pro/resources/flightsim/controller 12 /opt/google/earth/pro/resources/flightsim/keyboard 8 /opt/google/earth/pro/resources/flightsim/planet 112 /opt/google/earth/pro/resources/flightsim 12660 /opt/google/earth/pro/resources 15220 /opt/google/earth/pro/lang 1180 /opt/google/earth/pro/plugins/imageformats 468 /opt/google/earth/pro/plugins/bearer 396 /opt/google/earth/pro/plugins/platforms 80 /opt/google/earth/pro/plugins/printsupport 72 /opt/google/earth/pro/plugins/audio 18892 /opt/google/earth/pro/plugins 244252 /opt/google/earth/pro 244256 /opt/google/earth 2828 /opt/google/chrome/swiftshader 10140 /opt/google/chrome/WidevineCdm/_platform_specific/linux_x64 10144 /opt/google/chrome/WidevineCdm/_platform_specific 10156 /opt/google/chrome/WidevineCdm 20 /opt/google/chrome/cron 599088 /opt 599088 total
ステップ4–ディスク使用量を人間が読める形式で表示する
du -h /opt
次の出力が表示されます。
440K /opt/google/earth/pro/shaders 8.6M /opt/google/earth/pro/resources/gdal 16K /opt/google/earth/pro/resources/flightsim/aircraft 12K /opt/google/earth/pro/resources/flightsim/hud 56K /opt/google/earth/pro/resources/flightsim/controller 12K /opt/google/earth/pro/resources/flightsim/keyboard 8.0K /opt/google/earth/pro/resources/flightsim/planet 112K /opt/google/earth/pro/resources/flightsim 13M /opt/google/earth/pro/resources 15M /opt/google/earth/pro/lang 1.2M /opt/google/earth/pro/plugins/imageformats 468K /opt/google/earth/pro/plugins/bearer 396K /opt/google/earth/pro/plugins/platforms 80K /opt/google/earth/pro/plugins/printsupport 72K /opt/google/earth/pro/plugins/audio 19M /opt/google/earth/pro/plugins 239M /opt/google/earth/pro 239M /opt/google/earth 2.8M /opt/google/chrome/swiftshader 10M /opt/google/chrome/WidevineCdm/_platform_specific/linux_x64 10M /opt/google/chrome/WidevineCdm/_platform_specific 10M /opt/google/chrome/WidevineCdm 20K /opt/google/chrome/cron 16K /opt/google/chrome/MEIPreload 21M /opt/google/chrome/locales 92K /opt/google/chrome/default_apps 255M /opt/google/chrome 493M /opt/google
ステップ5–ディレクトリの合計ファイルサイズを表示する
-sオプションを使用して、ディレクトリの合計サイズのみを表示できます。
du -hs /opt
次の出力が表示されます。
586M /opt
ステップ6–ファイルとディレクトリをサイズで並べ替える
並べ替えでduコマンドを使用すると、すべてのファイルとディレクトリをサイズで表示および並べ替えることができます。
du /opt | sort -n -r
次の出力が表示されます。
599088 /opt 504580 /opt/google 260320 /opt/google/chrome 244256 /opt/google/earth 244252 /opt/google/earth/pro 94268 /opt/ffmpeg 58604 /opt/ffmpeg/bin 28400 /opt/ffmpeg/lib 21360 /opt/google/chrome/locales 18892 /opt/google/earth/pro/plugins 15220 /opt/google/earth/pro/lang 12660 /opt/google/earth/pro/resources 10156 /opt/google/chrome/WidevineCdm 10144 /opt/google/chrome/WidevineCdm/_platform_specific 10140 /opt/google/chrome/WidevineCdm/_platform_specific/linux_x64 8804 /opt/google/earth/pro/resources/gdal 5972 /opt/ffmpeg/share 5668 /opt/ffmpeg/share/doc 5664 /opt/ffmpeg/share/doc/ffmpeg 2828 /opt/google/chrome/swiftshader 1288 /opt/ffmpeg/include
ステップ7–最大のファイルまたはディレクトリを見つける
ソートでduコマンドを使用して、システム内で最大のファイルまたはディレクトリを検索します。
-aオプションを指定してduコマンドを実行し、最大のファイルとディレクトリを見つけて表示します。
du -a / | sort -n -r | head -n 10
次の出力が表示されます。
1653740 / 931232 /usr 483812 /swapfile 425916 /usr/lib 268820 /usr/src 164476 /usr/share 156912 /var 151216 /usr/lib/x86_64-linux-gnu 142644 /var/lib 137388 /usr/lib/modules
結論
このガイドでは、さまざまなオプションを使用してファイルとディレクトリのサイズを確認する方法について説明しました。これは、Linuxでのファイルシステムの使用状況を追跡するのに役立ちます。 Atlantic.NetからVPSホスティングを始めましょう。