command for deleting log files based on some condition

Hello,

Can anyone pls. provide me with the command for deleting files older then 15 days with a restriction to keep at least 5 files in a directory even if they are older then 15 days.

Any help will be highly appreciated.

Thanks,
Pulkit

What is the reason to this on your system, or is it homework?

Regards

Yes its a kind of assignment...:slight_smile:

Ok, some hints:

To find older files you can use the find command with the -mtime option.
To keep 5 files of the selection you can pipe the result to awk, to ignore the first 5 files you can do something like:

awk 'NR<6{next}{print}'

So it should look like:

find <options> | awk '....' | rm -f

If you have many files you can use xargs.

...| xargs rm -f

Check the man page of find for the options, search on this forum or Google for examples.

Regards

Thanks in Tons...:slight_smile: