df -h
选项说明:
- -h
:以人类可读格式显示(GB/MB)
- -T
:显示文件系统类型
- -i
:显示inode使用情况
du -sh /path/to/directory # 查看目录总大小
du -h --max-depth=1 /path # 查看目录下一级子目录大小
ls -lh file # 查看单个文件大小
find / -type f -size +100M -exec ls -lh {} \; # 查找大于100M的文件
find / -type f -size +1G # 查找大于1G的文件
journalctl --vacuum-size=200M # 限制系统日志为200MB
find /var/log -type f -name "*.log" -exec truncate -s 0 {} \; # 清空日志文件
# Debian/Ubuntu
apt-get clean
apt-get autoremove
# RHEL/CentOS
yum clean all
dnf clean all
# Ubuntu/Debian
apt-get purge $(dpkg -l | awk '/^ii linux-image-/{print $2}' | grep -v $(uname -r))
# RHEL/CentOS
package-cleanup --oldkernels --count=1
rm -rf /tmp/*
rm -rf ~/.cache/*
# 创建物理卷
pvcreate /dev/sdb
# 创建卷组
vgcreate vg_data /dev/sdb
# 创建逻辑卷
lvcreate -L 100G -n lv_data vg_data
# 扩展逻辑卷
lvextend -L +50G /dev/vg_data/lv_data
resize2fs /dev/vg_data/lv_data # 对于ext文件系统
# 检查并修复文件系统
fsck /dev/sda1
# 优化ext4文件系统
tune2fs -o journal_data_writeback /dev/sda1
tune2fs -m 1 /dev/sda1 # 保留1%的空间给root
# 创建压缩文件系统
mkfs.btrfs -m single -d single /dev/sdb1
# 压缩现有目录
tar -czvf archive.tar.gz /path/to/directory
设置logrotate自动轮转日志:
vim /etc/logrotate.conf
示例配置:
/var/log/app/*.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
create 640 root adm
sharedscripts
postrotate
/usr/bin/killall -HUP rsyslogd
endscript
}
# 添加到crontab中定期检查
*/30 * * * * df -h | awk '$5 > 90 {print $1 " is almost full: " $5}' | mail -s "Disk Space Alert" admin@example.com
mount -t overlay overlay -o lowerdir=/lower,upperdir=/upper,workdir=/work /merged
mksquashfs /source /destination/image.sqsh
mount /destination/image.sqsh /mount/point -t squashfs -o loop
btrfs filesystem defragment -r -v /path
btrfs filesystem du /path
通过以上方法,您可以有效地管理和优化Linux系统的磁盘空间,确保系统高效稳定运行。