Linux でファイルとディレクトリのディスク使用量を確認する方法
Linux の「du」 (ディスク使用量) は、マシン上のファイルやディレクトリのディスク使用量情報を確認するために使用される標準の Unix/Linux コマンドです。
du コマンドには、さまざまな形式で結果を取得するために使用できる多くのパラメータ オプションがあります。 du コマンドは、ファイルとディレクトリのサイズを再帰的に表示します。
この記事では、Linux のファイルとディレクトリのサイズを決定するのに役立つ便利な ‘du’ コマンドを例とともに説明します。この記事で提供される情報は、「du」コマンドのマニュアル ページから得られています。
Linux でディレクトリのサイズを確認する方法
1. ディレクトリ ツリー ‘/home/tecmint’ とそのサブディレクトリのディスク使用量の概要を取得するには、次のコマンドを使用します。
du /home/tecmint
4 /home/tecmint/bin
4 /home/tecmint/.ffbinaries-cache
672 /home/tecmint/.openjfx/cache/17.0.3-ea
676 /home/tecmint/.openjfx/cache
680 /home/tecmint/.openjfx
36 /home/tecmint/.ssh
92 /home/tecmint/.config/xfce4/xfconf/xfce-perchannel-xml
96 /home/tecmint/.config/xfce4/xfconf
12 /home/tecmint/.config/xfce4/desktop
...
56758808 /home/tecmint
上記のコマンドの出力には、/home/tecmint ディレクトリ内のディスク ブロックの数とそのサブディレクトリが表示されます。
人間が判読できる形式でディレクトリ サイズを確認する方法
2. 「du」 コマンドで 「-h」
オプションを使用すると、「人間が読める形式」で結果を取得できます。形式」を使用すると、バイト、キロバイト、メガバイト、ギガバイトなどの単位でサイズを表示できます。
du -h /home/tecmint
4.0K /home/tecmint/bin
4.0K /home/tecmint/.ffbinaries-cache
672K /home/tecmint/.openjfx/cache/17.0.3-ea
676K /home/tecmint/.openjfx/cache
680K /home/tecmint/.openjfx
36K /home/tecmint/.ssh
92K /home/tecmint/.config/xfce4/xfconf/xfce-perchannel-xml
96K /home/tecmint/.config/xfce4/xfconf
12K /home/tecmint/.config/xfce4/desktop
...
55G /home/tecmint
Linux でディレクトリの合計サイズを確認する方法
3. ディレクトリの合計ディスク使用量の概要を取得するには、次のように '-s'
オプションを使用します。
du -sh /home/tecmint
55G /home/tecmint
4. 「du」 コマンドで 「-a」
フラグを使用すると、すべてのファイルとディレクトリのディスク使用量が表示されます。
du -a /home/tecmint
4 /home/tecmint/fix-deb-src-error-ubuntu
4 /home/tecmint/tecmint.py
0 /home/tecmint/bin/FoxitReader
4 /home/tecmint/bin
4 /home/tecmint/.ffbinaries-cache
52 /home/tecmint/Eclipse-with-PyDev.jpg
300 /home/tecmint/Screenshot from 2023-07-27 14-02-45.png
16 /home/tecmint/.openjfx/cache/17.0.3-ea/libglass.so
28 /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font_pango.so
20 /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font.so
64 /home/tecmint/.openjfx/cache/17.0.3-ea/libprism_es2.so
512 /home/tecmint/.openjfx/cache/17.0.3-ea/libglassgtk3.so
...
56759528 /home/tecmint
5. '-a'
フラグを '-h'
とともに使用すると、すべてのファイルとフォルダのディスク使用量が人間が判読できる形式で表示されます。フォーマット。結果の出力は、ファイル サイズがキロバイト、メガバイトなどの単位で表示されるため、理解しやすくなります。
du -ah /home/tecmint
4.0K /home/tecmint/fix-deb-src-error-ubuntu
4.0K /home/tecmint/tecmint.py
28K /home/tecmint/download.php?file=%2Foomph%2Fepp%2F2023-06%2FR%2Feclipse-inst-jre-linux64.tar.gz.1
0 /home/tecmint/bin/FoxitReader
4.0K /home/tecmint/bin
4.0K /home/tecmint/.ffbinaries-cache
52K /home/tecmint/Eclipse-with-PyDev.jpg
300K /home/tecmint/Screenshot from 2023-07-27 14-02-45.png
16K /home/tecmint/.openjfx/cache/17.0.3-ea/libglass.so
28K /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font_pango.so
20K /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font.so
64K /home/tecmint/.openjfx/cache/17.0.3-ea/libprism_es2.so
512K /home/tecmint/.openjfx/cache/17.0.3-ea/libglassgtk3.so
...
55G /home/tecmint
6. ディレクトリ ツリーとそのサブディレクトリのディスク使用量を キロバイト ブロック単位で確認するには、'-k'
オプションを使用します (サイズを単位で表示します)。 1024バイト単位)。
du -k /home/tecmint
4 /home/tecmint/bin
4 /home/tecmint/.ffbinaries-cache
672 /home/tecmint/.openjfx/cache/17.0.3-ea
676 /home/tecmint/.openjfx/cache
680 /home/tecmint/.openjfx
8 /home/tecmint/.uk.co.screamingfrog.seospider.ui.b/webview
12 /home/tecmint/.uk.co.screamingfrog.seospider.ui.b
36 /home/tecmint/.ssh
92 /home/tecmint/.config/xfce4/xfconf/xfce-perchannel-xml
96 /home/tecmint/.config/xfce4/xfconf
12 /home/tecmint/.config/xfce4/desktop
4 /home/tecmint/.config/xfce4/xfwm4
8 /home/tecmint/.config/xfce4/panel/launcher-18
8 /home/tecmint/.config/xfce4/panel/launcher-17
8 /home/tecmint/.config/xfce4/panel/launcher-14
8 /home/tecmint/.config/xfce4/panel/launcher-3
8 /home/tecmint/.config/xfce4/panel/launcher-16
8 /home/tecmint/.config/xfce4/panel/launcher-5
...
56769128 /home/tecmint
7. サブディレクトリを含むディレクトリ ツリーのディスク使用量の概要をメガバイト (MB) 単位でのみ取得するには、'-mh'
オプションは次のようになります。 '-m'
フラグはブロックを MB 単位でカウントし、'-h'
は人間が読める形式を表します。
du -mh /home/tecmint
4.0K /home/tecmint/bin
4.0K /home/tecmint/.ffbinaries-cache
672K /home/tecmint/.openjfx/cache/17.0.3-ea
676K /home/tecmint/.openjfx/cache
680K /home/tecmint/.openjfx
8.0K /home/tecmint/.uk.co.screamingfrog.seospider.ui.b/webview
12K /home/tecmint/.uk.co.screamingfrog.seospider.ui.b
36K /home/tecmint/.ssh
92K /home/tecmint/.config/xfce4/xfconf/xfce-perchannel-xml
96K /home/tecmint/.config/xfce4/xfconf
12K /home/tecmint/.config/xfce4/desktop
4.0K /home/tecmint/.config/xfce4/xfwm4
8.0K /home/tecmint/.config/xfce4/panel/launcher-18
8.0K /home/tecmint/.config/xfce4/panel/launcher-17
8.0K /home/tecmint/.config/xfce4/panel/launcher-14
8.0K /home/tecmint/.config/xfce4/panel/launcher-3
...
55G /home/tecmint
8. '-c'
フラグは、最後の行にディスク容量の使用量の総計を示します。ディレクトリが 55GB のスペースを占有している場合、出力の最後の 2 行は次のように表示されます。
du -ch /home/tecmint
4.0K /home/tecmint/bin
4.0K /home/tecmint/.ffbinaries-cache
672K /home/tecmint/.openjfx/cache/17.0.3-ea
676K /home/tecmint/.openjfx/cache
680K /home/tecmint/.openjfx
8.0K /home/tecmint/.uk.co.screamingfrog.seospider.ui.b/webview
12K /home/tecmint/.uk.co.screamingfrog.seospider.ui.b
36K /home/tecmint/.ssh
92K /home/tecmint/.config/xfce4/xfconf/xfce-perchannel-xml
96K /home/tecmint/.config/xfce4/xfconf
12K /home/tecmint/.config/xfce4/desktop
4.0K /home/tecmint/.config/xfce4/xfwm4
8.0K /home/tecmint/.config/xfce4/panel/launcher-18
8.0K /home/tecmint/.config/xfce4/panel/launcher-17
8.0K /home/tecmint/.config/xfce4/panel/launcher-14
8.0K /home/tecmint/.config/xfce4/panel/launcher-3
8.0K /home/tecmint/.config/xfce4/panel/launcher-16
...
55G /home/tecmint
55G total
duコマンドでファイルを除外する方法
9. 次のコマンドは、指定されたパターンに一致するファイルを除く、すべてのファイルとディレクトリのディスク使用量を計算して表示します。
提供された例では、コマンドはディレクトリの合計サイズを計算するときに '.txt'
ファイルを除外します。この方法では、'--exclude'
フラグを使用して特定のファイル形式を除外できます。
出力を確認すると、'.txt'
ファイルのエントリがないことがわかります。
du -ah --exclude="*.txt" /home/tecmint
4.0K /home/tecmint/fix-deb-src-error-ubuntu
4.0K /home/tecmint/tecmint.py
0 /home/tecmint/bin/FoxitReader
4.0K /home/tecmint/bin
4.0K /home/tecmint/.ffbinaries-cache
52K /home/tecmint/Eclipse-with-PyDev.jpg
300K /home/tecmint/Screenshot from 2023-07-27 14-02-45.png
16K /home/tecmint/.openjfx/cache/17.0.3-ea/libglass.so
28K /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font_pango.so
20K /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font.so
64K /home/tecmint/.openjfx/cache/17.0.3-ea/libprism_es2.so
512K /home/tecmint/.openjfx/cache/17.0.3-ea/libglassgtk3.so
28K /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font_freetype.so
672K /home/tecmint/.openjfx/cache/17.0.3-ea
...
55G /home/tecmint
変更時間ごとにディレクトリの使用状況を確認する方法
10. 変更時間に基づいてディスク使用量を表示するには、以下に示すように '--time'
フラグを使用します。
du -ha --time /home/tecmint
4.0K 2023-07-06 13:15 /home/tecmint/fix-deb-src-error-ubuntu
4.0K 2023-06-14 12:50 /home/tecmint/tecmint.py
0 2021-12-07 12:26 /home/tecmint/bin/FoxitReader
4.0K 2021-12-07 12:26 /home/tecmint/bin
4.0K 2022-10-10 11:02 /home/tecmint/.ffbinaries-cache
52K 2023-05-12 12:09 /home/tecmint/Eclipse-with-PyDev.jpg
300K 2023-07-27 14:02 /home/tecmint/Screenshot from 2023-07-27 14-02-45.png
16K 2022-10-01 12:41 /home/tecmint/.openjfx/cache/17.0.3-ea/libglass.so
28K 2022-10-01 12:41 /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font_pango.so
20K 2022-10-01 12:41 /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font.so
64K 2022-10-01 12:41 /home/tecmint/.openjfx/cache/17.0.3-ea/libprism_es2.so
512K 2022-10-01 12:41 /home/tecmint/.openjfx/cache/17.0.3-ea/libglassgtk3.so
28K 2022-10-01 12:41 /home/tecmint/.openjfx/cache/17.0.3-ea/libjavafx_font_freetype.so
672K 2022-10-01 12:41 /home/tecmint/.openjfx/cache/17.0.3-ea
676K 2022-10-01 12:41 /home/tecmint/.openjfx/cache
...
55G 2023-08-14 10:22 /home/tecmint
du コマンドは多用途であり、さまざまなオプションやフラグを使用してニーズに合わせて調整できます。そのオプションと使用法の詳細については、ターミナルで man du
を使用してください。