Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I check in bash whether a file was created more than x time ago?

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 
like image 886
flybywire Avatar asked Feb 16 '09 09:02

flybywire


People also ask

How can I tell when a file was created in bash?

The easiest way to get the file creation date is with the stat command. As we can see, the creation date is shown in the “Birth” field.

How can you tell how old a file is in Linux?

We can search for files based on their modified, accessed, or changed time using the find tool with the -mmin, -amin, and -cmin options, respectively, for these timestamps.

How can I tell what time a file was modified?

To see the access time for a file with ls , append the -u option in your command. In this case, our access time is the same as the file's modified time, which is normal for files that have not been accessed since they were last saved.

How can I tell if a file has been modified Linux?

You can use the stat command on a file to check access and modification times or set up RCS to track changes. You can use MD5 or sum to get the current state of the file, copy that value to a file and then that file to verify that the original file wasn't changed.


1 Answers

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.

like image 161
kmkaplan Avatar answered Sep 19 '22 11:09

kmkaplan