由于线上业务用的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=6060+0 records in60+0 records out62914560 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.state4M /data/cache/coss/squid1/swap.state270k /data/cache/coss/squid2/swap.state4M /data/cache/coss/squid3/swap.state8M /data/cache/coss/squid4/swap.state53M /data/cache/coss/squid5/swap.state35M /data/cache/coss/squid6/swap.state6M /data/cache/coss/squid7/swap.state7M /data/cache/coss/squid8/swap.state97M /data/cache/coss/squid9/swap.state75M /data/cache/coss/squid10/swap.state [root@kevin ~]# du -sh -b /data/cache/coss/squid*/swap.state4194304 /data/cache/coss/squid1/swap.state276480 /data/cache/coss/squid2/swap.state4194304 /data/cache/coss/squid3/swap.state8388608 /data/cache/coss/squid4/swap.state55574528 /data/cache/coss/squid5/swap.state36700160 /data/cache/coss/squid6/swap.state6291456 /data/cache/coss/squid7/swap.state7340032 /data/cache/coss/squid8/swap.state101711872 /data/cache/coss/squid9/swap.state78643200 /data/cache/coss/squid11/swap.state 使用du -sh -b查找出相应文件的大小,同时使用awk 过滤第一个字段,只保留数字[root@kevin ~]# du -sh -b /data/cache/coss/squid*/swap.state | awk '{ print $1 }'  41943042764804194304838860855574528367001606291456734003210171187278643200[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/bashfor 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    donedone 结合crontab进行定时执行[root@kevin ~]# chmod 755 /root/cache_gt_60.sh[root@kevin ~]# /bin/bash -x  /root/cache_gt_60.sh[root@kevin ~]# crontab -e0 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}'41943042764804194304838860855574528367001606291456734003210171187278643200[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/bashfor 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    donedone结合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 -e0 2 * * 6 /bin/bash -x /root/cache_gt_60.sh > /dev/null 2>&1

3) 方法三:"find -size"
-size 选项用于查找满足指定的大小条件的文件(注意不查找目录), +表示大于, -表示小于, 没有+或-表示正好等于。

[root@kevin ~]# du -sh /data/cache/coss/squid*/swap.state4M /data/cache/coss/squid1/swap.state270k /data/cache/coss/squid2/swap.state4M /data/cache/coss/squid3/swap.state8M /data/cache/coss/squid4/swap.state53M /data/cache/coss/squid5/swap.state35M /data/cache/coss/squid6/swap.state6M /data/cache/coss/squid7/swap.state7M /data/cache/coss/squid8/swap.state97M /data/cache/coss/squid9/swap.state75M /data/cache/coss/squid10/swap.state[root@kevin ~]# find  /data/cache/coss/squid*/swap.state -size +60M /data/cache/coss/squid9/swap.state/data/cache/coss/squid10/swap.state[root@redis-new03 ~]# for i in $(find  /data/cache/coss/squid*/swap.state -size +60M);do echo " " > $i;done [root@kevin ~]# du -sh /data/cache/coss/squid*/swap.state4M /data/cache/coss/squid1/swap.state270k /data/cache/coss/squid2/swap.state4M /data/cache/coss/squid3/swap.state8M /data/cache/coss/squid4/swap.state53M /data/cache/coss/squid5/swap.state35M /data/cache/coss/squid6/swap.state6M /data/cache/coss/squid7/swap.state7M /data/cache/coss/squid8/swap.state4.0K /data/cache/coss/squid9/swap.state4.0K /data/cache/coss/squid10/swap.state编写脚本[root@kevin ~]# vim /root/cache_gt_60.sh#!/bin/bashfor i in $(find  /data/cache/coss/squid*/swap.state -size +60M);do    echo " " > $i;done 结合crontab进行定时执行[root@kevin ~]# crontab -e0 2 * * 6 /bin/bash -x /root/cache_gt_60.sh > /dev/null 2>&1
©著作权归作者所有:来自51CTO博客作者80民工的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. 17年未修复的Firefox本地文件窃取漏洞分析
  2. 【linux】循序渐进学运维-基础篇-Linux系统目录
  3. 在Linux中创建 本地yum源
  4. 【linux】循序渐进学运维-基础篇-操作系统初始化
  5. 【linux】循序渐进学运维-基础篇-文件权限管理
  6. [linux]循序渐进学运维-基础命令篇-文件的归档和压缩
  7. Linux中常用压缩及解压命令
  8. 【linux-56】文件系统管理-df,du
  9. Vue(5): APP.vue引入CSS样式文件和动态切换组件的方法

随机推荐

  1. 《Android开发从零开始》——35.GridView
  2. 防止回收利用触摸事件
  3. android NDK 实用学习(四)-类缓存
  4. 【血泪】SDL终于移植成功,并且在模拟器上
  5. 关于Android资源文件中出现百分号的问题
  6. android侧滑菜单-DrawerLayout的基本使用
  7. android中include标签使用详解
  8. Mars《Android开发视频教程》全集下载(第
  9. 详细Android Studio + NDK范例
  10. 如何扩大一个view的touch和click响应区域