I have a backup script that compresses various files and directories and creates .tgz archives. Files are named, e.g.


I want to manage these files so only the last 5 backups are kept, with older files being deleted.

Can I use logrotate to do this? They aren't log files and are already compressed. They're in /root and not in /var/log - can I still use it?


5 Answers 5


Logrotate rotates files, so the answer is yes - probably, and if no sufficient permissions then place them in /backup or something. Check what group and user the rotated logs have :-).

There's options for compression in logrotate, så if "compress" is NOT configured - well then it won't try. Also in your case, "rotate 5" option.

Take a look in /etc/logrotate.d (or where ever it's stored in your system)


Without a change to your process, logrotate on its own will not do what you're looking for here. The key problem here is that, while logrotate can take wildcards, it will not treat the files as one if you do so and will instead attempt to rotate all of them individually, which is definitely NOT what you want.

You can, however, make it work the way you describe as long as the most recent backup is created without a date stamp. If you backup process creates /root/backup.tar.gz for instance, you could use the following logrotate configuration:

/root/backup.tar.gz {
    rotate 5
    dateformat _%Y-%m-%d
    extension .tar.gz

The quick rundown of the options here:

  • rotate 5 -- keep 5 rotations before deleting
  • nocompress -- do not compress the files after rotating
  • dateext -- use the date as the rotation extension instead of incrementing numbers
  • dateformat _%Y-%m-%d -- set the date extension format you want to use
  • extension .tar.gz -- make the .tar.gz come after the rotation extension
  • missingok -- if the file we want to rotate isn't there, don't worry about it and move on (the default is to throw an error)

Hope this helps!

  • 1
    Nice work. I learned a couple of things by reading this. I do want to add though, that some distributions, notably RedHat EL, tend to strip out options that aren't "Enterprise Ready," so YMMV. Nov 1, 2010 at 18:09
  • According to this solution stackoverflow.com/questions/14858752/… logrotate could accomplish the task.
    – Pieter
    Oct 24, 2016 at 10:33

You don't have to use logrotate to do it. Just use a command like this:

ls -1 /root/backup_* | sort -r | tail -n +6 | xargs rm > /dev/null 2>&1

This command will leave the most recent 5 files and remove the remaining (if any). You can use it in a cron job.

  • but this doesn't rotate....
    – simpleuser
    Mar 18, 2017 at 21:03
  • Doesn't answer the question.
    – kubanczyk
    May 22, 2017 at 2:39
  • 5
    The question was to keep five most recent backups an this does exactly that. Even simpler: ls -t1 /root/backup_* | tail -n +6 | xargs rm -f
    – yoyoma2
    Apr 16, 2018 at 15:36
  • This answered my question. Sadly the original question was the closest I could find with Google-fu.
    – Sukima
    Jun 20, 2019 at 17:03

I just had the same situation. Logrotate sounds pretty cool but it didn't work for me at 100% because it mismatch data and file names.

So, to avoid confusion, I decided to incorporate the following line to execute after created my current backup in order to keep the last 5 backups.

My logs are i.e.:

  • tbl-bcx-20180308_010501.tar.bz2
  • tbl-bcx-20180307_010501.tar.bz2
  • tbl-bcx-20180306_010501.tar.bz2

New line into my backup-script (based on a comment above)

  • find /BCX/dumpsql/ -type f | sort -r | tail -n +6 | xargs rm



You can rotate it manually.



_LOG_FILE_BYTES_SIZE_=`wc -c ${_LOG_FILE_} | awk '{print $1}'`


# in MB
if [ ${_LOG_FILE_BYTES_SIZE_} -gt ${_FILE_SIZE_IN_BYTES_} ] ; then
        #cat /dev/null > ${_LOG_FILE_}
         for (( i=${_LOG_ROTATE_MAX_INDEX_}; i>=1; i-- ))
                 if [ $i -eq ${_LOG_ROTATE_MAX_INDEX_} -a -f ${_LOG_FILE_}.$i ] ; then
                        rm -rf ${_LOG_FILE_}.$i

                if [ $i -gt 10 ] ; then
                        rm -rf ${_LOG_FILE_}.10
                elif [ $i -eq 9 -a -f ${_LOG_FILE_}.9 ] ; then
                       mv ${_LOG_FILE_}.9 ${_LOG_FILE_}.10
                elif [ $i -eq 8 -a -f ${_LOG_FILE_}.8 ] ; then
                       mv ${_LOG_FILE_}.8 ${_LOG_FILE_}.9
                elif [ $i -eq 7 -a -f ${_LOG_FILE_}.7 ] ; then
                       mv ${_LOG_FILE_}.7 ${_LOG_FILE_}.8
                elif [ $i -eq 6 -a -f ${_LOG_FILE_}.6 ] ; then
                       mv ${_LOG_FILE_}.6 ${_LOG_FILE_}.7
                elif [ $i -eq 5 -a -f ${_LOG_FILE_}.5 ] ; then
                       mv ${_LOG_FILE_}.5 ${_LOG_FILE_}.6
                elif [ $i -eq 4 -a -f ${_LOG_FILE_}.4  ] ; then
                       mv ${_LOG_FILE_}.4 ${_LOG_FILE_}.5
                elif [ $i -eq 3 -a -f ${_LOG_FILE_}.3  ] ; then
                       mv ${_LOG_FILE_}.3 ${_LOG_FILE_}.4
                elif [ $i -eq 2 -a -f ${_LOG_FILE_}.2  ] ; then
                       mv ${_LOG_FILE_}.2 ${_LOG_FILE_}.3
                elif [ $i -eq 1 -a -f ${_LOG_FILE_}.1 ] ; then
                       mv ${_LOG_FILE_}.1 ${_LOG_FILE_}.2
        mv ${_LOG_FILE_} ${_LOG_FILE_}.1
        touch ${_LOG_FILE_}

echo `date +%Y-%m-%d:%H:%M:%S` : "${_MESSAGE_TO_ECHO_}" >> ${_LOG_FILE_}
#echo "" >> ${_LOG_FILE_}

Now to put data to the log, just do

rotating_logger <file_path> "Welcome world!"
  • 1
    Perhaps you could put a little effort into formatting your post, if you want it to be useful.
    – Tim
    May 22, 2017 at 0:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .