Popular blog tags

Ubuntu命令大全:磁盘空间清理

Published

Table of Content

查看Linux系统环境

Ubuntu磁盘空间清理

1.Ubuntu 查看磁盘空间大小命令

df -hl

Df命令是linux系统以磁盘分区为单位查看文件系统,可以加上参数查看磁盘剩余空间信息.

显示结果为:

文件系统              容量 已用 可用 已用% 挂载点 
Filesystem            Size Used Avail Use% Mounted on
/dev/hda2              45G   19G   24G 44% /
/dev/hda1             494M   19M 450M   4% /boot
/dev/hda6             4.9G 2.2G 2.5G 47% /home
/dev/hda5             9.7G 2.9G 6.4G 31% /opt
none                 1009M     0 1009M   0% /dev/shm
/dev/hda3             9.7G 7.2G 2.1G 78% /usr/local
/dev/hdb2              75G   75G     0 100% /
/dev/hdb2              75G   75G     0 100% /

上面的输出表示的意思为:

HD硬盘接口的第二个硬盘(b),第二个分区(2),容量是75G,用了75G,可用是0,因此利用率是100%, 被挂载到根分区目录上(/)

查看目录占用空间 

#du -hs 

#du -hs 目录名

 

Ubuntu磁盘空间清理

step 1:首先确定是哪个文件夹占用较多

du -h / --max-depth=1 | sort -hr | head -n 10

or

cd /
du -sh *

output

50G /
28G    /var
6.0G   /dfs
4.0G   /opt
3.0G   /root
2.3G   /usr
148M   /lib
39M    /log
30M    /etc
29M    /boot

 step 2:继续往下层目录找,定位到/var目录占用超过50%(磁盘总量50G) 

du -h /var --max-depth=1 | sort -hr | head -n 10

output

28G    /var
24G    /var/lib
3.8G   /var/log
371M   /var/cache

删除指定目录

rm -rf test/

ubuntu找出大文件并删除

# du -t 100M
output
# du -t 100M
505472	./.local/share/NuGet/v3-cache/1ca707a4d90792ce8e42453d4e350886a0fdaa4d$ps:_api.nuget.org_v3_index.json
509408	./.local/share/NuGet/v3-cache
509412	./.local/share/NuGet
509420	./.local/share
509424	./.local
172940	./.nuget/packages/system.runtime
224236	./.nuget/packages/skiasharp/1.68.1.1
224240	./.nuget/packages/skiasharp
2670532	./.nuget/packages
2670544	./.nuget
181412	./dotnet_install/dotnet_packages
362848	./dotnet_install

 

Free disk space

1.找出 /var/tmp 目录下 超过10M的大文件并删除

find /var/tmp -type f -size +10000k -delete

 

 2.Linux 系统清理 /var/log/journal/ 垃圾日志

step 1:用journalctl --disk-usage 命令查看

# journalctl --disk-usage
Archived and active journals take up 2.4G in the file system.

发现/var/log/journal日志文件占用了近3G空间,每个日志文件体积高达8-128M,这些日志文件记录了很长时间以来的systemd情况,毫无价值

step 2:用journalctl --vacuum-size=10M命令将其清理

#journalctl --vacuum-size=10M

Output

journalctl --vacuum-size=10M
Vacuuming done, freed 0B of archived journals from /var/log/journal.
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/b9ca2ef9b.journal (104.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/d35bb592c.journal (88.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/2b1bf8237.journal (72.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/0a0db1bdf.journal (80.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/5edd0703c.journal (104.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/9898ebb20.journal (104.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/45ad34b24.journal (80.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/22dc301dd.journal (80.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/~ (16.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/~ (8.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/~ (8.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/~ (8.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/287d11b86.journal (104.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/d80fcc771.journal (104.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/fcb956bd1.journal (104.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/6c7baf9f3.journal (48.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/5810d631e.journal (8.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/65b5aa90b.journal (88.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/e0ff52a1a.journal (88.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/3bcf36c45.journal (96.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/82ffcb305.journal (88.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/841f8d073.journal (88.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/87a436d4f.journal (96.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/9f3c96beb.journal (88.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/d956ebf11.journal (88.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/25c44c55b.journal (88.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/3533ae1f3.journal (88.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/5c8677825.journal (88.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/a042c8c99.journal (96.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/83cdc6fa5.journal (88.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/3758c8ded.journal (80.0M).
Deleted archived journal /var/log/journal/9371d6f120e047adb899dcdeab12e0cb/4e3108ed8.journal (88.0M).
Vacuuming done, freed 2.3G of archived journals from /var/log/journal/9371d6f120e047adb899dcdeab12e0cb.

 

用journalctl --vacuum-size=10M命令将其清理之后,腾出了2.7G的空间。用df命令一查,/根分区果然宽敞了很多。

# df
Filesystem     1K-blocks    Used Available Use% Mounted on
udev              472776       0    472776   0% /dev
tmpfs             100876     700    100176   1% /run
/dev/vda1       25746000 8297348  16121616  34% /
tmpfs             504360       0    504360   0% /dev/shm
tmpfs               5120       0      5120   0% /run/lock
tmpfs             504360       0    504360   0% /sys/fs/cgroup
tmpfs             100872       4    100868   1% /run/user/0

清理后
# df
Filesystem     1K-blocks    Used Available Use% Mounted on
udev              472776       0    472776   0% /dev
tmpfs             100876     700    100176   1% /run
/dev/vda1       25746000 5872448  18546516  25% /
tmpfs             504360       0    504360   0% /dev/shm
tmpfs               5120       0      5120   0% /run/lock
tmpfs             504360       0    504360   0% /sys/fs/cgroup
tmpfs             100872       4    100868   1% /run/user/0



3. clean /var/log/elasticsearch

 

4. remove files in in /var/cache/apt/archives 

When running sudo apt full-upgrade, it will show how much data will be downloaded and how much space it will take up on the SD card. It’s worth checking with df -h that you have enough free disk space, as unfortunately apt will not do this for you. Also be aware that downloaded package files (.deb files) are kept in /var/cache/apt/archives. You can remove these in order to free up space with sudo apt clean (sudo apt-get clean in older releases of apt).

df -h

 sudo apt-get clean

:~/dotnet_install# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            2.0G     0  2.0G   0% /dev
tmpfs           395M  3.0M  392M   1% /run
/dev/vda1        40G   28G  9.6G  75% /
tmpfs           2.0G   16K  2.0G   1% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup
tmpfs           395M     0  395M   0% /run/user/0
/dev/loop4       62M   62M     0 100% /snap/core20/1328
/dev/loop5       44M   44M     0 100% /snap/certbot/1788
/dev/loop6       44M   44M     0 100% /snap/snapd/14978
/dev/loop1       62M   62M     0 100% /snap/core20/1361
/dev/loop2       44M   44M     0 100% /snap/certbot/1842

:~/dotnet_install#  sudo apt-get clean

:~/dotnet_install# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            2.0G     0  2.0G   0% /dev
tmpfs           395M  3.0M  392M   1% /run
/dev/vda1        40G   28G   11G  73% /
tmpfs           2.0G   16K  2.0G   1% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup
tmpfs           395M     0  395M   0% /run/user/0
/dev/loop4       62M   62M     0 100% /snap/core20/1328
/dev/loop5       44M   44M     0 100% /snap/certbot/1788
/dev/loop6       44M   44M     0 100% /snap/snapd/14978

2.Ubuntu控制台输出结果到文本

ls>t.txt

python test.py | tee output.txt

 

Linux磁盘空间100% 查找磁盘空间并删除大文件

 

3.How do I provide a username and password to wget?

Syntax

The syntax is:

wget options url

 

4.查看进程和进程端口号

netstat -ntlp

显示结果如下:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 27555/sshd
tcp6 0 0 :::22 :::* LISTEN 27555/sshd

5.服务管理

查看启用状态,用于启动失败排错,显示启动错误详细信息

journalctl -e -u n.service

ubuntu中搜索文件或文件夹的方法

find / -name +文件名

find是在指定的目录下遍历查找,如果目录使用 / 则表示在所有目录下查找,find方式查找文件消耗资源比较大,速度也慢一点。

~# find / -name 6273713-03c88b78cd55ae49.png

 

更新系统

# 更新本地报数据库
sudo apt update

# 更新所有已安装的包(也可以使用 full-upgrade)
sudo apt upgrade

# 自动移除不需要的包
sudo apt autoremove

常用的清理命令:

命令描述
apt autoclean将已删除软件包的.deb安装文件从硬盘中删除
apt clean同上,但会把已安装的软件包的安装包也删除掉
apt autoremove删除为了满足其他软件包的依赖而安装,但现在不再需要的软件包
apt remove [软件包名]删除已安装的软件包(保留配置文件)
apt --purge remove [软件包名]删除已安装包(不保留配置文件)

 

Linux系统环境(Ubuntu)

[ ~]# cat /etc/redhat-release                       #==》系统版本
CentOS release 6.7 (Final)
[ ~]# uname –r                                      #==》内核版本
2.6.32-573.el6.x86_64
[ ~]# uname -m                                      #==》系统架构
x86_64
[ ~]# echo $LANG                                    #==》系统字符集
en_US.UTF-8
[ conf]# nginx -V                                     #==》Nginx版本
nginx version: nginx/1.14.0 (Ubuntu)
built with OpenSSL 1.1.1  11 Sep 2018
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-DUghaW/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module