0

In bash, one can test whether file1 is older than file2:

  if [[ file1 -ot file2 ]]; then
    ...
  fi

If file1 or file2 (or both) are symbolic links, I want files compared by mtime of symbolic link itself, not the file it points to (i.e. I want bash not to follow symbolic links).

Is it even possible without using an external program? In any case, what is the best way to do it.

Val
  • 6,585
  • 5
  • 22
  • 52

1 Answers1

0
#Compares modification dates on files (or links for that matter)
fname1=f1.txt
fname2=f2.txt

file1=$(date +%s -d "$(stat $fname1 | grep Modify | awk '{print $2" "$3}')")
file2=$(date +%s -d "$(stat $fname2 | grep Modify | awk '{print $2" "$3}')")

if [ $file1 -gt $file2 ];then
    echo $fname1 is newer than $fname1
else
    echo $fname1 is older than $fname2
fi
Josiah DeWitt
  • 1,594
  • 13
  • 15