Bourne returning files based on creation date

I'm wanting to write a bourne shell script that takes in two command line arguments - a directory and a file. With this I want to return a list of files within the directory that are older (based on creation date) than the given file, and print the number of files that have not been listed (they are newer than the given file).
I've never used bourne before so I'm hoping someone could help me out with a few tips on how I'd go about this. I've been thinking it will need an if statement that will separate those older files from the newer ones and update a count of the newer ones? But I'm unsure about how to work with the dates and such.

I've written a little mock up, that I know isn't correct but would something like this come anywhere close to working?

#!/bin/sh

directory = $1
file = $2
count = 0

for f in $directory
do
    if [ "$f" �ot "$file"]; then
	echo "$f"
	count='expr $count + 1'
    fi
done

echo "There are $x newer files"

Any help would be greatly appreciated!
Thanks!

here's something to start with for the 'older' files:

find myDirectory ! -cnewer myReferenceFile -print
1 Like

-ot is defined for the [[ ]] compound in ksh and bash:

#!/bin/ksh

directory=$1
file=$2
count=0

for f in "$directory"/*
do
  if [[ "$f" -ot "$file" ]]; then
    echo "$f"
    count=$((count + 1))
  fi
done

echo "There are $x newer files"

As you see, ksh and bash also have the built-in $(( )) arithmetic.

As already suggested, this might be easier with find , and can be done also in an old Bourne shell:

#!/bin/sh

directory=$1
file=$2

find "$directory" -newer "$file" -o -print

Both -newer and -ot take the file's mtime (not ctime as -cnewer does).
But the find will descend into sub-directories and sub-sub-directories if present.

1 Like

Thanks heaps for clearing that up! Do you know if there's a way to do the same as find, but instead count the files instead of printing them all out?

find .... | wc -l