(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會排在一起,會導致誤 操作

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章