list an amount of files for deleting

Hello again, I am using the find to list an X amount of files and then deleting it. But I just want to delete the olders one so this is what I do:
Code:
find /mnt/text/ -maxdepth 1 -mtime +3 -name '*.gz'-execdir rm -- '{}' \;

The idea is to left inside the folder only the 3 last files.
I have this working on Linux, but for some reason its not working "-mtime +3", does not matter if I write +3 or +10, whatever it always shows the same amount of files. Anyone knows what could be wrong or another option to do this task? Thanks
 
You don't have to construct a whole command, just use the -delete parameter, see also the find(1) manualpage ( man find). You'll also find the other options there at your disposal.

The reason why it includes all files is because mtime defaults to 24 hour units. So you've basically asked the system to find files which modification time differs 72 hours in comparison to the start of find. No modern computer is that slow these days ;)

So yeah, your answers lie in the manual page. Don't assume that the BSD tools work the same as Linux tools, because they usually don't.
 
You don't have to construct a whole command, just use the -delete parameter, see also the find(1) manualpage ( man find). You'll also find the other options there at your disposal.

The reason why it includes all files is because mtime defaults to 24 hour units. So you've basically asked the system to find files which modification time differs 72 hours in comparison to the start of find. No modern computer is that slow these days ;)

So yeah, your answers lie in the manual page. Don't assume that the BSD tools work the same as Linux tools, because they usually don't.
I will take a look at it and post the final line here so any other can use it if it's needed
 
Searching in the web I found that this command line should work but it doesnt:
Code:
find /mnt/text/ -maxdepth 1 -mtime +3 -name '*.gz'

But this command line for example works:

Code:
find /mnt/text/ -maxdepth 1 -mtime +11d -name '*.gz'
 
I was following along yesterday before the forum cert problem shut me down.
I find find to be my most used utility program so far.
What I learned is more about how nasty Seamonkey is and what files it is touching.
I was messing with this from the handbook:
find /mnt/ -type f -newerct '1minuite ago'
.
So have you tried the -newer*t option instead of -mtime?

Some of my recent history messing with this:
find / -newerct 'a fortnight ago' -print
find / -newerat '1minuite ago' -print
find /usr/src/ -newerat '30day ago' -name '*.gz'
 
Here is my -newer*t decoding of the handbook:
-newerat=last access time
-newerct=last change time time
-newerBt=inode creation time
-newermt=modification time
 
I was following along yesterday before the forum cert problem shut me down.
I find find to be my most used utility program so far.
What I learned is more about how nasty Seamonkey is and what files it is touching.
I was messing with this from the handbook:
find /mnt/ -type f -newerct '1minuite ago'
.
So have you tried the -newer*t option instead of -mtime?

Some of my recent history messing with this:
find / -newerct 'a fortnight ago' -print
find / -newerat '1minuite ago' -print
find /usr/src/ -newerat '30day ago' -name '*.gz'
Thanks a lot for sharing, I will try tomorrow at work. I have a folder with a backup file for each day, so I just want to keep the last 3 backups always. So I run cron each 7 days.
 
Thanks all, Phishfry your lines there are very useful and I will save it for another time. I finally solved using tail at the end, since my folder has a backup of everyday and I just need to delete the old 3 ones to keep disk space. So the line I used is this:

Code:
find /mnt/text/ -maxdepth 1 -name '*.gz'  | sort | head -n 3
 
In that case, just use ls:

ls -1 /mnt/text/*.gz | head -3 | xargs rm

:D

Exactly, that is what I am using now. Thanks so much to all of you for the help.
This for delete the old 3:
Code:
find /mnt/car/ -maxdepth 1 -name '*.gz' | sort | head -n 3 | xargs rm


And for copy the first one this:
Code:
find /mnt/car/ -maxdepth 1 -name '*.gz' | sort | tail -n 1 | xargs -I {} cp {} /mnt/ay/
 
Back
Top