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 [
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 ;-)
%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