(linux命令学习)找到相应性质的文件并删除

今天看到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会排在一起,会导致误 操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值