Shell脚本删除自动清理超过大小的文件

目的:Linux下删除超过指定大小的文件

思路:

        使用dd命令来创建不同大小的文件

        使用不同的方式列出目录下的所有文件

        提取文件的大小,名称

        判断进行处理删除

1,使用dd命令来模拟实验环境 

格式:dd if=块文件存放位置  of=存放目录 bs=大小 count=个数

[root@localhost ~]# mkdir /file/
[root@localhost ~]# dd if=/dev/zero of=/file/1.txt bs=1M count=1
记录了1+0 的读入
记录了1+0 的写出
1048576字节(1.0 MB)已复制,0.000853269 秒,1.2 GB/秒
[root@localhost ~]# dd if=/dev/zero of=/file/2.txt bs=1M count=2
记录了2+0 的读入
记录了2+0 的写出
2097152字节(2.1 MB)已复制,0.00110556 秒,1.9 GB/秒
[root@localhost ~]# dd if=/dev/zero of=/file/3.txt bs=1M count=3
记录了3+0 的读入
记录了3+0 的写出
3145728字节(3.1 MB)已复制,0.00140054 秒,2.2 GB/秒
[root@localhost ~]# dd if=/dev/zero of=/file/4.txt bs=1M count=4
记录了4+0 的读入
记录了4+0 的写出
4194304字节(4.2 MB)已复制,0.0016842 秒,2.5 GB/秒
[root@localhost ~]# dd if=/dev/zero of=/file/5.txt bs=1M count=5
记录了5+0 的读入
记录了5+0 的写出
5242880字节(5.2 MB)已复制,0.0024222 秒,2.2 GB/秒
[root@localhost ~]# dd if=/dev/zero of=/file/6.txt bs=1M count=6
记录了6+0 的读入
记录了6+0 的写出
6291456字节(6.3 MB)已复制,0.00287438 秒,2.2 GB/秒
[root@localhost ~]# du -sh /file/*
1.0M	/file/1.txt
2.0M	/file/2.txt
3.0M	/file/3.txt
4.0M	/file/4.txt
5.0M	/file/5.txt
6.0M	/file/6.txt
[root@localhost ~]# ll -h /file/*
-rw-r--r--. 1 root root 1.0M 7月   7 07:55 /file/1.txt
-rw-r--r--. 1 root root 2.0M 7月   7 07:55 /file/2.txt
-rw-r--r--. 1 root root 3.0M 7月   7 07:55 /file/3.txt
-rw-r--r--. 1 root root 4.0M 7月   7 07:55 /file/4.txt
-rw-r--r--. 1 root root 5.0M 7月   7 07:55 /file/5.txt
-rw-r--r--. 1 root root 6.0M 7月   7 07:55 /file/6.txt

方法1:使用du -sh -b来计算大小进行处理: 

dd 选项:

-s:仅计算总数

-h: 以K,M,G为单位

-b:显示目录或文件以byte为单位

        1T=1024G
        1G=1024M
        1M=1024K
        1K=1024B(字节)

#提取文件的大小
[root@localhost ~]# du -sh -b /file/* | awk '{print $1}'
1048576
2097152
3145728
4194304
5242880
6291456

#提取文件的名称
[root@localhost ~]# du -sh /file/* | awk '{print $2}' | awk -F/ '{print $3}'
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt

编写脚本:

        思路:循环将第一个提取处理的大小赋予size变量,循环提取这个值的文件名,判断此文件的大小是否大于3MB,如果大于将此文件删除,给file变量赋予空值,嵌套循环结束,开始判断第二个文件,依次类推

#!/bin/bash
for size in $(du -sh -b /file/* | awk '{print $1}' )
do
        for file in $(du -sh -b /file/* | grep ${size} | awk '{print $2}' | awk -F/ '{print $3}')
        do
                if [ $size -gt 3145728 ] ; then
                        rm -rf /file/${file}
                        echo "${file}${size}"
                        echo "" >> $file
                fi
        done
done

 方法2:使用ls -l语句进行过滤删除文件

-l:长格式列出文件

同等于:ll

[root@localhost ~]# ls -l /file
总用量 18432
-rw-r--r--. 1 root root 1048576 7月   7 07:55 1.txt
-rw-r--r--. 1 root root 2097152 7月   7 07:55 2.txt
-rw-r--r--. 1 root root 3145728 7月   7 07:55 3.txt
-rw-r--r--. 1 root root 4194304 7月   7 08:59 4.txt
-rw-r--r--. 1 root root 4194304 7月   7 08:59 5.txt
-rw-r--r--. 1 root root 4194304 7月   7 08:59 6.txt
[root@localhost ~]# ls -l /file | awk '{print $5}' | grep -v "^$"
1048576
2097152
3145728
4194304
4194304
4194304
[root@localhost ~]# ls -l /file | awk '{print $9}' | grep -v "^$"
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt

        思路:和方法1相似,ls提取处理的会开头有一个多余的空行,可以过滤一下(grep -v '^$')其余基本相同

#!/bin/bash
for size in $(ls -l /file/* | awk '{print $5}' | grep -v "^$")
do
        for file in $(ls -l /file/* |grep $size | awk '{print $9}'|awk -F/ '{print $3}' | grep -v '^$')
        do
                if [ $size -gt 3145728 ] ; then
                        rm -rf /file/${file}
                        echo "${file}${size}"
                        echo "" > $file
                fi
        done
done

方法3:使用find命令提取 

-size:查找大小(KB|MB|GB)

#!/bin/bash
for i in $(find /file -size +3M);
do
   rm -rf $i
done

 最后:使用crontab定时任务自动清理

> /dev/null:将正确信息丢弃

2> /var/delete:将错误信息报错到文件内,方便管理员查看

[root@localhost ~]# crontab -l
0 5 * * 7 /bin/bash /脚本目录/脚本名称 > /dev/null 2>/var/delete.errer

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,您可以编写一个 Shell 脚本,结合 `find` 和 `rm` 命令来实现日志目录大于1GB自动清理旧日志的功能。 以下是一个示例脚本: ```bash #!/bin/bash # 设置日志目录路径和阈值大小 log_dir="/path/to/log/dir" max_size=1073741824 # 1GB,单位为字节 # 查找日志文件并按修改时间排序,最早修改的文件排在最前面 log_files=$(find $log_dir -maxdepth 1 -type f -name "*.log" -printf "%T+ %p\n" | sort) # 计算日志目录的总大小 total_size=$(du -cs $log_dir | tail -n 1 | awk '{print $1}') # 如果总大小小于阈值,则直接退出 if [ $total_size -lt $max_size ]; then exit 0 fi # 遍历日志文件列表,删除最早修改的文件,直到目录大小小于阈值 for log_file in $log_files; do size=$(echo $log_file | awk '{print $1}') file=$(echo $log_file | awk '{print $2}') total_size=$(($total_size - $size)) rm $file if [ $total_size -lt $max_size ]; then break fi done ``` 该脚本首先设置日志目录路径和阈值大小,然后使用 `find` 命令查找日志文件并按修改时间排序,最早修改的文件排在最前面。接着,统计日志目录的总大小,如果总大小小于阈值,则直接退出。 如果总大小大于等于阈值,则遍历日志文件列表,依次删除最早修改的文件,直到目录大小小于阈值为止。每次删除文件后,都需要更新目录大小,如果目录大小已经小于阈值,则退出循环。 最后,您可以将该脚本加入到定时任务中,例如每天凌晨执行一次,来实现自动清理旧日志的功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

征服bug

curry.30

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值