清理Linux的磁盘使用空间
硬盘虽然并不值钱,但对于一个强迫症来说,看到莫名其妙地内容占据了好些空间也非常不爽。记录下在Linux中如何发现空间占用大户,以及可以删除什么以缩减使用空间。
aptitude
sudo apt autoremove
可以自动清理不再需要的文件,不过作用有限。aptitude是个好工具,可以在文本界面下方便地浏览已安装的包,删除不需要的内容。aptitude将包进行了分类,可以按类查找,甚至可以将某类包整个都删除。比如对server来说,desktop和GUI并不是必须的,可以将gnome相关的包都删除以节省空间。
Actions Undo Package Resolver Search Options Views Help
C-T: Menu ?: Help q: Quit u: Update g: Preview/Download/Install/Remove Pkgs
aptitude 0.8.10 @ DEAN
--- Upgradable Packages (1)
--- New Packages (167)
--\ Installed Packages (1900)
--- admin Administrative utilities (install software, manage users, etc) (117)
--- comm Programs for faxmodems and other communication devices (2)
--- database Database servers and tools (2)
--- debug Debugging symbols (3)
--- devel Utilities and programs for software development (55)
--- doc Documentation and specialized programs for viewing documentation (8)
--- editors Text editors and word processors (14)
--- fonts Fonts and font utilities (98)
--- gnome The GNOME Desktop Environment (4)
--- graphics Utilities to create, view, and edit graphics files (6)
--- httpd Webservers and their modules (9)
--- interpreters Interpreters for interpreted languages (7)
Packages in the 'editors' section allow you to edit plain ASCII text. These are not necessarily word processors, although some word
processors may be found in this section.
This group contains 14 packages.
ncdu
ncdu是个好东西,可以快速分析出磁盘中的大文件所在位置,以便于有针对性地清理。可以先用sudo ncdu /
来分析全盘的使用情况,如果磁盘比较大,扫描可能会比较慢:
ncdu 1.12 ~ Use the arrow keys to navigate, press ? for help
--- / ------------------------------------------------------------------------------------------------------------------------
7.7 GiB [##########] /var
5.8 GiB [####### ] /data
4.7 GiB [###### ] /usr
1.3 GiB [# ] /lib
562.8 MiB [ ] /opt
293.4 MiB [ ] /boot
...
可以根据上述分析结果进入到具体条目中,看看到底是哪些文件占了比较大的空间,如无必要,删除之。
比较重要的两个键位,一个是-
,标记某个类别或某个包将被删除,g
执行已标记的操作。
清理/usr/local/lib
进一步看/usr/local/lib
:
ncdu 1.12 ~ Use the arrow keys to navigate, press ? for help
--- /usr/local/lib -----------------------------------------------------------------------------------------------------------
/..
528.7 MiB [##########] libbitcoin-explorer.a
331.5 MiB [###### ] /python3.5
148.1 MiB [## ] libbitcoin-explorer.so.0.0.0
98.1 MiB [# ] libbitcoin-network.a
84.6 MiB [# ] libbitcoin.a
64.8 MiB [# ] /python2.7
39.8 MiB [ ] /node_modules
35.0 MiB [ ] libbitcoin-network.so.0.0.0
27.0 MiB [ ] libbitcoin.so.0.0.0
26.4 MiB [ ] libzmq.a
20.5 MiB [ ] libbitcoin-protocol.a
9.3 MiB [ ] libbitcoin-client.a
...
就本机为例,里面有许多libbitcoin相关的lib,而实际上它们已经不再使用了。不幸的是,并没有一个好工具清理/usr/local/lib
,因为此处就是放许多库文件的文件夹,如果不用apt/yum之类的包管理软件,系统很难知道哪些库文件是不再使用的,这是用户层面的逻辑。
清理/var/log/journal
先查看journal log有多大:
$ journalctl --disk-usage
Archived and active journals take up 5.8G in the file system.
用如下命令手动清理只保留最近10天的log: $ journalctl --vacuum-time=10d
Vacuuming done, freed 5.1G of archived journals from /var/log/journal.
最后可以编辑/etc/systemd/journald.conf
,设置最大jounal
log占用空间为1000M:
SystemMaxUse=1000M
清理conda
conda clean -a
是个好东西,按提示来即可,说明如下:
$ conda clean --help
usage: conda clean [-h] [-a] [-i] [-p] [-t] [-f]
[-c TEMPFILES [TEMPFILES ...]] [-d] [--json] [-q] [-v] [-y]
Remove unused packages and caches.
Options:
optional arguments:
-h, --help Show this help message and exit.
Removal Targets:
-a, --all Remove index cache, lock files, unused cache packages,
and tarballs.
清理/var/log/azure/
/var/log/azure/Microsoft.OSTCExtensions.LinuxDiagnostic
这目录里的日志文件还挺大,有不少讨论这个的:
https://github.com/Azure/azure-linux-extensions/issues/618
没什么好方法,用logrotate解决,在/etc/logrotate.d/
中增加个azurediagnostic文件:
/var/log/azure/Microsoft.OSTCExtensions.LinuxDiagnostic/*.log {
daily
rotate 3
size 100M
copytruncate
compress
delaycompress
missingok
}
手动先删除下该文件夹下已存在的文件即可。