Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2008
    Location
    Toronto, Canada
    Posts
    2,369

    Unanswered: Renaming files in dir

    I need to rename a file every month (up to .12 version). Here is what we currently have:

    cp $HOME/sqllib/db2dump/db2diag.log.11 $HOME/sqllib/db2dump/db2diag.log.12
    cp $HOME/sqllib/db2dump/db2diag.log.10 $HOME/sqllib/db2dump/db2diag.log.11
    cp $HOME/sqllib/db2dump/db2diag.log.9 $HOME/sqllib/db2dump/db2diag.log.10
    cp $HOME/sqllib/db2dump/db2diag.log.8 $HOME/sqllib/db2dump/db2diag.log.9
    cp $HOME/sqllib/db2dump/db2diag.log.7 $HOME/sqllib/db2dump/db2diag.log.8
    cp $HOME/sqllib/db2dump/db2diag.log.6 $HOME/sqllib/db2dump/db2diag.log.7
    cp $HOME/sqllib/db2dump/db2diag.log.5 $HOME/sqllib/db2dump/db2diag.log.6
    cp $HOME/sqllib/db2dump/db2diag.log.4 $HOME/sqllib/db2dump/db2diag.log.5
    cp $HOME/sqllib/db2dump/db2diag.log.3 $HOME/sqllib/db2dump/db2diag.log.4
    cp $HOME/sqllib/db2dump/db2diag.log.2 $HOME/sqllib/db2dump/db2diag.log.3
    cp $HOME/sqllib/db2dump/db2diag.log.1 $HOME/sqllib/db2dump/db2diag.log.2
    cp $HOME/sqllib/db2dump/db2diag.log $HOME/sqllib/db2dump/db2diag.log.1


    Can you please suggest how this can be simplified using loop?

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I don't have access to a machine with ksh to steal or rewrite a script that I frequently use that I call "version". It takes two arguments: a required pathname and an optional version count which defaults to 99. The steps are:
    1. Delete $pathname.$limit
    2. set $this = $limit
    3. set $next = $this - 1
    4. move $pathname.$next to $pathname.$this
    5. decrement this
    6. if 0 -lt $this goto 3
    7. move $pathname to $pathname.1
    Implement this using the shell or language of your choice.

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  3. #3
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Code:
    echo "Enter max number revisions \c"
    read MAX
    BASE="$HOME/sqllib/db2dump/db2diag.log"
    CURR=`expr $MAX - 1`
    while [ $CURR -gt 0 ]
    do
       if [ -r $BASE.$CURR ]
       then
          mv $BASE.$CURR $BASE.$MAX
       fi
       MAX=`expr $MAX - 1`
       CURR=`expr $CURR - 1`
    done
    mv $BASE $BASE.1
    You should use mv instead of cp. There is no sense in reading the files when all that is needed is to rename them.

  4. #4
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Thanks! That looks like you "stole" my script!

    The only difference was that mine used an optional argument for $MAX too.

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  5. #5
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    i wrote it to your specs.

  6. #6
    Join Date
    Aug 2008
    Location
    Toronto, Canada
    Posts
    2,369
    Thank you.

Posting Permissions

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