Results 1 to 13 of 13
  1. #1
    Join Date
    Feb 2004
    Location
    Delray Beach, FL
    Posts
    77

    Very simple shell script needed for automatisation, please help

    Hello!

    I need a script which would rename bunch of files. For example:

    file1
    file2
    ....
    file20

    need to be renamed

    newname1
    newname2
    .....
    newname20

    Part of the filename needs to be changed and keep thee numbers, I know can be done with sed, dont get it.

    Numbers are not always from 1, it can be
    file45
    file46 etc.

    Can somebody help me please?

  2. #2
    Join Date
    Dec 2003
    Posts
    941
    Hi igorkl
    Feel free to contact me at my email or AIM
    Thanks
    Primary email: advanced dot programmer at gmail dot com ..

  3. #3
    Join Date
    Nov 2001
    Location
    Ann Arbor, MI
    Posts
    2,979
    man rename

    If you're using RH linux, this program is in util-linux.
    -Mark Adams
    www.bitserve.com - Secure Michigan web hosting for your business.
    Only host still offering a full money back uptime guarantee and prorated refunds.
    Offering advanced server management and security incident response!

  4. #4
    I was kinda bored tonight, and so I decided to code you up an all-bash way to do this.. no external libraries / apps needed.

    HOWEVER.. its very inefficient and slow. .but it works!!!! It uses arrays to construct the filenames, and arrays are kind of slow in bash. This script takes almost 5 seconds to execute. Keep in mind it doesnt check for filenames like file.1, just file1 as you have described. Feel free to hack it up

    Code:
    #!/bin/bash
    OLDPREFIX='file'
    NEWPREFIX='newname'
    
    #construct prefix array
    cp=0
    for i in `ls $OLDPREFIX*`; do
    prefix[$cp]=$(echo $i | tr -d [:digit:])
    cp=$(( $cp + 1 ))
    done
    
    #construct suffix array
    cs=0
    for i in `ls $OLDPREFIX*`; do
    suffix[$cs]=$(echo $i | tr -d [:alpha:])
    cs=$(( $cs + 1 ))
    done
    
    #rewrite prefix array with new name
    cn=0
    for i in `echo ${prefix[@]}`; do
    newname[$cn]=$( echo $i | sed "s/$OLDPREFIX/$NEWPREFIX/g" )
    cn=$(( $cn + 1 ))
    done
    
    #finally, rename the files
    cr=0
    for i in `seq $(echo ${#newname[@]})`; do
    mv "${prefix[$cr]}${suffix[$cr]}" "${newname[$cr]}${suffix[$cr]}"
    cr=$(( $cr + 1 ))
    done
    "The only difference between a poor person and a rich person is what they do in their spare time."
    "If youth is wasted on the young, then retirement is wasted on the old"

  5. #5
    Join Date
    Feb 2004
    Location
    Delray Beach, FL
    Posts
    77
    [b]innova[b]

    Thank you for the script, I use the elements from it.

    now I can read the filenames,

    #!/bin/bash
    echo "server-two56.rmvb" | tr -d [:alpha:]

    #/tmp>bash aa
    -56.

    This cuts everything but "-" (dash) and "." . How do I cut it too?

  6. #6
    Hi,

    My example was not very complete for handling all types of filenames.. I assumed at the time that your names were simply things like:

    file1
    file2

    Now, I am going to assume once more that your file extension is not going to change. Using your example:

    server-two56.rmvb

    I dont think 'tr' will work so well for this. Reason being, instead of an example like 'file1' where we dont have a delimiter, in this case we do: the period before the extension.

    Here are some ideas to get you started:

    filename=$(echo 'server-two56.rmvb' | cut -d '.' -f1)
    echo $filename # server-two56

    fileext=$(echo 'server-two56.rmvb' | cut -d '.' -f2)
    echo $fileext # rmvb

    This example is actually a lot easier to deal with than the first, because having a file extension makes it easy to get the prefix separated.

    Now, if you want to do further processing on the 'server-two56' portion, you can use 'tr' as in the original or 'cut' as in my last example. Possibilities include:

    field1 #server
    field2 #two56

    OR

    field1 #server-two
    field2 #56

    I am not sure which part you want to change. .if you want to provide more info I would be happy to help.

    A little disclaimer: As you can tell, this isnt all that easy to do in pure bash-shell. I am thinking though that there may be an easier way to do it without using arrays. I am also sure that a perl god could probably code a one-liner to do all the work that this script does

    There are plenty of further optimizations I think that could be utilized as well. If you are interested, check out bash's string functions, which can replace external calls to sed, possibly cut/tr as well. I dont know them as well offhand, and I didnt have my reference available at the time of the post.

    Nevertheless, its an interesting exercise. I learned most of what I know from the examples in the 'Advanced bash-scripting guide'.
    "The only difference between a poor person and a rich person is what they do in their spare time."
    "If youth is wasted on the young, then retirement is wasted on the old"

  7. #7
    Join Date
    Mar 2001
    Location
    California
    Posts
    332
    This should be simple enough..

    Code:
    #!/bin/sh
    for X in `ls file[1-9]*`
    do
      Y=`echo $X | sed s/file/newname/`
      mv $X $Y
    done
    Last edited by RutRow; 01-14-2005 at 08:27 PM.

  8. #8
    Join Date
    Feb 2004
    Location
    Delray Beach, FL
    Posts
    77
    Originally posted by RutRow
    This should be simple enough..

    Code:
    #!/bin/sh
    for X in `ls file[1-9]*`
    do
      Y=`echo $X | sed s/file/newname/`
      mv $X $Y
    done
    Yep, that's it , exactly what I was trying to do, couldn't figure out with [1-9], that's why was working with combination with cut and tr.


    Thank you

  9. #9
    Join Date
    Nov 2001
    Location
    Ann Arbor, MI
    Posts
    2,979
    Not sure why you didn't try rename.

    rename file newname file*
    -Mark Adams
    www.bitserve.com - Secure Michigan web hosting for your business.
    Only host still offering a full money back uptime guarantee and prorated refunds.
    Offering advanced server management and security incident response!

  10. #10
    Rename is not a standard utility.
    "The only difference between a poor person and a rich person is what they do in their spare time."
    "If youth is wasted on the young, then retirement is wasted on the old"

  11. #11
    Join Date
    Feb 2004
    Location
    Delray Beach, FL
    Posts
    77
    Okay, now I am stucked in phase 2 in this script.

    How do I do double condition?

    Now I have:

    if [ $b -le $a -a $a -le $c ] ; then
    do something ;
    else
    or do something different ;
    fi

    How do I insert checking one or more conditions?
    need to check

    if [ $b -le $a -a $a -le $c ] [$f -le $a -a a -le $f]
    and do something if all of this is true.

    Is this only way to do this:

    if []
    if[]
    if []

    ?

  12. #12
    Join Date
    Mar 2001
    Location
    California
    Posts
    332
    You can use && between the sets of brackets.... if I am reading your question correctly.

    Code:
     if [ $b -le $a -a $a -le $c ] && [$f -le $a -a a -le $f]

  13. #13
    To handle multiple conditions, either use the 'elif' statement, or perhaps a 'case' statement would work also.

    Example elifs:

    if [ some test ] && [ other test ]
    then
    do something
    elif [ some other test ] && [ other test ]
    then
    do something else
    elif [ some other test ] && [ whatever ]
    then
    do something else
    else #none of the conditions apply
    do something #when none of the prior conditions work
    fi
    "The only difference between a poor person and a rich person is what they do in their spare time."
    "If youth is wasted on the young, then retirement is wasted on the old"

Posting Permissions

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