122

I want to check in linux bash whether a file was created more than x time ago.

let's say the file is called text.txt and the time is 2 hours.

 if [ what? ]
 then
     echo "old enough"
 fi

9 Answers 9

167

Only for modification time

if test `find "text.txt" -mmin +120`
then
    echo old enough
fi

You can use -cmin for change or -amin for access time. As others pointed I don’t think you can track creation time.

10
  • 6
    you can drop 'test' and the backticks. This is analogous to 'if (test == true)' vs 'if (test)'
    – guns
    Commented Sep 9, 2009 at 19:51
  • 2
    You're correct; I suppose I was thinking that find returned non-zero when no matches are found.
    – guns
    Commented Dec 15, 2011 at 0:37
  • 6
    You can drop the backticks and do if test $(find text.txt -mmin +120) Commented Dec 14, 2013 at 8:30
  • 1
    @BroSlow look better: there is no "more than one" possible. Of course, text.txt is presumed not to be a directory.
    – kmkaplan
    Commented Jul 4, 2014 at 16:06
  • 7
    I prefer this syntax: [ "$(find path/to/file -mmin +120)" ] && echo "it is old"
    – Alfe
    Commented Sep 1, 2015 at 20:41
52

I always liked using date -r /the/file +%s to find its age.

You can also do touch --date '2015-10-10 9:55' /tmp/file to get extremely fine-grained time on an arbitrary date/time.

8
  • I was looking for same thing today. Best!
    – Umair A.
    Commented Jan 4, 2014 at 20:26
  • Thanks. I landed on this SO question because I googled "how to print age of file bash." So this is exactly what I was looking for! Commented Sep 16, 2015 at 18:52
  • 7
    Thanks! with the only caveat that the above is a timestamp (not age) but can be used to calc the age like so let AGE=$(($(date +%s)-$(date -r /path/to/file +%s))) [with the assumption that created and never modified, or "age relative to mod"]
    – nhed
    Commented Nov 16, 2016 at 17:58
  • function file-age { FILE_CREATED_TIME=`date -r "$1" +%s`; TIME_NOW=`date +%s`; echo "$[ ${TIME_NOW} - ${FILE_CREATED_TIME} ]"; }
    – turiyag
    Commented Apr 28, 2018 at 18:50
  • 1
    @turiyag Obligatory note that $[ ] is, if not deprecated, at least old and less portable, and one should use $(( )) instead; and backquotes are more fragile than $( ); and also hyphens in function names may not be fully portable. The essence of it is good, though! Commented Mar 18, 2019 at 3:54
34

Using the stat to figure out the last modification date of the file, date to figure out the current time and a liberal use of bashisms, one can do the test that you want based on the file's last modification time1.

if [ "$(( $(date +"%s") - $(stat -c "%Y" "$somefile") ))" -gt "7200" ]; then
   echo "'$somefile' is older then 2 hours"
fi

While the code is a bit less readable then the find approach, I think its a better approach then running find to look at a file you already "found". Also, date manipulation is fun ;-)


  1. As Phil correctly noted creation time is not recorded, but use %Z instead of %Y below to get "change time" which may be what you want.

[Update]

For mac users, use stat -f "%m" "$somefile" instead of the Linux specific syntax above

10
  • What shell are date and stat built in to? My bash (GNU, 3.2.48) doesn't have them. Commented Sep 9, 2009 at 19:35
  • 1
    @Guss right solution is to install coretutils with brew. :-) Commented Feb 14, 2015 at 23:29
  • 1
    @ErikJohansson I prefer not to require installation of additional software when using the existing one works fine - this isn't Windows, you know ;-)
    – Guss
    Commented Feb 16, 2015 at 9:24
  • 1
    For people who want to use this but modify it, it would be good to know exactly what it is doing: $(( $(date +"%s") -- seconds since 1970-01-01 00:00:00 UTC (Epoch) $(stat -c "%Y" $somefile) )) -- Time of last modification as seconds since Epoch -gt "7200" -- checks if the difference is greater than x number of seconds.
    – devo
    Commented Mar 2, 2016 at 18:38
  • 1
    You should always quote variables that might contain spaces or special characters, e.g. "$somefile". (It's the only parameter there that really needs quoting.)
    – mwfearnley
    Commented Apr 4, 2022 at 16:16
17

Creation time isn't stored.

What are stored are three timestamps (generally, they can be turned off on certain filesystems or by certain filesystem options):

  • Last access time
  • Last modification time
  • Last change time

a "Change" to the file is counted as permission changes, rename etc. While the modification is contents only.

13

Although ctime isn't technically the time of creation, it quite often is.

Since ctime it isn't affected by changes to the contents of the file, it's usually only updated when the file is created. And yes - I can hear you all screaming - it's also updated if you change the access permissions or ownership... but generally that's something that's done once, usually at the same time you put the file there.

Personally I always use mtime for everything, and I imagine that is what you want. But anyway... here's a rehash of Guss's "unattractive" bash, in an easy to use function.

#!/bin/bash
function age() {
   local filename=$1
   local changed=`stat -c %Y "$filename"`
   local now=`date +%s`
   local elapsed

   let elapsed=now-changed
   echo $elapsed
}

file="/"
echo The age of $file is $(age "$file") seconds.
5
  • This was useful, but it didn't work right away for OSX because stat is implemented differently on it. The only cross-system compatible way I found was to use perl. The script should work on Macs if you replace the corresponding line with this: local changed=$(perl -MFile::stat -e "print stat(\"${filename}\")->mtime") superuser.com/questions/427551/… Commented May 29, 2013 at 23:45
  • 2
    "Creation time" is a concept hard to grasp. What does it mean for a copied file? What for a moved file? What if it was moved across a device border (so it got created anew, in a sense)? What does it mean for a packaged and later unpackaged file? Several answers are possible here and no consensus exists. Mentioning the ctime is nice, though, but I wouldn't put it semantically near the vague concept of a "creation time". Especially since a simple chmod, applied on the file, will nullify that idea. But I can live with you doing that ;-)
    – Alfe
    Commented Oct 6, 2014 at 10:08
  • @Alfe: You're missing the +%s from the second half
    – Grizly
    Commented Oct 8, 2014 at 0:19
  • That age can be way shorter: age() { echo $(( $(date +%s) - $(date -r "$1" +%s) )); } (Thanks to @Grizly for the correction!)
    – Alfe
    Commented Oct 8, 2014 at 7:39
  • Actually it's better to use access time which is a creation time on all file systems mounted with noatime option (which is good for performance so you should use it). So just use %X instead of %Y.
    – Nux
    Commented May 26, 2015 at 7:37
9

The find one is good but I think you can use anotherway, especially if you need to now how many seconds is the file old

date -d "now - $( stat -c "%Y" $filename ) seconds" +%s

using GNU date

2
  • 1
    use gstat, "brew install coreutils" Commented Feb 14, 2015 at 23:26
  • Age in days: echo $(( $(gdate -d "now - $( gstat -c "%Y" $1 ) seconds" +%s) / 86400 ))
    – wytten
    Commented Aug 26 at 18:35
5

Consider the outcome of the tool 'stat':

  File: `infolog.txt'
  Size: 694         Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d  Inode: 11635578    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/     fdr)   Gid: ( 1000/     fdr)
Access: 2009-01-01 22:04:15.000000000 -0800
Modify: 2009-01-01 22:05:05.000000000 -0800
Change: 2009-01-01 22:05:05.000000000 -0800

You can see here the three dates for Access/modify/change. There is no created date. You can only really be sure when the file contents were modified (the "modify" field) or its inode changed (the "change" field).

Examples of when both fields get updated:

"Modify" will be updated if someone concatenated extra information to the end of the file.

"Change" will be updated if someone changed permissions via chmod.

1
  • granted, stat can tell you relevant information. But it will be quite tedious to parse in order to get what we want.
    – PypeBros
    Commented Dec 7, 2017 at 9:31
0

I use

file_age() {
    local filename=$1
    echo $(( $(date +%s) - $(date -r $filename +%s) ))
}

is_stale() {
    local filename=$1
    local max_minutes=20
    [ $(file_age $filename) -gt $(( $max_minutes*60 )) ]
}

if is_stale /my/file; then
    ...
fi
0

Since coreutils 8.31, glibc 2.28 and kernel version 4.11 or newer, stat returns the creation date of a file. To retrieve the creation date

file_creation_date=`stat -c '%w' text.txt`

We retrieve the time it was two hours ago

two_hours_ago=`date -u +"%F %T" -d '2 hours ago'`

And, we compare the two dates

if [[ $file_creation_date > $two_hours_ago ]]; then
    echo "older"
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.