您的位置:首页 > 运维架构 > Linux

Linux下自动清理超过指定大小的文件(shell脚本,通过du -sh或ls -l)

2018-10-11 00:29 1366 查看

 

由于线上业务用的squid,根据经验值如果长时间运行则缓存目录下的swap.state会慢慢变大,一旦超过60M,squid的性能就会急剧下降,因此需要定时去清理大于60M的swap.state文件。由此引出需求,查找cache目录下的所有大于60M的swap.state文件并清除,即:
1)查找cache目录下的所有swap.state文件
2)判断是否大于60M
3)大于60M则清空

解题思路:
以byte为单位显示文件大小,然后和60M大小做对比. 60M换算成字节为62914560这里判断是否大于60M,大于则使用echo 语句将对应文件置空。
60M=60*1024*1024=62914560 byte

可以使用dd命令创建一个60M的文件,测试下:

[root@kevin ~]# dd if=/dev/zero of=/mnt/test bs=1M count=60
60+0 records in
60+0 records out
62914560 bytes (63 MB) copied, 0.0492826 s, 1.3 GB/s
[root@kevin ~]# du -sh /mnt/test
60M     /mnt/test
[root@kevin ~]# du -sh -b /mnt/test
62914560        /mnt/test
[root@kevin ~]# ls -l /mnt/test
-rw-r--r--. 1 root root 62914560 Oct 12 14:15 /mnt/test

注意:
如果文件是带小数点的M单位,比如文件大小为1.3M,则换算成byte单位时,就不能直接使用1.3*1024*1024=1363148.8这样计算了,因为这个
1.3M的大小是估算出来的M单位的大小,不是精确到的. 如果直接加-b参数换算成byte单位大小则就是精确的值了,如下:
[root@kevin logs]# du -sh catalina.out
1.3M    catalina.out
[root@kevin logs]# du -sh -b catalina.out
1349930 catalina.out
[root@kevin logs]# ls -l catalina.out
-rw-r--r--. 1 root root 1349930 Oct 12 14:20 catalina.out

1) 方法一: "du -sh -b"

语法
# du [-abcDhHklmsSx][-L <符号连接>][-X <文件>][--block-size][--exclude=<目录或文件>][--max-depth=<目录层数>][--help][--version][目录或文件]

参数说明:
-a或-all                                                             显示目录中个别文件的大小。
-b或-bytes                                                         显示目录或文件大小时,以byte为单位。
-c或--total                                                          除了显示个别目录或文件的大小外,同时也显示所有目录或文件的总和。
-D或--dereference-args                                     显示指定符号连接的源文件大小。
-h或--human-readable                                       以K,M,G为单位,提高信息的可读性。
-H或--si                                                             与-h参数相同,但是K,M,G是以1000为换算单位。
-k或--kilobytes                                                   以1024 bytes为单位。
-l或--count-links                                                重复计算硬件连接的文件。
-L<符号连接>或--dereference<符号连接>           显示选项中所指定符号连接的源文件大小。
-m或--megabytes                                               以1MB为单位。
-s或--summarize                                               仅显示总计。
-S或--separate-dirs                                           显示个别目录的大小时,并不含其子目录的大小。
-x或--one-file-xystem                                         一开始处理时的文件系统为准,若遇上其它不同的文件系统目录则略过。
-X<文件>或--exclude-from=<文件>                   <文件>指定目录或文件。
--exclude=<目录或文件>                                    略过指定的目录或文件。
--max-depth=<目录层数>                                  超过指定层数的目录后,予以忽略。
--help 显示帮助。
--version                                                           显示版本信息。

[root@kevin ~]# du -sh /data/cache/coss/squid*/swap.state
4M /data/cache/coss/squid1/swap.state
270k /data/cache/coss/squid2/swap.state
4M /data/cache/coss/squid3/swap.state
8M /data/cache/coss/squid4/swap.state
53M /data/cache/coss/squid5/swap.state
35M /data/cache/coss/squid6/swap.state
6M /data/cache/coss/squid7/swap.state
7M /data/cache/coss/squid8/swap.state
97M /data/cache/coss/squid9/swap.state
75M /data/cache/coss/squid10/swap.state

[root@kevin ~]# du -sh -b /data/cache/coss/squid*/swap.state
4194304 /data/cache/coss/squid1/swap.state
276480 /data/cache/coss/squid2/swap.state
4194304 /data/cache/coss/squid3/swap.state
8388608 /data/cache/coss/squid4/swap.state
55574528 /data/cache/coss/squid5/swap.state
36700160 /data/cache/coss/squid6/swap.state
6291456 /data/cache/coss/squid7/swap.state
7340032 /data/cache/coss/squid8/swap.state
101711872 /data/cache/coss/squid9/swap.state
78643200 /data/cache/coss/squid11/swap.state

使用du -sh -b查找出相应文件的大小,同时使用awk 过滤第一个字段,只保留数字
[root@kevin ~]# du -sh -b /data/cache/coss/squid*/swap.state | awk '{ print $1 }'
4194304
276480
4194304
8388608
55574528
36700160
6291456
7340032
101711872
78643200

[root@kevin ~]# du -sh -b /data/cache/coss/squid*/swap.state | awk '{ print $2 }'
/data/cache/coss/squid1/swap.state
/data/cache/coss/squid2/swap.state
/data/cache/coss/squid3/swap.state
/data/cache/coss/squid4/swap.state
/data/cache/coss/squid5/swap.state
/data/cache/coss/squid6/swap.state
/data/cache/coss/squid7/swap.state
/data/cache/coss/squid8/swap.state
/data/cache/coss/squid9/swap.state
/data/cache/coss/squid11/swap.state

批量处理的脚本
[root@kevin ~]# vim /root/cache_gt_60.sh
#!/bin/bash
for size in $(du -sh -b /data/cache/coss/squid*/swap.state| awk '{ print $1 }')
do
for file in $(du -sh -b /data/cache/coss/squid*/swap.state|grep ${size}|awk '{print $2}')
do
if [ ${size} -gt 62914560 ];then
echo ${file} ${size}
echo "" > ${file}
fi
done
done

结合crontab进行定时执行
[root@kevin ~]# chmod 755 /root/cache_gt_60.sh
[root@kevin ~]# /bin/bash -x  /root/cache_gt_60.sh
[root@kevin ~]# crontab -e
0 2 * * 6 /bin/bash -x /root/cache_gt_60.sh > /dev/null 2>&1

2) 方法二: "ls -l"
ls命令是linux下用来列出目录下的文件. 下面是关于ls的一些常规用法:

ls -a      列出文件下所有的文件,包括以“.“开头的隐藏文件(linux下文件隐藏文件是以.开头的,如果存在..代表存在着父目录)。
ls -l       列出文件的详细信息,如创建者,创建时间,文件的读写权限列表等等。
ls -F      在每一个文件的末尾加上一个字符说明该文件的类型。"@"表示符号链接、"|"表示FIFOS、"/"表示目录、"="表示套接字。
ls -s      在每个文件的后面打印出文件的大小。  size(大小)
ls -t      按时间进行文件的排序  Time(时间)
ls -A     列出除了"."和".."以外的文件。
ls -R    将目录下所有的子目录的文件都列出来,相当于我们编程中的“递归”实现
ls -L    列出文件的链接名。Link(链接)
ls -S    以文件的大小进行排序

ls可以结合管道符”|“来进行一下复杂的操作。比如: ls | less用于实现文件列表的分页,ls

[root@clamav-server ~]# ls -l /data/cache/coss/squid*/swap.state
-rw-r--r--. 1 root root      4194304  Oct 3 11:52 /data/cache/coss/squid1/swap.state
-rw-r--r--. 1 root root      276480 Oct 3 12:12 /data/cache/coss/squid2/swap.state
-rw-r--r--. 1 root root      4194304 Oct 3 12:34 /data/cache/coss/squid3/swap.state
-rw-r--r--. 1 root root      8388608 Oct 3 14:06 /data/cache/coss/squid4/swap.state
-rw-r--r--. 1 root root      55574528 Oct 3 14:13 /data/cache/coss/squid5/swap.state
-rw-r--r--. 1 root root      36700160 Oct 3 15:21 /data/cache/coss/squid6/swap.state
-rw-r--r--. 1 root root      6291456 Oct 3 15:58 /data/cache/coss/squid7/swap.state
-rw-r--r--. 1 root root      7340032 Oct 3 17:12 /data/cache/coss/squid8/swap.state
-rw-r--r--. 1 root root      101711872 Oct 3 17:40 /data/cache/coss/squid9/swap.state
-rw-r--r--. 1 root root      78643200 Oct 3 19:27 /data/cache/coss/squid11/swap.state

[root@clamav-server ~]# ls -l /data/cache/coss/squid*/swap.state |awk '{print $5}'
4194304
276480
4194304
8388608
55574528
36700160
6291456
7340032
101711872
78643200
[root@clamav-server ~]# ls -l /data/cache/coss/squid*/swap.state |awk '{print $9}'
/data/cache/coss/squid1/swap.state
/data/cache/coss/squid2/swap.state
/data/cache/coss/squid3/swap.state
/data/cache/coss/squid4/swap.state
/data/cache/coss/squid5/swap.state
/data/cache/coss/squid6/swap.state
/data/cache/coss/squid7/swap.state
/data/cache/coss/squid8/swap.state
/data/cache/coss/squid9/swap.state
/data/cache/coss/squid11/swap.state

批量处理的脚本
[root@clamav-server ~]# vim /root/cache_gt_60.sh
#!/bin/bash
for size in $(ls -l /data/cache/coss/squid*/swap.state |awk '{print $5}')
do
for file in $(ls -l /data/cache/coss/squid*/swap.state|grep $size |awk '{print $9}')
do
if [ ${size} -gt 62914560 ];then
echo ${file} ${size}
echo "" > ${file}
fi
done
done

结合crontab进行定时执行
[root@CRN-JZ-2-36X ~]# chmod 755 /root/cache_gt_60.sh
[root@CRN-JZ-2-36X ~]# /bin/bash -x  /root/cache_gt_60.sh
[root@CRN-JZ-2-36X ~]# crontab -e
0 2 * * 6 /bin/bash -x /root/cache_gt_60.sh > /dev/null 2>&1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: