I have a server running Ubuntu 20.04.3. On this server there is a process which dumps a MySQL database and then tar
's the files once per day for backup purposes.
My goal is to write a script which deletes any backups that are > 30 days old.
In order to test this out I decided to set something up locally on my Macbook (running macOS Big Sur). In my Terminal I did the following:
Created a directory on the Desktop to test my work:
% cd ~ % mkdir Desktop/testfiles % cd Desktop/testfiles
Created 4 files:
% touch a.txt b.txt c.txt d.txt
Modify the files created in (2) so the created/modified timestamp is different on each one. This effectively means each file has been created on a different day, e.g.
touch -t 202201162300 b.txt
meansb.txt
was created/modified on 16th January 2022. The commands I ran are below:% touch -t 202201152300 a.txt % touch -t 202201162300 b.txt % touch -t 202201172300 c.txt
The file d.txt
remains with today's timestamp.
Check that (3) has worked:
% ls -l -rw-r--r-- 1 a.user staff 0 15 Jan 23:00 a.txt -rw-r--r-- 1 a.user staff 0 16 Jan 23:00 b.txt -rw-r--r-- 1 a.user staff 0 17 Jan 23:00 c.txt -rw-r--r-- 1 a.user staff 0 18 Jan 15:45 d.txt
So as per (4) each of these files was created on a different day.
If I then switch back to my home directory, cd ~
and run any of the following commands there is no output:
% find Desktop/testfiles/* -mtime +2
% find Desktop/testfiles/* -mtime +3
I assumed the output of these would be that it would find files created more than 2 and 3 days ago respectively. So the output I was assuming was for -mtime +2
would give b.txt
and c.txt
whereas -mtime +3
would give a.txt
, b.txt
and c.txt
.
Am I doing something wrong here? When I read https://unix.stackexchange.com/a/112407 my understanding was that the plus (e.g. +2
) is the right option because I want files "older than" 2 days, in this example. I do not think minus (e.g. -2
) is appropriate in my situation unless I've misunderstood.
I also am assuming this works the same in the zsh terminal on my Mac and Ubuntu.
If I did get this to work my plan was to use this on the Ubuntu server:
% find /path/to/files/on/server/* -mtime +30 -exec rm {} \;
But I'm reluctant to use this because when I'm trying to find the appropriate files locally it's not giving me the files I expect.
Any help here would be appreciated.
-mtime +2
means "3 or more full periods of 24 hours have passed".