linux - 用于删除小于 x kb 的文件的 Shell 脚本

标签 linux bash file shell scripting

我正在尝试找出如何编写一个小脚本来删除小于 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/

相关文章:

python - 使用 RabbitMQ 和 Django 从内部服务器获取信息

c++ - libusb中断传输

node.js - 如何将curl 输出直接发送到Azure 存储?

linux - 在 Shell 中比较十六进制数

file - 防止 nano 在收到 SIGHUP 或 SIGTERM 时创建保存文件

php - 使用执行位运行 CLI php 脚本

bash - 找到每行一个项目的两个文本文件之间的差异

Python:导入文件并转换为列表

尝试读取文件时出现 java.lang.NullPointerException

linux - 如何在linux mint上添加backtrack等kali工具?