LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   strange behaving using find with cmin in script (https://www.linuxquestions.org/questions/linux-newbie-8/strange-behaving-using-find-with-cmin-in-script-914202/)

danishgambit 11-18-2011 03:37 AM

strange behaving using find with cmin in script
 
Morning,

hoping you might be able to help with some strange (to me) behaviour I'm seeing in one of the scripts I use.

we have a process that archives off 3 files every 30 mins. My script runs every 30 mins checking for files created in the last 30 mins and scp's them elsewhere.

The script generally runs fine, transferring the expected files across, but every evening when it runs at about 11.30, it sends every file in the sub-directories across, and looking at the date/time stamps against the files I've no idea why.

the script excerpt is

CheckInterval=30
for FileToSend in `find . -type f -cmin -$CheckInterval`
do
if scp -rp $FileToSend......
.....
done


TIA

DanishGambit.

Nominal Animal 11-18-2011 06:16 AM

-cmin does not look at file creation time, it looks at the last status change. If you use e.g. SELinux, you might have a security context setting script that runs then. That would be a status change.

Try last modification time, -mmin instead.

Also, you do realize your script will break if you have a file with whitespace in the name? If you use Bash, then you might consider
Code:

find . -type f -mmin -$CheckInterval -print0 | while read -rd "" FileToSend ; do
    if scp -p "$FileToSend" ...
done

which will work correctly for all file names in Linux. Note that the body of the while loop is run in a subshell, so no changes to variables are seen outside the loop. If you need to pass variables outside the loop body, use form
Code:

while read -rd "" FileToSend ; do
    if scp -p "$FileToSend" ...
done < <(find . -type f -mmin -$CheckInterval -print0)

instead.

danishgambit 11-18-2011 07:29 AM

Hi Nominal Animal,

thanks for the response, I'll change the script to use mmin tonight and see how it goes. Whitespace isn't an issue in this particular instance due to the naming convention used by the file-generating process, but it's something I'll look at incorporating for other scripts, so good tip.

Cheers,

DanishGambit.

danishgambit 11-21-2011 03:07 AM

solved
 
Morning,

just to confirm that I've tried my script using mmin rather than cmin and that's sorted the issue.

Thanks,

DG.


All times are GMT -5. The time now is 09:51 PM.