Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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?


  2. #2
    Linux User
    Join Date
    Jul 2004
    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:
    #! /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" ]]
    # 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