I want to delete log files which are older than 5 days from a directory. But deletion should not be based on the timestamp of file. It should be based on the name of file. For Example todays date is 07/05/2012 and the directory contains 10 files of names like ABC_20120430.log, ABC_20120429.log, ABC_20120502.log, ABC_20120320.log etc. I want to be able to remove the files by extracting the date from the name of the file.
Tell me more
×
Unix & Linux Stack Exchange is a question and answer site for
users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
|
|
||||
|
|
migrated from stackoverflow.com May 7 '12 at 11:54
|
Based on date from filename:
|
|||
|
|
I think @oHessling almost has it: Don't parse ls, and you can do more in bash:
|
|||||||||||||
|
|
One way using Content of
To test it I create some files:
Check them with
Run the script like:
With following output:
|
||||
|
|
|
What you could do instead is use the fact that your filenames will sort in chronological order. For instance, to keep the last 5 files:
|
|||||
|
