Skip to content

Instantly share code, notes, and snippets.

@camsaul
Created October 23, 2014 08:39
Show Gist options
  • Save camsaul/fdfa44aacfcd7340c4b4 to your computer and use it in GitHub Desktop.
Save camsaul/fdfa44aacfcd7340c4b4 to your computer and use it in GitHub Desktop.
Bash function to get the date a file is modified, works for BSD or Linux
#!/bin/bash
# return unix timestamp for file or 0 if it doesn't exist
[ `uname` == "Darwin" ] && STAT_FORMAT_FLAG='-f' || STAT_FORMAT_FLAG='-c'
file_modified () {
file=$1
if [ -f "$file" ] && [ -n "$file" ]; then # make sure filename is non-empty, -f will return true otherwise
echo `stat $STAT_FORMAT_FLAG "%a" $file`
else
echo "0";
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment