(linux命令学习)找到相应性质的文件并删除
2015-09-03 22:31
567 查看
今天看到find命令,于是想起怎么去做“找到指定大小的文件并删除”
find /tmp -maxdepth 1 -name "*.*" -exec ls -l {} \; | awk '{if($5>1000000) print $9}' | xargs rm -fr
找到目录下指定大小的文件并删除前5个最大的
1、制作测试文件
# vim test.sh
#!/bin/sh
for i in {1..30}
do
dd if=/dev/zero of=/tmp/test/$i.file bs=1M count=$i
done
#chmod +x test.sh
#./test.sh
#ls -lh
total 466M
-rw-r--r-- 1 root root 10M Sep 3 21:17 10.file
-rw-r--r-- 1 root root 11M Sep 3 21:17 11.file
-rw-r--r-- 1 root root 12M Sep 3 21:17 12.file
-rw-r--r-- 1 root root 13M Sep 3 21:17 13.file
-rw-r--r-- 1 root root 14M Sep 3 21:17 14.file
-rw-r--r-- 1 root root 15M Sep 3 21:17 15.file
-rw-r--r-- 1 root root 16M Sep 3 21:17 16.file
-rw-r--r-- 1 root root 17M Sep 3 21:17 17.file
-rw-r--r-- 1 root root 18M Sep 3 21:17 18.file
-rw-r--r-- 1 root root 19M Sep 3 21:17 19.file
-rw-r--r-- 1 root root 1.0M Sep 3 21:17 1.file
-rw-r--r-- 1 root root 20M Sep 3 21:17 20.file
-rw-r--r-- 1 root root 21M Sep 3 21:17 21.file
-rw-r--r-- 1 root root 22M Sep 3 21:17 22.file
-rw-r--r-- 1 root root 23M Sep 3 21:17 23.file
-rw-r--r-- 1 root root 24M Sep 3 21:17 24.file
-rw-r--r-- 1 root root 25M Sep 3 21:17 25.file
-rw-r--r-- 1 root root 26M Sep 3 21:17 26.file
-rw-r--r-- 1 root root 27M Sep 3 21:17 27.file
-rw-r--r-- 1 root root 28M Sep 3 21:17 28.file
-rw-r--r-- 1 root root 29M Sep 3 21:17 29.file
-rw-r--r-- 1 root root 2.0M Sep 3 21:17 2.file
-rw-r--r-- 1 root root 30M Sep 3 21:17 30.file
-rw-r--r-- 1 root root 3.0M Sep 3 21:17 3.file
-rw-r--r-- 1 root root 4.0M Sep 3 21:17 4.file
-rw-r--r-- 1 root root 5.0M Sep 3 21:17 5.file
-rw-r--r-- 1 root root 6.0M Sep 3 21:17 6.file
-rw-r--r-- 1 root root 7.0M Sep 3 21:17 7.file
-rw-r--r-- 1 root root 8.0M Sep 3 21:17 8.file
-rw-r--r-- 1 root root 9.0M Sep 3 21:17 9.file
-rwxr-xr-x 1 root root 91 Sep 3 21:17 test.sh
2、解决问题
开始的时候尝试使用
#]# find /tmp/test/ -maxdepth 1 -name "*.*" -exec ls -lh {} \; | sort -n
-rw-r--r-- 1 root root 10M Sep 3 21:17 /tmp/test/10.file
-rw-r--r-- 1 root root 1.0M Sep 3 21:17 /tmp/test/1.file
-rw-r--r-- 1 root root 11M Sep 3 21:17 /tmp/test/11.file
-rw-r--r-- 1 root root 12M Sep 3 21:17 /tmp/test/12.file
-rw-r--r-- 1 root root 13M Sep 3 21:17 /tmp/test/13.file
-rw-r--r-- 1 root root 14M Sep 3 21:17 /tmp/test/14.file
-rw-r--r-- 1 root root 15M Sep 3 21:17 /tmp/test/15.file
-rw-r--r-- 1 root root 16M Sep 3 21:17 /tmp/test/16.file
-rw-r--r-- 1 root root 17M Sep 3 21:17 /tmp/test/17.file
-rw-r--r-- 1 root root 18M Sep 3 21:17 /tmp/test/18.file
-rw-r--r-- 1 root root 19M Sep 3 21:17 /tmp/test/19.file
-rw-r--r-- 1 root root 20M Sep 3 21:17 /tmp/test/20.file
-rw-r--r-- 1 root root 2.0M Sep 3 21:17 /tmp/test/2.file
-rw-r--r-- 1 root root 21M Sep 3 21:17 /tmp/test/21.file
-rw-r--r-- 1 root root 22M Sep 3 21:17 /tmp/test/22.file
-rw-r--r-- 1 root root 23M Sep 3 21:17 /tmp/test/23.file
-rw-r--r-- 1 root root 24M Sep 3 21:17 /tmp/test/24.file
-rw-r--r-- 1 root root 25M Sep 3 21:17 /tmp/test/25.file
-rw-r--r-- 1 root root 26M Sep 3 21:17 /tmp/test/26.file
-rw-r--r-- 1 root root 27M Sep 3 21:17 /tmp/test/27.file
-rw-r--r-- 1 root root 28M Sep 3 21:17 /tmp/test/28.file
-rw-r--r-- 1 root root 29M Sep 3 21:17 /tmp/test/29.file
-rw-r--r-- 1 root root 30M Sep 3 21:17 /tmp/test/30.file
-rw-r--r-- 1 root root 3.0M Sep 3 21:17 /tmp/test/3.file
-rw-r--r-- 1 root root 4.0M Sep 3 21:17 /tmp/test/4.file
-rw-r--r-- 1 root root 5.0M Sep 3 21:17 /tmp/test/5.file
-rw-r--r-- 1 root root 6.0M Sep 3 21:17 /tmp/test/6.file
-rw-r--r-- 1 root root 7.0M Sep 3 21:17 /tmp/test/7.file
-rw-r--r-- 1 root root 8.0M Sep 3 21:17 /tmp/test/8.file
-rw-r--r-- 1 root root 9.0M Sep 3 21:17 /tmp/test/9.file
-rwxr-xr-x 1 root root 91 Sep 3 21:17 /tmp/test/test.sh
由上可见,得出的结果排序并不是按预想的那样。find命令得出的结果都是字符。
find传递给sort的是字符串,sort对字符串的排序是先对第一个字符来排序,因此会导致出现上面的结果。
上面的结果,是无法得到我们问题的操作效果的。
改用du来统计文件大小
#du -sh * | sort -n
1.1M 1.file
2.1M 2.file
3.1M 3.file
4.0K test.sh
4.1M 4.file
5.1M 5.file
6.1M 6.file
7.1M 7.file
8.1M 8.file
9.1M 9.file
11M 10.file
12M 11.file
13M 12.file
14M 13.file
15M 14.file
16M 15.file
17M 16.file
18M 17.file
19M 18.file
20M 19.file
21M 20.file
22M 21.file
23M 22.file
24M 23.file
25M 24.file
26M 25.file
27M 26.file
28M 27.file
29M 28.file
30M 29.file
31M 30.file
我们要找出前5个最大的
#du -sh * | sort -nr | head -n 5
31M 30.file
30M 29.file
29M 28.file
28M 27.file
27M 26.file
删除 5个最大的文件
du -sh * | sort -rn | head -n 5 | xargs rm -fr
看看操作后的结果
# du -sh * | sort -rn
26M 25.file
25M 24.file
24M 23.file
23M 22.file
22M 21.file
21M 20.file
20M 19.file
19M 18.file
18M 17.file
17M 16.file
16M 15.file
15M 14.file
14M 13.file
13M 12.file
12M 11.file
11M 10.file
9.1M 9.file
8.1M 8.file
7.1M 7.file
6.1M 6.file
5.1M 5.file
4.1M 4.file
4.0K test.sh
3.1M 3.file
2.1M 2.file
1.1M 1.file
结果如预期,完成 !
总结:
1、find命令查找的结果,传递给下一个命令时是字符数据类型,因此注意使用,特别是排序等
2、查找文件的大小,最好还是使用du,特别是需要排序的情况下
3、如果使用du并需要传递给后面的命令操作时,最好不要使用-h参数,因为如上所示4K与4M会排在一起,会导致误 操作
find /tmp -maxdepth 1 -name "*.*" -exec ls -l {} \; | awk '{if($5>1000000) print $9}' | xargs rm -fr
找到目录下指定大小的文件并删除前5个最大的
1、制作测试文件
# vim test.sh
#!/bin/sh
for i in {1..30}
do
dd if=/dev/zero of=/tmp/test/$i.file bs=1M count=$i
done
#chmod +x test.sh
#./test.sh
#ls -lh
total 466M
-rw-r--r-- 1 root root 10M Sep 3 21:17 10.file
-rw-r--r-- 1 root root 11M Sep 3 21:17 11.file
-rw-r--r-- 1 root root 12M Sep 3 21:17 12.file
-rw-r--r-- 1 root root 13M Sep 3 21:17 13.file
-rw-r--r-- 1 root root 14M Sep 3 21:17 14.file
-rw-r--r-- 1 root root 15M Sep 3 21:17 15.file
-rw-r--r-- 1 root root 16M Sep 3 21:17 16.file
-rw-r--r-- 1 root root 17M Sep 3 21:17 17.file
-rw-r--r-- 1 root root 18M Sep 3 21:17 18.file
-rw-r--r-- 1 root root 19M Sep 3 21:17 19.file
-rw-r--r-- 1 root root 1.0M Sep 3 21:17 1.file
-rw-r--r-- 1 root root 20M Sep 3 21:17 20.file
-rw-r--r-- 1 root root 21M Sep 3 21:17 21.file
-rw-r--r-- 1 root root 22M Sep 3 21:17 22.file
-rw-r--r-- 1 root root 23M Sep 3 21:17 23.file
-rw-r--r-- 1 root root 24M Sep 3 21:17 24.file
-rw-r--r-- 1 root root 25M Sep 3 21:17 25.file
-rw-r--r-- 1 root root 26M Sep 3 21:17 26.file
-rw-r--r-- 1 root root 27M Sep 3 21:17 27.file
-rw-r--r-- 1 root root 28M Sep 3 21:17 28.file
-rw-r--r-- 1 root root 29M Sep 3 21:17 29.file
-rw-r--r-- 1 root root 2.0M Sep 3 21:17 2.file
-rw-r--r-- 1 root root 30M Sep 3 21:17 30.file
-rw-r--r-- 1 root root 3.0M Sep 3 21:17 3.file
-rw-r--r-- 1 root root 4.0M Sep 3 21:17 4.file
-rw-r--r-- 1 root root 5.0M Sep 3 21:17 5.file
-rw-r--r-- 1 root root 6.0M Sep 3 21:17 6.file
-rw-r--r-- 1 root root 7.0M Sep 3 21:17 7.file
-rw-r--r-- 1 root root 8.0M Sep 3 21:17 8.file
-rw-r--r-- 1 root root 9.0M Sep 3 21:17 9.file
-rwxr-xr-x 1 root root 91 Sep 3 21:17 test.sh
2、解决问题
开始的时候尝试使用
#]# find /tmp/test/ -maxdepth 1 -name "*.*" -exec ls -lh {} \; | sort -n
-rw-r--r-- 1 root root 10M Sep 3 21:17 /tmp/test/10.file
-rw-r--r-- 1 root root 1.0M Sep 3 21:17 /tmp/test/1.file
-rw-r--r-- 1 root root 11M Sep 3 21:17 /tmp/test/11.file
-rw-r--r-- 1 root root 12M Sep 3 21:17 /tmp/test/12.file
-rw-r--r-- 1 root root 13M Sep 3 21:17 /tmp/test/13.file
-rw-r--r-- 1 root root 14M Sep 3 21:17 /tmp/test/14.file
-rw-r--r-- 1 root root 15M Sep 3 21:17 /tmp/test/15.file
-rw-r--r-- 1 root root 16M Sep 3 21:17 /tmp/test/16.file
-rw-r--r-- 1 root root 17M Sep 3 21:17 /tmp/test/17.file
-rw-r--r-- 1 root root 18M Sep 3 21:17 /tmp/test/18.file
-rw-r--r-- 1 root root 19M Sep 3 21:17 /tmp/test/19.file
-rw-r--r-- 1 root root 20M Sep 3 21:17 /tmp/test/20.file
-rw-r--r-- 1 root root 2.0M Sep 3 21:17 /tmp/test/2.file
-rw-r--r-- 1 root root 21M Sep 3 21:17 /tmp/test/21.file
-rw-r--r-- 1 root root 22M Sep 3 21:17 /tmp/test/22.file
-rw-r--r-- 1 root root 23M Sep 3 21:17 /tmp/test/23.file
-rw-r--r-- 1 root root 24M Sep 3 21:17 /tmp/test/24.file
-rw-r--r-- 1 root root 25M Sep 3 21:17 /tmp/test/25.file
-rw-r--r-- 1 root root 26M Sep 3 21:17 /tmp/test/26.file
-rw-r--r-- 1 root root 27M Sep 3 21:17 /tmp/test/27.file
-rw-r--r-- 1 root root 28M Sep 3 21:17 /tmp/test/28.file
-rw-r--r-- 1 root root 29M Sep 3 21:17 /tmp/test/29.file
-rw-r--r-- 1 root root 30M Sep 3 21:17 /tmp/test/30.file
-rw-r--r-- 1 root root 3.0M Sep 3 21:17 /tmp/test/3.file
-rw-r--r-- 1 root root 4.0M Sep 3 21:17 /tmp/test/4.file
-rw-r--r-- 1 root root 5.0M Sep 3 21:17 /tmp/test/5.file
-rw-r--r-- 1 root root 6.0M Sep 3 21:17 /tmp/test/6.file
-rw-r--r-- 1 root root 7.0M Sep 3 21:17 /tmp/test/7.file
-rw-r--r-- 1 root root 8.0M Sep 3 21:17 /tmp/test/8.file
-rw-r--r-- 1 root root 9.0M Sep 3 21:17 /tmp/test/9.file
-rwxr-xr-x 1 root root 91 Sep 3 21:17 /tmp/test/test.sh
由上可见,得出的结果排序并不是按预想的那样。find命令得出的结果都是字符。
find传递给sort的是字符串,sort对字符串的排序是先对第一个字符来排序,因此会导致出现上面的结果。
上面的结果,是无法得到我们问题的操作效果的。
改用du来统计文件大小
#du -sh * | sort -n
1.1M 1.file
2.1M 2.file
3.1M 3.file
4.0K test.sh
4.1M 4.file
5.1M 5.file
6.1M 6.file
7.1M 7.file
8.1M 8.file
9.1M 9.file
11M 10.file
12M 11.file
13M 12.file
14M 13.file
15M 14.file
16M 15.file
17M 16.file
18M 17.file
19M 18.file
20M 19.file
21M 20.file
22M 21.file
23M 22.file
24M 23.file
25M 24.file
26M 25.file
27M 26.file
28M 27.file
29M 28.file
30M 29.file
31M 30.file
我们要找出前5个最大的
#du -sh * | sort -nr | head -n 5
31M 30.file
30M 29.file
29M 28.file
28M 27.file
27M 26.file
删除 5个最大的文件
du -sh * | sort -rn | head -n 5 | xargs rm -fr
看看操作后的结果
# du -sh * | sort -rn
26M 25.file
25M 24.file
24M 23.file
23M 22.file
22M 21.file
21M 20.file
20M 19.file
19M 18.file
18M 17.file
17M 16.file
16M 15.file
15M 14.file
14M 13.file
13M 12.file
12M 11.file
11M 10.file
9.1M 9.file
8.1M 8.file
7.1M 7.file
6.1M 6.file
5.1M 5.file
4.1M 4.file
4.0K test.sh
3.1M 3.file
2.1M 2.file
1.1M 1.file
结果如预期,完成 !
总结:
1、find命令查找的结果,传递给下一个命令时是字符数据类型,因此注意使用,特别是排序等
2、查找文件的大小,最好还是使用du,特别是需要排序的情况下
3、如果使用du并需要传递给后面的命令操作时,最好不要使用-h参数,因为如上所示4K与4M会排在一起,会导致误 操作
相关文章推荐
- Linux socket 初步
- 10 篇对初学者和专家都有用的 Linux 命令教程
- Linux 与 Windows 对UNICODE 的处理方式
- Ubuntu12.04下QQ完美走起啊!走起啊!有木有啊!
- 解決Linux下Android开发真机调试设备不被识别问题
- 运维入门
- 运维提升
- Linux 自检和 SystemTap
- Ubuntu Linux使用体验
- c语言实现hashmap(转载)
- Linux 信号signal处理机制
- linux下mysql添加用户
- Scientific Linux 5.5 图形安装教程
- 基于 Linux 集群环境上 GPFS 的问题诊断
- 谁是桌面王者?Win PK Linux三大镇山之宝
- vivi下重新调整分区
- Linux VS Unix:Linux欲一统天下 Unix不死
- linux下设定环境变量
- Linux下修改MySQL编码的方法