Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7684237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:58:49+00:00 2026-05-31T18:58:49+00:00

I have a directory full of log files that I need to rotate using

  • 0

I have a directory full of log files that I need to rotate using a posix shell script on an embedded system. They are named file, file.0, file.1 and so on. I didn’t see this question already asked and had trouble finding it on google.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:58:50+00:00Added an answer on May 31, 2026 at 6:58 pm

    I ended up solving this myself, and thought I should post the solution here. If anyone has suggestions for improvement, feel free to add them.

    # Usage:  rotate_logs base [max]
    # Example:
    #         rotate_logs messages 10    # Rotate "messages" files keeping only 10 rotated files.
    
    rotate_logs() {
      #set -x
      local base="$1"
      # ensure we have a base and it is not null
      if [ $# -lt 1 -o -z "$1" ]; then
        echo "Usage:  $0 base [max]" >&2
        echo "Example:" >&2
        echo -e "\t$0 messages 10\t# Rotate \"messages\" files keeping only 10 rotated files." >&2
        return 1
      fi
      # see if we were given a max number and make sure it is a number
      if [ $# -ge 2 ]; then
        if ! echo "$2" | egrep -q '^[0-9]+$'; then
          echo "Maximum number, \"$2\", must be a number." >&2
          return 1
        fi
        local max=$2
      fi
      local i
      local n
      # Get a list of all of the already rotated files in reverse
      # base.3 base.2 base.1
      for i in $(ls -1r "$base".*); do
        # Calculate the next number for the current file, i.e. 4 for base.3
        n=$((1 + $(echo $i | sed 's,'"$base"'.,,')))
        # If the new file number is greater than max, delete it.
        if [ $n -gt ${max:-999999} ]; then
          rm -rf "$i"
        else
          # otherwise move the file to the new number
          mv $i "$base".$n
        fi
      done
      # If there is a "base" file with no extension move that now.
      if [ -e "$base" ]; then
        mv "$base" "$base".0
      fi
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a directory full of log files in the form ${name}.log.${year}{month}${day} such that
I have a directory full of files and I need to pull the headers
I have a directory full of images that I would like to resize to
I have a directory full of email files, which look like this: From: Scoop
I have a directory full of html files, each of which has a clinical
I have a function that converts a relative path (e.g. log\crash.txt) to the full
I have some code that will load full file names (ex.F:\logs\1234.log) into a listbox
I have some code that iterates through files in a directory and does useful
I have a directory full of image files. I want to move all the
I have a directory full of XML files. For each of these files I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.