0

I'll try to explain what i have done until now.

First of all i have build the list of directories i want to investigate with the code below:

$MDIR="/home/user/scripts/fcron"
DIRS=`ls -l $MDIR | egrep '^d' | awk '{print $9}' | grep ^$ts-`

The DIRS list now contain directory names of the form :

NETGEAR-2013-06-30
NETGEAR-2013-07-01
........
NETGEAR-2013-05-05

Now i need to check all those directories (contained in list DIRS) and i need to find one that is newer than 1 month before now (e.g 27 or 29 days). In case i find one match i need to exit the script.

In 'pseudocode' i would write:

for dir is DIRS:
    if dir is newer than 30 days old:
       exit the script
    else:
       continue

My difficulty is in translating just the above 'pseudocode' in a bash script.

/////Latest Update/////

OK i have updated the part with the pseudocode with this:

    for DIR is $DIRS;
    do
    if (( $(stat -c %Y "$dir") < $(date +%s) - 3600*24*30 )); then
        echo "exiting!!"       
    exit
    else
        continue
    fi
    done

But now i am getting this :

 line 40: syntax error: unexpected word (expecting "do")
3

Try doing this :

dirs='dir1 dir2 dir3'

for dir is $dirs; do
    if (( $(stat -c %Y "$dir") < $(date +%s) - 3600*24*30 )); then
       exit
    else
       continue
    fi
done
1
1

The general approach:

find . -mindepth 1 -maxdepth 1 -type d \
  -newermt "$(date --date="1 month ago 00:00" --rfc-3339=seconds)"

Using your DIRS:

find $DIRS -maxdepth 0 -type d \
  -newermt "$(date --date="1 month ago 00:00" --rfc-3339=seconds)"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.