目的: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