C-shell: find file more that 12 hours - find

I am using solaris and i need to find file that more than 12 hours.
In solaris, there is no -mmin function. So, what i do is:
set timer = "/admin/timer.txt"
echo date > $timer
find . -type ! newer $timer
But this only work I set the script run every 12 hours.
The thing is I need to run the script every 1 hours but I couldn't find a way how to do it.
Any suggestions?

You can invoke a shell function to do the test. Something like:
now=$(perl -e 'print time')
find . -exec sh -c 'expr $1 - $(stat -f %m "$2" ) \< 43200 > /dev/null' _ $now {} \; -print

Related

How to replace date -d from script running on Linux to get it working on AIX

On a Linux system I'm running a bash script what is running fine, but now I want to use the same script on an AIX system.
Only a small part of it isn't able to run because on the AIX system the command "date -d" is not working.
Below is the code that is running on the linux server, baised on the hostname in this case system1 and system2 a script will run only on a Sunday at the given time.
declare -A myservertime
myservertime["system1"]="02:00"
myservertime["system2"]="02:10"
for keys in "${!myservertime[#]}";
do
if [[ "$keys" == "$HOSTNAME" ]]; then
mylaunchtime=${myservertime["$keys"]}
fi
done
if [ -z "$mylaunchtime" ]; then
echo "Server not found."
exit
fi
timenow=$(date +"%s")
weekday=$(date +"%a" -d #$timenow )
timerun=$(date +"%s" -d ${mylaunchtime} )
if [ $timerun -lt $timenow ]; then
timerun=$(( timerun + 86400 ))
fi
sleep_time=$(( timerun - timenow ))
#Check is weekday = Sunday
if [ $weekday == "Sun" ]; then
#If Sunday then wait until starttime is reached, until then sleep
sleep $sleep_time
#If starttime is reached execute startprogram
startprogram
fi
I already fixed the declare part, this is also not working on AIX, to the following:
typeset -A myservertime=(["pvm00066"]="02:30" ["pvm00100"]="01:30")
But I have troubles to get the variables $weekday and $timerun working, because of that "date -d $variable"
Anyone here that can help me with this, I tried and searched all kind of stuff but I was unable to get one working.
When you do want to have an identical crontab and script on all systems, and you only want to run the script on Sunday, just use an offset from 00:00.
Crontab:
0 0 * * 7 start_with_delay.sh
start_with_delay.sh:
declare -A myservertime
myserversleep["system1"]=7200
myserversleep["system2"]=7800
sleep ${myserversleep[HOSTNAME]}
startprogram
When you do not need fixed starting times for the different hosts, but only want different hosts to have different starting times, you can consider using the the last number of the IP as an input for calculating the sleep time.
Two reasons why you might consider another solution:
Starting a job with crontab, that starts with sleeping, is very confusing.
An operator reading the crontab expects something to start at the time given in the crontab.
Using a scriptname like sleep_and_start helps a little.
When the system reboots at Sunday 01:00, nothing is started at 02:00
The sleep command has been stopped during the reboot.
I would try to find a smart install script, that will write a different crontab on each host. Something with Ansible, a Puppet template or shellscript.
case $HOSTNAME in
system1)
starthour=2
startminute=0
;;
system2)
starthour=2
startminute=10
;;
*)
echo "No scheduled startprogram on ${HOSTNAME}.
exit 0
;;
esac
echo "${startminute} ${starthour} * * SUN startprogram" | crontab -
When you want to update en existing crontab, you might want something like
(crontab -l 2>/dev/null && echo "${startminute} ${starthour} * * 7 startprogram") |
sort -u | crontab -

how to get number of files older than 1 hour on ksh HP-UX

I need to list set of files created older than 1 hour in certain folder of HP-UX. Following is the command i tried.
find . -type f -mmin +60 | wc -l
But it return following error for ksh
find: bad option -mmin
What is the alternative option to get number of files older than 1 hour?
Even i tried following command. Still another error. But it also work on bash
find . -type f -mtime +0.04 | wc -l
find: Error in processing the argument 0.04
find in HP-UX has no options for minutes, mtime takes days as argument.
You can create a testfile, "touch" it with the desired time and then compare with ! -newer[m]. For instance:
# onehourago=`date +"%m %d %H %M" | awk '{ onehourago=$3 - 1 ; if (onehourago<0) { onehourago=59 } printf("%.2d%.2d%.2d%.2d\n",$1,$2,onehourago,$4) }'`
# touch -t "$onehourago" testfile
# find . -type f ! -newer testfile | wc -l

Split all of the files in a directory by 1000 lines and process them through a perl script

I used this line to try and split all of the files in a directory into smaller files and put them in a new directory.
cd /path/to/files/ && find . -maxdepth 1 -type f -exec split -l 1000 '{}' "path/to/files/1/prefix {}" \;
The result was 'no file found', so how do I make this work so that I split all of the files in a directory into smaller 1000-line files and place them in a new directory?
Later on...
I tried many variations and this is not working. I read another article that split cannot operate on multiple files. Do I need to make a shell script, or how do I do this?
I had a bright idea to use a loop. So, I researched the 'for' loop and the following worked:
for f in *.txt.*; do echo "Professing $f file..."; split -l 1000 $f 1split.ALLEMAILS.txt. ; done
.txt. is in all of the files in the working directory. the 'echo' command was optional. for the 'split' command, instead of naming one file, I replaced that with $f as defined by the 'for' line.
The only thing I would like to have been able to do is move all of these to another directory in the command.
Right now, I am stuck on the find command for moving all matching files. This is what I have done so far that is not working:
find . -type f -name '1split.*' -exec mv {} new/directory/{} \;
I get the error ' not a directory ' ; or I tried:
find . -type f -name '1split.*' -exec mv * . 1/ \;
and I get ' no such file or directory '
Any ideas?
I found that this command moved ALL of the files to the new directory instead of the ones specifically meeting the criteria '1split.*'
So, the answers to my questions are:
for f in *.txt.*; do echo "Professing $f file..."; split -l 1000 $f 1split.ALLEMAILS.txt. ; done
and
mv *searchcriteria /new/directory/path/
I did not need a find command for this after all. So, combining both of these would have done the trick:
for f in *.txt.*; do echo "Professing $f file..."; split -l 1000 $f 1split.ALLEMAILS.txt. ; done
mv *searchcriteria /new/directory/path/ | echo "done."
---later on...
I found that this basically took 1 file and processed it.
I fixed that with a small shell script:
#!/bin/sh
for f in /file/path/*searchcriteria ; ## this was 'split.*' in my case
do echo "Processing $f in /file/path/..." ;
perl script.pl --script=options $f > output.file ;
done ;
echo "done."

Escape the current file name "{}" of shell find -exec in the subcommand result "$()"?

I want to run something like:
find . -type files -exec echo "touch -cmd '"$(date --utc -r '{}' +"%Y-%m-%d %H:%M:%S.%N +0000")"' "$(ls --quoting-style=shell '{}')
It doesn't seem to work since "{}" doesn't seem to be expanded in the $(). How could I do to make it work right?
Simplify your problem.
Write a shell script, and then get the shell script working. Then execute your shell script using find.
find . -type files -exec myScript '{}'

How to get files which are created one hour ago in solaris 5.9

I want to get files which are created one hour ago, i tried following command
/usr/bin/find /home/FILES/ -name '*.xml' -atime +.0417 -exec ls -l{} \;
In the above command .0417 is (1 day /24 hours ).
The find command which i am using does not have -mmin option.
Is there a way to get files created less one hour ago.
set the file time to 1 hours ago
touch -t 201410042236 /tmp/hourold.tmp;
/usr/bin/find /home/FILES/ -name '*.xml' /tmp/hourold -type f -exec ls -l {} \;