加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-13 23:57:28 所属栏目:Linux 来源:网络整理
导读:我试图找出如何编写一个小脚本来删除小于50千字节的文本文件,但我没有成功. 我的尝试看起来像这样: #!/bin/bashfor i in *.txtdo if [ stat -c %s 5 ] then rm $i fidone 我会赞美一些指导,谢谢! 解决方法 您应该使用fedorqui的版本,但供参考: #!/bin/bas
我试图找出如何编写一个小脚本来删除小于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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读