I want a shell script that keep all file with time-stamp of the same month as the current time + the latest file before that, and delete the rest of the files in the directory.
All filename stored in the directory are structured as
So, this means that if there is the following file in the directory:
name161214-082211.gz name161202-082211.gz name161020-082211.gz name161003-082211.gz name161001-082211.gz
The remaining file in the directory after the execution of this code will be:
name161214-082211.gz name161202-082211.gz name161020-082211.gz
PS. Extremely new to shell. Would love to not only have a working code, but also to learn. So, if you would be so kind, please explain the code as well. Thank you!