Bash script find if file is created less then X time ago - bash

I want to have a bash script witch tells me if the file is created less than a 1h ago.
Any suggestion how I can achieve that?

You can use find command with -mmin option.
if [ `find path/to/file -mmin -60 | wc -c` -gt 0 ]; then
#found
fi

Related

Search inside folder, if no files found older than X Days output something

Wondering if someone can help, i seem to be in abit of a pickle,
I have a separate program responsible for zipping up data and uploading it to a set folder.
Each Client Site has its own Folder.
I have a bash script to search these folders and delete any files older than 7 days when there is a file count of 7 (I do not want to delete backups if the backup program is not running for example a PC dies and i am unaware of it)
But i would also like to be able to set a trigger that if no files are found older than 2 days, Then to trigger an event, I am struggling to do this part.
Here is an example of my code:
for dir in $(find /var/www/vhosts/XXXXXXXXXXX.co.uk/Backups/ -mindepth 1 -maxdepth 1 -type d); do # iterate over the directories
files=$(ls --ignore='.*' $dir | egrep '\.Zip$' | wc -l) # array of files in that directory
if (( $files > 6 )); then #If filecount 7 then delete some backups
echo $dir "Directory has "$files " files"
find $dir -mtime +7 -type f -delete #add -delete to delete the files
echo "Deleted Files older than 7 days old"
elif [[ -n "find $dir -mtime -2" ]]; then # Check if no uploads been present for more than 48 hours, if so trigger warning
echo $dir "Has not backed up for 2 days - trigger email"
fi
done
The Backup Folder Structure is as follow:
Backups/ClientName/20220217.Zip
Backups/ClientName/20220218.Zip
If client has multiple sites.
Backups/ClientName/Sitename2/20220218.Zip
Backups/ClientName/Sitename2/20220217.Zip
Backups/ClientName/Sitename1/20220218.Zip
Backups/ClientName/Sitename1/20220217.Zip
The code is basically duplicated twice using -mindepth 2 -maxdepth 3 to overcome the directory change.
All i am wanting to do is echo out the directory structure of the folder than has no backups in 48 hours.
From here i can write a trigger to send a email alert.
[[ -n "find $dir -mtime -2" ]] always evaluate as true because you are not running find, you are testing the string "find $dir -mtime -2" for emptiness. Run find and test the result:
res=$(find "$dir" -mtime -2)
if [[ -n "$res" ]]; then
Note:
files=$(ls --ignore='.*' $dir | egrep '\.Zip$' | wc -l)
is terrible. You should probably prefer something like:
files=("$dir"/*.Zip)
if (( ${#files[#]} > 6 )); then
That is, use the regular bash pathname expansion ("$dir"/*.Zip) to store the paths of all files of interest in bash array files, and then use the number of elements in the array (${#files[#]}).

BASH: Using a pipe to create a variable [duplicate]

This question already has answers here:
How do I redirect output to a variable in shell? [duplicate]
(8 answers)
bash: Assign variable from pipe?
(6 answers)
Closed 4 years ago.
I have a bash file that is looking to find files in a certain directory older than a certain date and delete them. It works fine and I'm able to echo the number of deleted files but I am having problems when I try to get the integer into a variable.
#!/bin/bash
# Make this dynamic to look at different directories.
pathtofolder=/var/www/website/temp2/
if [ $hours ]; then
# To specify older than one day it is better to talk in hours because
# the days integer is just an integer so everything less than 2 days
# would be 1 day, so 1 day 23 hours and 59 minutes is not greater than
# 1 day.
# For this reason I am using mmin and using the hours in minutes.
timeinmins=$(($hours*60))
elif [ $mins ]
then
timeinmins=$mins
else
# The default is 24 hours but we want to test with 24 minutes
timeinmins=24
fi
find "$pathtofolder"* -mmin +$timeinmins -exec rm -vr {} \; | output="$(wc -l)"
echo "Files deleted: $output"
echo "Minutes: $timeinmins"
In the above case, the $output is blank.
But this works, below, to just to echo...
find "$pathtofolder"* -mmin +$timeinmins -exec rm -vr {} \; | "Files deleted: $(wc -l)"
Any ideas? thanks in advance.

How to check if a file is older than 30 minutes in unix

I've written a script to iterate though a directory in Solaris. The script looks for files which are older than 30 minutes and echo. However, my if condition is always returning true regardless how old the file is. Someone please help to fix this issue.
for f in `ls -1`;
# Take action on each file. $f store current file name
do
if [ -f "$f" ]; then
#Checks if the file is a file not a directory
if test 'find "$f" -mmin +30'
# Check if the file is older than 30 minutes after modifications
then
echo $f is older than 30 mins
fi
fi
done
You should not parse the output of ls
You invoke find for every file which is unnecessarily slow
You can replace your whole script with
find . -maxdepth 1 -type f -mmin +30 | while IFS= read -r file; do
[ -e "${file}" ] && echo "${file} is older than 30 mins"
done
or, if your default shell on Solaris supports process substitution
while IFS= read -r file; do
[ -e "${file}" ] && echo "${file} is older than 30 mins"
done < <(find . -maxdepth 1 -type f -mmin +30)
If you have GNU find available on your system the whole thing can be done in one line:
find . -maxdepth 1 -type f -mmin +30 -printf "%s is older than 30 mins\n"
Another option would be to use stat to check the time. Something like below should work.
for f in *
# Take action on each file. $f store current file name
do
if [ -f "$f" ]; then
#Checks if the file is a file not a directory
fileTime=$(stat --printf "%Y" "$f")
curTime=$(date +%s)
if (( ( ($curTime - $fileTime) / 60 ) < 30 ))
echo "$f is less than 30 mins old"
then
echo "$f is older than 30 mins"
fi
fi
done
Since you are iterating through a directory you could try the below command which will find all files ending with log type edited in the past 30 min. Using:
-mmin +30 would give all files edited before 30 minutes ago
-mmin -30 would give all files that have changed within the last 30 minutes
find ./ -type f -name "*.log" -mmin -30 -exec ls -l {} \;

How to create shell script that compares File modification Time and current time on Solaris

I know I can use date +%s and stat in Linux system.
But as Solaris does not support those format or command for epoch,
how can I create a shellscript to compare file modification time and current time ?
modification file time should be within 10 min comparing to current time.
[ShellScript]
Current_Time=
Modification_Time=
Compare = Current_Time - Modification_time
if ["$Compare" -gt 600]; then
Echo "there is no find found within 10 min"
else
Echo "Found the file"
if
To know if somefile was modified in the last ten minutes, you can run this command:
file=somefile
if [ -n $(find . -name $file -mmin +10 2>/dev/null) ]; then
echo " $file was just modified"
else
echo " $file stayed unchanged"
fi
Find command should solve your problem, did you give a try?
$ find . -mmin 10 -print
-mmin n (modification time in minutes)

bash check core exists and is recent

I am looking for a simpler way of doing this:
#!/usr/bin/bash
FILE=core
DAYS=1
cd /programdir
if [ -f ${FILE} ]; then
agetest=$(find . -name "${FILE}" -type f -mtime +${DAYS} -print | wc -c)
if [[ agetest -eq 0 ]] ; then
echo "$FILE exists and is not older than ${DAYS} days."
fi
fi
I want to process a core file (using the dbx command) if the script finds it and the core file is recent (within 1 day). So I would run a dbx command where that echo statement is. It seems like there should be a way to do this in a more elegant way with 1 if statement, but I can't think of how to do that. Any ideas?
I know it would be easier to just clean up the old core files with tmpwatch or find/rm, but I'm not allowed to do that.
#!/usr/bin/bash
FILE=core
DAYS=1
if [ `find /programdir -name "${FILE}" -type f -mtime +${DAYS}` ]; then
echo "$FILE exists and is not older than ${DAYS} days."
fi

Resources