我正在尝试找出如何编写一个小脚本来删除小于 50 KB 的文本文件,但我没有成功。
我的尝试是这样的:
#!/bin/bash
for i in *.txt
do
if [ stat -c %s < 5 ]
then
rm $i
fi
done
希望得到一些指导,谢谢!
最佳答案
为此,您可以直接使用带有 size
选项的 find
:
find /your/path -name "*.txt" -size -50k -delete
^^^^^^^^^^
if you wanted bigger than 50k, you'd say +50
您可能希望坚持当前目录中的文件,而不是进入目录结构。如果是这样,你可以说:
find /your/path -maxdepth 1 -name "*.txt" -size -50k -delete
来自 man find
:
-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)
关于linux - 用于删除小于 x kb 的文件的 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27789254/