My aim is to try to grep from the files which were modified one minute ago, i.e. in the above example grep from the files which were modified at 12:52, separately since they are different file (example am1slc01* and am1slc02*)
I have tried to build a script below in which it managed to take the last files modified (not one minute ago yet) however the grep won't work, it seems the grep works only on the first file
When echoing the files they are not listed as when making a simple 'ls' command but near each other so I'm suspecting that's the reason why it won't work.
I support it also needs to be clarified as to what you mean by "one minute ago"
Is it:-
Within the last minute (ie. less than 60 seconds old)
Current minute (probably very few if the time is 12:53:01 but more if it's 12:52:59)
Exactly one minute (up to seconds/milliseconds)
At least one minute but less than two minutes
Files timestamped as the previous minute to now (ie. if I'm at 12:53, I want all 12:52 files, even if they are 90 seconds old
Something else?
There are probably several ways to get these, but we need to know exactly your definition of "one minute ago" first so we don't go off on a wild chase.
The while loop seems to do the job, however I am now left with two or more outputs depending upon files found. For example when tested there were two files so grep was performed for both and output two values. Can these be summed up in the code?
Also this loop still takes the last files but not one minute ago. To clarify i am trying to achieve what Robin listed below:
Files timestamped as the previous minute to now (ie. if I'm at 12:53, I want all 12:52 files, even if they are 90 seconds old
---------- Post updated 07-07-16 at 12:01 PM ---------- Previous update was 07-06-16 at 06:34 PM ----------
Hi,
just found a dirty solution for this. copy the files based on date:
mydate=`date +"%H:%M"`
for i in `ls -lrt am1slc01_* | grep $mydate | awk '{print$9}'`; do cp $i /home/nms/scripts/CDRs/slc01; done
for i in `ls -lrt am1slc02_* | grep $mydate | awk '{print$9}'`; do cp $i /home/nms/scripts/CDRs/slc02; done
then do the required grep and delete files
Now the next step is to copy the files from one minute ago
You haven't said what you need that for, but i suppose you want to process "incoming" files somehow and want to prevent processing them twice.
In this case, wouldn't it be easier to move the files once they are processed? You wouldn't need to diddle with time stamps and complicated calculations and could adapt something like the following (sketch)
Two directories: /some/where/incoming and /some/where/processed , files go into "incoming"
while : ; do
ls /some/where/incoming | while read FILE ; do
process_file $FILE
mv $FILE /some/where/processed
done
sleep 5
done
Depending on what the process_file requirements are, you might gobble up the input to your while loop, especially if you have a while read loop. I'd suggest something more like:-
while :
do
for FILE in /some/where/incoming/*
do
process_file "$FILE"
mv "$FILE" /some/where/processed
done
sleep 5
done
If your files might have spaces in them, change the for loop to be:-