---
slug: "シェルスクリプトでファイルが判定日時より古かったらなにかをする"
title: "If the File is Older Than the Specified Date, Do Something in Shell Script"
description: "How to use the `date` command to retrieve the timestamp of a file's last modification date.\nThis article was written to determine tasks that have not been processed for a certain period and to issue an alert when opening the terminal on a Mac."
url: "https://www.ytyng.com/en/blog/シェルスクリプトでファイルが判定日時より古かったらなにかをする"
publish_date: "2022-05-28T07:29:33Z"
created: "2022-05-28T07:29:33Z"
updated: "2026-02-27T00:07:09.358Z"
categories: ["Shellscript(Bash/Zsh)"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20230812/ab9d425c0d654442b8d2e9ed5142a454.png.webp?width=768"
has_video: false
has_music: false
video_urls: []
music_urls: []
lang: "en"
---

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

<p>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.</p>
<p></p>
<pre>date -r &lt;target-file-path&gt; +"%s"</pre>
<p>&uarr; You can get the Unix timestamp (epoch seconds) of a file, so use this for calculation.</p>
<p></p>
<pre>#!/usr/bin/env zsh<br /># Alert if a certain amount of time has passed since the last processing<br /><br />cd "$(dirname $0)" || exit<br /><br />now=$(date "+%s")<br />file_timestamp=$(date -r &lt;target-file-path&gt; +"%s")<br />delta=$(($now - $file_timestamp))<br /><br />threshold=$((86400 * 35))<br /># echo "delta = ${delta}"<br /># echo "threshold = ${threshold}"<br /><br />if [ ${delta} -gt ${threshold} ]; then<br />  echo "More than 35 days have passed since the last processing."<br />  echo "Please perform the necessary processing."<br />fi</pre>
