inux下的find命令用來查找文件,通過man find就知道它是無所不能的。所以按照文件大小來查找文件就不在話下。從man find搜索size,可以看到如下信息:
-size n[cwbkMG] File uses n units of space. The following suffixes can be used: b for 512-byte blocks (this is the default if no suffix is used) c for bytes w for two-byte words k for Kilobytes (units of 1024 bytes) M for Megabytes (units of 1048576 bytes) G for Gigabytes (units of 1073741824 bytes)
注意:默認單位是b,而它代表的是512字節,所以2表示1K,1M則是2048,如果不想自己轉換,可以使用其他單位,如c、K、M等。
例子:查找當前目錄下文件大小為2048(2k)字節的文件
find ./ -size 4 或 find ./ -size 2048c 或 find ./ -size 2K
上述查找文件是等於指定大小的,那能不能查詢大於或小於某個指定值的文件呢,答案是肯定,例如:
查找大於2K的文件,+ 表示大於 find ./ -size +2048c 查找小於2K的文件,- 表示小於 find ./ -size -2048c -type f
找到的文件可以進一步操作!
如: 查找小於1000字節的文件刪除之
find ./ -size -1000c -type f -exec rm -rf {} \;