0

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

10-12-17

10-11-17

10-10-17 ..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")
2
  • Have you accepted the good news of ISO8601 into your heart today? en.wikipedia.org/wiki/ISO_8601 Commented Oct 13, 2017 at 13:51
  • s=$(date +%m-%d-%y); mv $s .$s; rm -rf [0-9][0-9]-[0-9][0-9]-[0-9][0-9]; mv .$s $s #why not just do that? Commented Oct 13, 2017 at 14:00

1 Answer 1

1

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). Bash will let you check for a string inequality using !=

#!/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
Sign up to request clarification or add additional context in comments.

5 Comments

./script.sh: line 6: ((: 10-08: value too great for base (error token is "08") ./script.sh: line 7: ((: 10-08: value too great for base (error token is "08") ./script.sh: line 6: ((: 10-09: value too great for base (error token is "09") ./script.sh: line 7: ((: 10-09: value too great for base (error token is "09")
unable to delete all folders. script is only capable enough to delete few. getting below mentioned error for folders like 10-08-17, 10-08-17, 09-30-17. ./script.sh: line 6: ((: 10-08: value too great for base (error token is "08") ./script.sh: line 7: ((: 10-08: value too great for base (error token is "08") ./script.sh: line 6: ((: 10-09: value too great for base (error token is "09") ./script.sh: line 7: ((: 10-09: value too great for base (error token is "09")
./script.sh: line 6: ((: 10-08: value too great for base (error token is "08") ./script.sh: line 7: ((: 10-08: value too great for base (error token is "08") ./script.sh: line 6: ((: 10-09: value too great for base (error token is "09") ./script.sh: line 7: ((: 10-09: value too great for base (error token is "09")
After testing with: for f in {0..1}{0..9}-{0..3}{0..9}-{0..9}{0..0}; do mkdir "$f"; done I found my error. I've fixed the conditional by changing (( foo != bar )) to [ foo != bar ]. Try now.
for f in $(seq 100); do mkdir $(date -d "$f days ago" +%m-%d-%y); done might be a better way to create a unit test for this, (and possibly a better way to iterate dates).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.