# list an amount of files for deleting



## bangmyhead (Jun 14, 2018)

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:

```
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


----------



## ShelLuser (Jun 14, 2018)

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.


----------



## bangmyhead (Jun 14, 2018)

ShelLuser said:


> 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


----------



## VladiBG (Jun 14, 2018)

`find /mnt/text/ -type f -mtime +3 -delete`


----------



## bangmyhead (Jun 15, 2018)

Searching in the web I found that this command line should work but it doesnt:

```
find /mnt/text/ -maxdepth 1 -mtime +3 -name '*.gz'
```

But this command line for example works:


```
find /mnt/text/ -maxdepth 1 -mtime +11d -name '*.gz'
```


----------



## Phishfry (Jun 15, 2018)

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'`


----------



## Phishfry (Jun 15, 2018)

There is also newerBt=Birth time
`find / -newerBt 'a fortnight ago' -name '*.conf'`

https://stackoverflow.com/questions...o-search-for-files-created-on-a-specific-date


----------



## Phishfry (Jun 15, 2018)

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


----------



## bangmyhead (Jun 15, 2018)

Phishfry said:


> 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:
> ...


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.


----------



## bangmyhead (Jun 18, 2018)

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:


```
find /mnt/text/ -maxdepth 1 -name '*.gz'  | sort | head -n 3
```


----------



## phoenix (Jun 19, 2018)

In that case, just use ls:

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


----------



## bangmyhead (Jun 19, 2018)

phoenix said:


> In that case, just use ls:
> 
> `ls -1 /mnt/text/*.gz | head -3 | xargs rm`



Exactly, that is what I am using now. Thanks so much to all of you for the help.
This for delete the old 3:

```
find /mnt/car/ -maxdepth 1 -name '*.gz' | sort | head -n 3 | xargs rm
```


And for copy the first one this:

```
find /mnt/car/ -maxdepth 1 -name '*.gz' | sort | tail -n 1 | xargs -I {} cp {} /mnt/ay/
```


----------

