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
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With