由于线上业务用的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

3) 方法三:"find -size"

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

[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 ~]# 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.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

4.0K /data/cache/coss/squid9/swap.state

4.0K /data/cache/coss/squid10/swap.state

编写脚本

[root@kevin ~]# vim /root/cache_gt_60.sh

#!/bin/bash

for i in $(find /data/cache/coss/squid*/swap.state -size +60M);

do

echo " " > $i;

done

结合crontab进行定时执行

[root@kevin ~]# crontab -e

0 2 * * 6 /bin/bash -x /root/cache_gt_60.sh > /dev/null 2>&1

linux如何批量清理文件大小,Linux下自动清理超过指定大小文件的方法相关推荐

  1. linux 文件大小 自动变化 写,Linux下自动清理超过指定大小文件的方法

    由于线上业务用的squid,根据经验值如果长时间运行则缓存目录下的swap.state会慢慢变大,一旦超过60M,squid的性能就会急剧下降,因此需要定时去清理大于60M的swap.state文件. ...

  2. linux如何删除指定大小以下的文件夹,Linux中定时删除超过指定大小的文件夹

    背景: 开发环境总是动不动就没有空间了, 大部分都是debug日志.所以有必要在日志很疯狂的时候,删除不必要的日志. 思路:一. 书写删除日志文件脚本: 定时任务执行.  但是有时候的日志是需要保存用 ...

  3. linux批量修改压缩图片大小,linux shell批量压缩某个目录下图片大小

    linux shell批量压缩某个目录下图片大小 #!/bin/sh read -p "Input Path:" SPATH maxsize=$((1024*200)) FILEL ...

  4. linux系统怎么清理指定日期的文件,Linux系统删除指定时间段文件的方法(2)

    搜索根目录下小于500KB的文件,命令行为: find / -size -500K -print 删除文件大小为0的文件 rm -i `find ./ -size 0` find ./size 0 e ...

  5. php读取iso文件,Linux_linux下如何读取使用iso 镜像文件的方法,如果拷贝到本地,可以使用moun - phpStudy...

    linux下如何读取使用iso 镜像文件的方法 如果拷贝到本地,可以使用mount mount fileName mountPoint -o loop,fileName是镜像文件名(*.iso,*.i ...

  6. 服务器拒绝接收office文件,Ghost Win7系统下Outlook设置拒绝接收垃圾文件的方法

    Outlook是Microsoftoffice套装软件的组件之一,可以用它来收发电子邮件.管理联系人信 息.记日记.安排日程.分配任务,还可以帮助用户查找和组织信息,不过有时候在接收文件的时候老是会接 ...

  7. Windows Server 2008下Microsoft Office Excel 不能访问文件解决方法

    Windows Server 2008下Microsoft Office Excel 不能访问文件解决方法 在VS2008 中写了一个EXCEL的导出功能的东西,但在Windows Server 20 ...

  8. HTML如何让IMG自动适应DIV容器大小的实现方法

    这篇文章主要介绍了HTML如何让IMG自动适应DIV容器大小的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 为了让IM ...

  9. 使用dd命令在Linux下创建大文件,批量大小文件生成方法

    前沿:最近在开发自动从U盘拷贝大批量文件到linux系统的的功能.由于需要几十个G的大文件来做测试,如果自己去找这么多资源,然后再拷贝,非常麻烦.所以学了下dd命令,现在总结一下: 一.参数介绍 if ...

最新文章

  1. Educational Codeforces Round 108(Rated for Div. 2) E - Off by One(一种一般图的边最大匹配,好题)
  2. MySQL 获得当前日期时间 函数
  3. LightOJ - 1265 概率
  4. 各种资源思科、gns3……
  5. 「C++: draft」一张图弄懂C++指针(*)和引用()以及深拷贝、浅拷贝
  6. 优酷视频如何将地区设置为中国大陆
  7. 小程序textarea的行间距_微信小程序组件:textarea多行输入框解读和分析
  8. socket编程(一)
  9. OpenCV精进之路(三):图像处理——形态学滤波(膨胀、腐蚀、开闭运算)
  10. MediaInfo源代码分析 3:Open()函数
  11. MEF(Managed Extensibility Framework)学习笔记
  12. 点餐系统+小程序常见问题解决(2022年最新版)
  13. 5.5matlab曲线拟合(多项式函数拟合)
  14. GraphRNN: Generating Realistic Graphs with Deep Auto-Regressive Models
  15. 电脑回收站删除的文件还能找回吗 电脑回收站删除的文件怎么恢复
  16. 这几种食物不要给1岁内宝宝吃
  17. 唯品会获得vip商品详情 API 返回值说明
  18. CCS Uniflash烧写CC3200开发板的简易操作笔记
  19. 百度定位实时获取位置android,通过百度定位sdk获取实时位置
  20. 如何通过ssh远程访问Unix/Linux服务器上的html

热门文章

  1. 可逆矩阵的特征值和原来矩阵_线性代数——相似矩阵的可逆变换矩阵P是否唯一...
  2. 职业中专计算机高考英语卷子,职业高中高考计算机专业试卷3.doc
  3. clock函数的时间单位_PAT B1026:程序运行时间
  4. layuiadmin上手好难_孩子学什么乐器好?十种最受欢迎乐器的优劣势分析
  5. w10无法连到家庭组计算机,一键W10装机版无法进入家庭组如何处理
  6. pycocotools安装_pycocotools安装问题
  7. C++socket编程(八):8.2简单的基于UDP的客户端和服务端
  8. python语音分割_Python 牺牲性能以提升程序员的工作效率
  9. java开发http协议接口_java开发接口利用http协议传输数据
  10. SpringSecurity Form Login