Bash – find greatest last-modified files of /dirA/file, /dirB/file

bash

Suppose we have two directories, dirA, dirB, which the exact same PDF files, just different last modified.

What is a bash script (no awk) that can search through each file name (assume always in both dirA, dirB) and for each filename, outputs which of the files (dirA/file or dirB/file) has the greater last-modified; file-modified-last?
e.g.

if dirA/file.lastmodified > dirB/file.lastmodified 
##take action

Best Answer

With GNU stat:

shopt -s dotglob

for file in dirA/*; do
    [[ -f "dirB/${file##*/}" ]] || continue
    if (( "$(stat -c %Y "$file")" > "$(stat -c %Y "dirB/${file##*/}")" )); then
        # take action
    fi
done
Related Question