Bash Scripting: getting the last modified file with find or ls is not always working good

168 Views Asked by At

So I am doing a script that monitors if a certain IPCam is writing on disk or not.

I mount the shared folder where the Cam should write, then my goal is to find the last modified file (ignoring INFO and . files) to compare timestamps or to find files modified whithin 2 mins.

After some research, I came up with:

https://pastebin.com/ZEXMZvct

Where the part that I'm using to find the files is:

cd $(find . -maxdepth 1 -not -name '.*' -and -not -name '9*' -and -not -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" ")

Twice to find and change to the 2 more recent directories (there are TONS of files so I cant perform a global find)

and

LASTOFUS=$(find . -type f -name "*.jpg" -mmin -2 -not -name ".*" -and -not -type d -and -not -name "INFO*" -exec ls -ltr {} + | tail -1 | awk '{print $9}')

to find the .jpg files modified whithin 2 minutes excluding certain filenames.

Everything is working really good but every 20-30 times (i have 90 cameras so it's a REAL problem) $LASTOFUS results empty, like the find command cant find the file.

cd commands are almost always working, it's that find which fails. If i use ls alternatives, the result is exactly the same.

The strange thing is that at the next loop, with same parameters, the file will be found.

I tried (as u see in the script) to force a re-check, but it's not always fixed after 30secs and I can't allow 1 min or more for each camera, would be really inappropriate.

What am I doing wrong? Or what can I do to workaround this?

Thanks!

0

There are 0 best solutions below