If file modification date is older than N days
Several approaches are available. One is just to ask find
to do the filtering for you:
if [[ $(find "$filename" -mtime +100 -print) ]]; then
echo "File $filename exists and is older than 100 days"
fi
Another is to use GNU date to do the math:
# collect both times in seconds-since-the-epoch
hundred_days_ago=$(date -d 'now - 100 days' +%s)
file_time=$(date -r "$filename" +%s)
# ...and then just use integer math:
if (( file_time <= hundred_days_ago )); then
echo "$filename is older than 100 days"
fi
If you have GNU stat, you can ask for a file's timestamp in seconds-since-epoch, and do some math yourself (though this will potentially be a bit off on the boundary cases, since it's counting seconds -- and not taking into account leap days and such -- and not rounding to the beginning of a day):
file_time=$(stat --format='%Y' "$filename")
current_time=$(( date +%s ))
if (( file_time < ( current_time - ( 60 * 60 * 24 * 100 ) ) )); then
echo "$filename is older than 100 days"
fi
Another option, if you need to support non-GNU platforms, is to shell out to Perl (which I'll leave it to others to demonstrate).
If you're interested more generally in getting timestamp information from files, and portability and robustness constraints surrounding same, see also BashFAQ #87.
Check the existence of file from 9 to 9:30 am using shell script and stop checking if file is found any time in between
Something like this run just once at the start of the 30 minute period from cron
:
#!/bin/bash
MESSAGE="Some message"
SUBJECT="Some subject"
RECIPIENT="somebody@somewhere.com"
# Test for 30 minutes
for ((i=0;i<7;i++)) ; do
if [ -f /home/test/"$1" ]; then
echo "File /home/test/$1 exists”
exit 0
fi
# Sleep 5 minutes
sleep 300
done
# Failed to find file in specified time
echo "$MESSAGE" | mailx -s "$SUBJECT" $RECIPIENT
A slightly different way of doing it is like this:
#!/bin/bash
MESSAGE="Some message"
SUBJECT="Some subject"
RECIPIENT="somebody@somewhere.com"
start=$SECONDS
# Test for 30 minutes
while : ; do
if [ -f "/home/test/" ]; then
exit 0
fi
((elapsed=SECONDS-start))
# Break out of loop if 30 minutes have elapsed
[ $elapsed -ge 1800 ] && break
# Sleep 5 minutes
sleep 300
done
# Failed to find file in specified time
echo "$MESSAGE" | mailx -s "$SUBJECT" $RECIPIENT
how do I check in bash whether a file was created more than x time ago?
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.
Need a way to check content of file (time data) is more than 5 minutes or not
First, I understand that on line 1 you're trying to have the latest file. I would rather have the file created always with the same name, then at the end of the script, after processing it, mv to another name (like myfile.txt.20200101). So you'll always know that myfile.txt is the latest one. More efficient than doing ls. Then again, you could improve your line with : (it's the number one, not the letter "l")
var10=$(ls -1rt /projects/Informatica/INFA_ADMIN/LogFiles/myfile.txt)
From your line 2, I assume the original file is space delimited. Actually, you might use that to your advantage instead of adding commas. Also, using a for loop with "cat" is less performant than using a while loop. For the time comparison, I would use a little trick. Transforming your threshold and reading, in second since epoch then compared them.
MyTreshold=$(date -d 00:05:00 +"%s")
while read -r line; do
MyArray=( $line )
MyReading=$(date -d ${MyArray[5]} +"%s")
#the array starts a index 0
if [[ ${MyArray[1]} -ge 75 && $MyReading -gt $MyThreshold ]]; then
<LOGIC>
fi
done <$var10
linux script to check if file exists at a particular directory more than an hour
I think you should try this:
#!/bin/bash
if [[ `find /path/to/my/directory/ -type f -mmin +60 | wc -l` -gt 0 ]];then
echo "files do exist"
else
echo "files do not exist"
fi
Explain: i will find all files under directory: /path/to/my/directory/ with modify time greater than 60 minutes then count them.
How to delete files older than X hours
Does your find
have the -mmin
option? That can let you test the number of mins since last modification:
find $LOCATION -name $REQUIRED_FILES -type f -mmin +360 -delete
Or maybe look at using tmpwatch
to do the same job. phjr also recommended tmpreaper
in the comments.
Related Topics
How to Correctly Install Wkhtmltopdf on Debian 64 Bit
Call to Daemon in a /Etc/Init.D Script Is Blocking, Not Running in Background
Linux Dlopen: Can a Library Be "Notified" When It Is Loaded
Whiptail: How to Redirect Output to Environment Variable
How to Add a Cron Job in Linux
Writing a Persistent Perl Script
Looping a Video with Gstreamer and Gst-Launch
Why Is This Kernel Module Marked at Permanent on 2.6.39
How to Listen for Mouse Events in Linux
Linux Debian Crontab Job Not Executed
How to Grep Download Speed from Wget Output
Rename Files to Md5 Sum + Extension (Bash)
Not Authorized for Query on Admin.System.Namespaces on Mongodb
Matlab on Linux Can't Plot Anything(Can't Load Libstdc++.So.6: Version 'Cxxabi_1.3.8' Not Found)