简体   繁体   English

在bash脚本中基于名称作为日期删除文件夹(以mm / dd / yy为单位)

[英]delete folders based on name as date in mm/dd/yy in bash script

I want to delete all directories apart from the current date's directory. 我想删除除当前日期目录之外的所有目录。 directory names are taken as date in this MM-DD-YY format so directory name is 目录名称以这种MM-DD-YY格式作为日期,因此目录名称为

10-12-17 17年10月12日

10-11-17 17年10月11日

10-10-17 ..etc 17-10-10 ..etc

    #!/bin/bash
    echo Hello World!

    one_day=$(date -d "1 days ago" +%m%d%y)
    for f in [0-9][0-9]-[0-9][0-9]-[0-9][0-9]; do
    [ -d "$f" ] || continue
    (( $f < $one_day )) &&  sudo rm -rf "$f"
    done

While running my script I am getting the following error: 运行脚本时,出现以下错误:

./script.sh: line 9: ((: 10-08: value too great for base (error token is "08")

./script.sh: line 9: ((: 10-09: value too great for base (error token is "09")

You seem to be trying to check an inequality between "11-10-17" and "111017" (one is an int and the other is a string). 您似乎正在尝试检查“ 11-10-17”和“ 111017”之间的不等式(一个是整数,另一个是字符串)。 Bash will let you check for a string inequality using != Bash可以让您使用!=检查字符串不等式

#!/bin/bash

one_day=$(date -d "1 days ago" +%m-%d-%y)
for f in [0-9][0-9]-[0-9][0-9]-[0-9][0-9]; do
  [ -d "$f" ] || continue
  [ "$f" != "$one_day" ] && sudo rm -rf "$f" && echo "$f" && continue
  [ "$f" == "$one_day" ] && echo "Leaving $f"
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM