If the File is Older Than the Specified Date, Do Something in Shell Script

Shellscript(Bash/Zsh)
2022-05-28 07:29 (3 years ago)
If the File is Older Than the Specified Date, Do Something in Shell Script

This script is written to check for tasks that have not been processed for a certain period of time and to trigger an alert when you open the terminal on a Mac.

date -r <target-file-path> +"%s"

↑ You can get the Unix timestamp (epoch seconds) of a file, so use this for calculation.

#!/usr/bin/env zsh
# Alert if a certain amount of time has passed since the last processing

cd "$(dirname $0)" || exit

now=$(date "+%s")
file_timestamp=$(date -r <target-file-path> +"%s")
delta=$(($now - $file_timestamp))

threshold=$((86400 * 35))
# echo "delta = ${delta}"
# echo "threshold = ${threshold}"

if [ ${delta} -gt ${threshold} ]; then
echo "More than 35 days have passed since the last processing."
echo "Please perform the necessary processing."
fi
Currently unrated
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.

Categories

Archive