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.

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

Other attempts at the same sort of code:

echo "Find files older & younger than X days"
echo "Type the number of X days and hit 'Enter':"
for d in /u1/?/*/Maildir/new ;
do FILE=$(find "$d" -type f -mtime +$PERIOD -exec ls {} \; >> OLDER.txt)
for d in /u1/?/*/Maildir/new ;
do FILE=$(find "$d" -type f -mtime -$PERIOD -exec ls {} \; >> YOUNGER.txt)
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. Jan 23, 2017 at 14:46

2 Answers 2


bash is not the best shell for that.

With zsh:

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

  if (($#over)); then
    ls -ld $newest >> OLDER.txt
    ls -ld $newest >> YOUNGER.txt
  • Very interesting. I am not familiar with zsh. I will check this out, I greatly appreciate your response. Thank you kind sir or madam. Jan 20, 2017 at 21:36

Here is a bash version.


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
            echo $FILE >> YOUNGER.txt
  • 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. Jan 23, 2017 at 20:56
  • You are asking about $d or $()? Jan 23, 2017 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. Jan 24, 2017 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. Jan 24, 2017 at 18:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .