微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我试图找出如何编写一个小脚本来删除小于50千字节的文本文件,但我没有成功.

我的尝试看起来像这样:

#!/bin/bash
for i in *.txt
do
   if [ stat -c %s < 5 ]
   then
     rm $i
   fi
done

我会赞美一些指导,谢谢!

解决方法:

您应该使用fedorqui的版本,但供参考:

#!/bin/bash
for i in ./*.txt   # ./ avoids some edge cases when files start with dashes
do
  # $(..) can be used to get the output of a command
  # use -le, not <, for comparing numbers
  # 5 != 50k
  if [ "$(stat -c %s "$i")" -le 50000 ]
  then
    rm "$i"  # specify the file to delete      
  fi # end the if statement
done

通常更容易编写一个程序并验证每个部分是否正常工作,而不是编写整个程序然后尝试调试它.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐