在 Linux 世界中,检查磁盘空间是任何系统管理员的一项基本任务。了解有多少可用空间及其使用方式对于保持系统平稳运行和避免潜在的数据丢失或停机至关重要。幸运的是,Linux 提供了几种用于检查磁盘空间的工具和方法,包括命令行实用程序和图形磁盘使用情况分析器。在这篇文章中,我们将探讨在 Linux 上检查磁盘空间的最常见和最有效的方法,包括 df 和 du 命令,以及一些流行的 GUI 磁盘使用情况分析器。我们还将介绍一些监视磁盘空间、优化使用情况和避免常见陷阱的最佳实践。因此,无论您是 Linux 新手还是经验丰富的专业人士,请继续阅读以了解如何检查 Linux 上的磁盘空间并保持系统平稳运行。
检查 Linux 上的磁盘空间
- 使用 df 命令。
命令“disk free”是一种流行的命令行实用程序,用于显示文件系统上的可用和已用磁盘空间量。这是在 Linux 上检查磁盘空间的一种简单有效的方法。df
要使用 df 命令,请打开终端窗口并键入以下命令:
<span class="pln">df </span><span class="pun">-</span><span class="pln">h</span>
输出:
<span class="typ">Filesystem</span> <span class="typ">Size</span> <span class="typ">Used</span> <span class="typ">Avail</span> <span class="typ">Use</span><span class="pun">%</span> <span class="typ">Mounted</span><span class="pln"> on </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda1 </span><span class="lit">512G</span> <span class="lit">46G</span> <span class="lit">466G</span> <span class="lit">14</span><span class="pun">%</span> <span class="pun">/</span><span class="pln"> none </span><span class="lit">4.0K</span> <span class="lit">0</span> <span class="lit">4.0K</span> <span class="lit">0</span><span class="pun">%</span> <span class="str">/sys/</span><span class="pln">fs</span><span class="pun">/</span><span class="pln">cgroup udev </span><span class="lit">1.7G</span> <span class="lit">6.0K</span> <span class="lit">1.9G</span> <span class="lit">1</span><span class="pun">%</span> <span class="pun">/</span><span class="pln">dev tmpfs </span><span class="lit">384M</span> <span class="lit">6.7M</span> <span class="lit">379M</span> <span class="lit">2</span><span class="pun">%</span> <span class="pun">/</span><span class="pln">run none </span><span class="lit">8.0M</span> <span class="lit">0</span> <span class="lit">5.0M</span> <span class="lit">0</span><span class="pun">%</span> <span class="str">/run/</span><span class="kwd">lock</span><span class="pln"> none </span><span class="lit">1.9G</span> <span class="lit">160M</span> <span class="lit">1.8G</span> <span class="lit">9</span><span class="pun">%</span> <span class="str">/run/</span><span class="pln">shm none </span><span class="lit">101M</span> <span class="lit">48K</span> <span class="lit">101M</span> <span class="lit">1</span><span class="pun">%</span> <span class="str">/run/</span><span class="pln">user </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">sda5 </span><span class="lit">179G</span> <span class="lit">36G</span> <span class="lit">135G</span> <span class="lit">21</span><span class="pun">%</span> <span class="pun">/</span><span class="typ">Data</span> <span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">loop0 </span><span class="lit">92M</span> <span class="lit">92M</span> <span class="lit">0</span> <span class="lit">100</span><span class="pun">%</span> <span class="str">/snap/</span><span class="pln">core</span><span class="pun">/</span><span class="lit">8592</span> <span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">loop1 </span><span class="lit">174M</span> <span class="lit">174M</span> <span class="lit">0</span> <span class="lit">100</span><span class="pun">%</span> <span class="str">/snap/</span><span class="pln">skype</span><span class="pun">/</span><span class="lit">112</span> <span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">loop2 </span><span class="lit">95M</span> <span class="lit">95M</span> <span class="lit">0</span> <span class="lit">100</span><span class="pun">%</span> <span class="str">/snap/</span><span class="pln">core</span><span class="pun">/</span><span class="lit">8689</span> <span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">loop3 </span><span class="lit">43M</span> <span class="lit">43M</span> <span class="lit">0</span> <span class="lit">100</span><span class="pun">%</span> <span class="str">/snap/</span><span class="pln">gtk</span><span class="pun">-</span><span class="pln">common</span><span class="pun">-</span><span class="pln">themes</span><span class="pun">/</span><span class="lit">1440</span> <span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">loop4 </span><span class="lit">52M</span> <span class="lit">55M</span> <span class="lit">0</span> <span class="lit">100</span><span class="pun">%</span> <span class="str">/snap/</span><span class="pln">core18</span><span class="pun">/</span><span class="lit">1668</span> <span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">loop5 </span><span class="lit">164M</span> <span class="lit">164M</span> <span class="lit">0</span> <span class="lit">100</span><span class="pun">%</span> <span class="str">/snap/</span><span class="pln">skype</span><span class="pun">/</span><span class="lit">115</span> <span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">loop6 </span><span class="lit">141M</span> <span class="lit">141M</span> <span class="lit">0</span> <span class="lit">100</span><span class="pun">%</span> <span class="str">/snap/</span><span class="pln">gnome</span><span class="pun">-</span><span class="lit">3</span><span class="pun">-</span><span class="lit">28</span><span class="pun">-</span><span class="lit">1804</span><span class="pun">/</span><span class="lit">116</span> <span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="pln">loop7 </span><span class="lit">57M</span> <span class="lit">57M</span> <span class="lit">0</span> <span class="lit">100</span><span class="pun">%</span> <span class="str">/snap/</span><span class="pln">teams</span><span class="pun">-</span><span class="kwd">for</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">/</span><span class="lit">79</span>
“” 选项以人类可读的格式显示磁盘使用情况。这样可以更轻松地理解输出。将 “” 替换为目录,您要检查磁盘空间。-h
/path/to/directory
- GUI 磁盘使用情况分析器
GUI 磁盘使用情况分析器提供磁盘使用情况的可视化表示形式,并允许用户浏览文件系统并查看每个目录和文件使用了多少空间。一些流行的 Linux GUI 磁盘使用情况分析器包括:
- 猴面包树(GNOME 桌面环境)
- KDirStat (KDE Desktop Environment)
- Filelight (KDE Desktop Environment)
GUI 磁盘使用情况分析器的优点是提供磁盘使用情况的图形表示形式,使其更易于解释。但是,它们可能并非在所有系统上都可用,并且可能无法提供与 df 和 du 命令相同的详细级别。
检查磁盘空间的最佳做法
建议定期检查磁盘空间,最好每周一次。这将帮助您在潜在问题成为问题之前识别它们。
检查磁盘空间时,必须查找关键文件系统(如根文件系统)上的磁盘空间不足。您还应该查找可以移动到外部硬盘驱动器或完全删除的大文件或目录。这将有助于释放系统上的空间并确保它继续正常运行。
管理磁盘空间的提示
为了有效地管理磁盘空间,建议:
- 定期删除不必要的文件或目录:删除不再需要的文件和目录可以释放大量磁盘空间。
- 考虑将大文件或目录移动到外部硬盘驱动器:将日常不需要的大文件或目录移动到外部硬盘驱动器可以释放系统上的空间。
- 使用压缩:压缩文件有助于节省磁盘空间。这对于很少访问的文件特别有用。
- 定期监视磁盘使用情况:定期监视磁盘使用情况将帮助您识别磁盘使用情况的任何突然增加。这可能是系统潜在问题的早期指标。
总之,检查 Linux 上的磁盘空间是一项基本任务,应定期执行以确保系统和应用程序正常运行。df 命令、du 命令和 GUI 磁盘使用率分析器是可用于检查 Linux 上磁盘空间的三种不同方法。GUI 磁盘使用情况分析器提供磁盘使用情况的图形表示,而 df 和 du 命令提供磁盘使用情况的详细摘要。为了有效地管理磁盘空间,建议定期检查磁盘空间,删除不必要的文件或目录,并定期监控磁盘使用情况。通过遵循这些最佳实践,用户可以确保他们的 Linux 系统平稳高效地运行。