What's new

Delete USB files over x days old

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

brooky

Occasional Visitor
Hi,

I've setup a cron to delete files on a mounted USB drive over than x amount of days old. I'm testing it right now with 1 day:

Code:
find /tmp/mnt/NAS/cctv/FI9821W_00626E4D07ED/record/* -mtime +1 -exec rm
{} \;

When I run that code in the SSH terminal nothing happens. No errors and files older than 1 day are still listed afterwards.

Code:
admin@NAS:/tmp/mnt/NAS/cctv/FI9821W_00626E4D07ED/record# ls -la
drwxrwxrwx    2 admin    root         32768 Aug 13 14:39 .
drwxrwxrwx    3 admin    root         32768 Aug 12 09:33 ..
-rwxrwxrwx    1 admin    root       6502814 Aug 12 16:00 alarm_20150812_114125.mkv
-rwxrwxrwx    1 admin    root       6621337 Aug 12 11:49 alarm_20150812_124858.mkv
-rwxrwxrwx    1 admin    root       7935728 Aug 13 08:35 alarm_20150813_093454.mkv
-rwxrwxrwx    1 admin    root       7883712 Aug 13 10:54 alarm_20150813_115355.mkv
-rwxrwxrwx    1 admin    root       1875573 Aug 13 14:40 alarm_20150813_153941.mkv

Can anyone see what I'm doing wrong?
 
Interestingly;

Code:
find /tmp/mnt/NAS/cctv/FI9821W_00626E4D07ED/record/* -mtime -1 -exec rm
{} \;

deleted all the files less than one day old. Hmmm..
 
When I run that code in the SSH terminal nothing happens. No errors and files older than 1 day are still listed afterwards.
That is standard behaviour for find. See the man page and/or google for mtime and atime. i.e.
-atime n
File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago.


PS Your usage of "/tmp/mnt/NAS/cctv/FI9821W_00626E4D07ED/record/*" is wrong. You should be specifying a path rather than a file mask. If you also want to match specific file names you should use the "-name" option.

What you have will work most of the time but it is not guaranteed. If for example, your "/tmp/mnt/NAS/cctv/FI9821W_00626E4D07ED/record/*" matched 100 files then the shell would replace that string with each of the 100 filenames (each including the path) separated by spaces before passing it to find. As you can imagine, there will come a point where the expanded command is too big to fit into the shells input buffer.
 
Last edited:

Similar threads

Latest threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top