2

I am attempting to write a script that will recursively search a directory's sub-directories and do the following:

  1. Find the most recent file in that sub-directory
  2. Find the date of that most recent file
  3. Check if that file is older than X days e.g. (180)
  4. If the file is older than X days, output its path to a file called "OLD.txt"
  5. If the file is NOT older than X days, output its path to a file called "YOUNG.txt"

Currently this is all I have. I am unsure how to grab the date from the file via the find command do the if/then comparison.

#!/bin/bash
for d in /u1/?/*/Maildir/new ;
do FILE=$(find "$d" -printf '%T+ %p\n' | sort -r | head >> Test.txt)
done

Other attempts at the same sort of code:

#!/bin/bash
echo "Find files older & younger than X days"
echo "Type the number of X days and hit 'Enter':"
read PERIOD
for d in /u1/?/*/Maildir/new ;
do FILE=$(find "$d" -type f -mtime +$PERIOD -exec ls {} \; >> OLDER.txt)
done
for d in /u1/?/*/Maildir/new ;
do FILE=$(find "$d" -type f -mtime -$PERIOD -exec ls {} \; >> YOUNGER.txt)
done
echo "COMPLETE!"
echo "Check 'OLDER.txt' and 'YOUNGER.txt' files in current directory for output"

The problem with the last one, is that it outputs ALL the files, while I need to just find the most RECENT file in the directory, and make sure that the most RECENT file isn't older than X days.

  • Would you be willing to give an example as to what you mean? I'm pretty sure I understand; just wanted to verify. Appreciate it. – Pietro Aretino Jan 23 '17 at 14:46
0

Here is a bash version.

#!/bin/bash
maildir='/u1/?/*/Maildir/new'
DAYS_OLD=180

for d in $maildir ; do
    if [ -d "$d" ] ; then
        FILE="$d/"$(ls "$d" -t1  | head -n 1)
        echo $FILE
        if test $(find "$FILE" -mtime +$DAYS_OLD) ; then
            echo $FILE >> OLDER.txt
        else
            echo $FILE >> YOUNGER.txt
        fi
    fi
done
  • Genius, thank you so much. This will really help me better understand bash scripting and loops. This works amazingly, I'm just confused by one thing in the script. if [ -d "$d" ] ; then What is the -d; I'm not familiar with the brackets either. I've just begun working on scripting so, I apologize for my ignorance. – Pietro Aretino Jan 23 '17 at 20:56
  • You are asking about $d or $()? – Stephen Rauch Jan 23 '17 at 20:57
  • My apologies for the confusion but I was asking about the if [ -d "$d" ] ; then argument. I know $d is the directory variable to loop through, but i'm not sure how -d comes into play, or what it is. Thank you for your time and help in this matter once again, much obliged kind sir/madam. – Pietro Aretino Jan 24 '17 at 15:18
  • Thanks again kind sir/madam! This is great information. I'm guessing the same syntax can be applied to files? if [ -f "$f" ] ; then etc. Once again much obliged. – Pietro Aretino Jan 24 '17 at 18:54
3

bash is not the best shell for that.

With zsh:

#! /bin/zsh -
period=${1?Period not given}
for d (/u1/?/*/Maildir/new) {
  newest=($d/*(DN.om[1]))
  (($#newest)) || continue

  over=($newest(Nm+$period))
  if (($#over)); then
    ls -ld $newest >> OLDER.txt
  else
    ls -ld $newest >> YOUNGER.txt
  fi
}
  • Very interesting. I am not familiar with zsh. I will check this out, I greatly appreciate your response. Thank you kind sir or madam. – Pietro Aretino Jan 20 '17 at 21:36

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.