Find the answer to your Linux question:
Results 1 to 2 of 2
I am new to scripting. I would like to join the output of daily files to a single file at the end of each month. The daily files are saved ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2004
    Posts
    9

    Getting a filename to used for automation


    I am new to scripting. I would like to join the output of daily files to a single file at the end of each month.

    The daily files are saved in the following format "logddmmyy.log" where dd is day, mm is month & yy is year.

    Any idea how i can do it via scripts?

    thanks

  2. #2
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    I think you should write a cron job that will be run at beginning of each month, and just cat the files from the previuos month together. To do it the script you invoke from cron might look like this:
    Code:
    #! /bin/bash
    
    # XXX: this uses base#value notation because month numbers smaller than 10 will
    # be padded with 0s, that would otherwise be interpreted as being in octal system.
    
    # Get number of the previous month and year.
    mon=$((10#`date +%m`))
    year=`date +%y`
    
    # check if the month was january
    if [[ $mon -eq "0" ]]
    then
        year=$((10#$year-1))
        mon=12
    fi
    
    # Compact files into a single file.
    cat log[0-9][0-9]`printf '%02d%02d' $mon $year`.log > "log`printf '%02d' $mon`.log"
    Try reading an introductory bash info to understand it fully. If you have some questions go on and ask.
    One disclaimer here: I'm not a bash guru and I've treated your question as a kind of challenge. Therefore I can't be sure whether it's the simplest or even correct solution.
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •