简体   繁体   中英

How do I keep latest 8 backup file and delete the older one

How do I keep the latest 8 backup files and delete the older one

backup-Y-M-D.zip
backup-Y-M-D.zip
backup-Y-M-D.zip
backup-Y-M-D.zip
.
.
backup-Y-M-D.zip

There are about 80 files having .zip extension all I wanted to do is to keep latest 8 files according to the date on which created. I also tried logrotate but failed to rotate logs as it is not doing anything. Below down is the config file of logrotate.

/root/test/*.zip {
    daily
    missingok
    extension .zip
    rotate 4
    nocompress 
}

If the naming convention is guaranteed you could just rely on the alphabetical ordering of the files when expanding a glob pattern to get the oldest or newest files. According to Filename Expansion :

After word splitting, unless the -f option has been set (see The Set Builtin), Bash scans each word for the characters '*', '?', and '['. If one of these characters appears, and is not quoted, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of filenames matching the pattern (see Pattern Matching).

Demo:

[user@hostname]$ touch backup-2022-06-14.zip backup-2022-06-13.zip backup-2021-07-04.zip
[user@hostname]$ echo *
backup-2021-07-04.zip backup-2022-06-13.zip backup-2022-06-14.zip

You can leverage this to get a list of files other than the last N elements:

[user@hostname]$ all_files=(*)
[user@hostname]$ old_files=( "${all_files[@]:0:${#all_files[@]}-1}" ) #change -1 to -8 if you want to keep the 8 newest
[user@hostname]$ echo "${old_files[@]}"
backup-2021-07-04.zip backup-2022-06-13.zip

And then do whatever you want with that list, such as remove it with rm "${old_files[@]}" .

One way to do this is with the following one-liner, ran from the directory where the logs are located:

ls -t | head -n -8 | xargs --no-run-if-empty rm

Explanation:

  • ls -t - lists all the files in order from youngest to oldest
  • head -n -8 - gets all the files except the first 8
  • xargs --no-run-if-empty rm - deletes the selected files if there are any, preventing errors if you ever have fewer than 8 logs

If you want to set this up to run automatically every day, giving you peace of mind in case your server is offline on the 7th day of a cycle and misses the one week mark, run crontab -e and add the following to your jobs:

0 0 * * * cd yourDirNameHere && ls -t | head -n -8 | xargs --no-run-if-empty rm

Then the log cleaner will be ran every night at midnight.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM