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

  • Home
  • SEARCH
  • 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 7654361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:14:13+00:00 2026-05-31T12:14:13+00:00

I am working on a UNIX task where i want check if a particular

  • 0

I am working on a UNIX task where i want check if a particular log file is present in the directory or not. If it is present, i would like to rename it by appending a timestamp at the end. The format of the file name is as such: ServiceFileName_0.log

This is what i have so far but it wouldn’t rename when i run the script, even though there is a file with the name ServiceFileName_0.log present.

renameLogs()
{
   #If a ServiceFileName log exists, rename it

   if [ -f $MY_DIR/logs/ServiceFileName_0.log ]; 
   then
    mv ServiceFileName_0.log ServiceFileName_0.log.%M%H%S
   fi
}

Pls Help!

Thanks

  • 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-31T12:14:14+00:00Added an answer on May 31, 2026 at 12:14 pm
    renameLogs()
    {
        if [ -f $MY_DIR/logs/ServiceFileName_0.log ] 
        then mv $MY_DIR/ServiceFileName_0.log $MY_DIR/ServiceFileName_0.log.$(date +%M%H%S)
        fi
    }
    

    Use the directory prefix consistently. Also you need to specify the time properly, as shown.

    Better, though (less repetition):

    renameLogs()
    {
        logfile="$MY_DIR/logs/ServiceFileName_0.log"
        if [ -f "$logfile" ] 
        then mv "$logfile" "$logfile.$(date +%H%M%S)"
        fi
    }
    

    NB: I’ve reordered the format from MMHHSS to the more conventional HHMMSS order. If you work with date components too, you should seriously consider using the ordering recommended by ISO 8601, which is [YYYY]mmdd. It groups all the log files for a month together in an ls listing, which is usually helpful. Using ddmm order means that the files for the first of each month are grouped together, then the files for the second of each month, etc. This is usually less desirable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have you seen library for flexible working with terminal(Unix like)? I want to implement
I'm working in a UNIX environment where /bin has been set up like this:
is the following command working under Unix&Linux? ProcessBuilder prcbdoc = new ProcessBuilder(cmd,/C,start, Documentation.doc); prcbdoc.directory(new
I am working on the apache tomcat server in unix enviroment. I want to
i am working on unix. i want to write a shell script which will
I'm starting to learn Assembler and I'm working in Unix. I want to open
I'm working on a proprietary unix-like os (I don't know if that's relevant though)
Can anybody tell me how to activate RTTI in c++ when working on unix.
If I am working on a Unix machine, how could I know the size
This unix command I haven't got quite working on Mac yet - any ideas

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.