[HPADM] SUMMARY: rename files with special characters

From: Muscat, Tyrone J. (MUSCATTJ_at_wattsind.com)
Date: 03/19/04

  • Next message: Long, Larry C: "[HPADM] Summary: Addendum to How do I move an installation of JetAdmin"
    To: "'hpux-admin@dutchworks.nl'" <hpux-admin@dutchworks.nl>
    Date: Fri, 19 Mar 2004 12:46:46 -0500
    
    

    Thank you to Corne Beerse for recommendation of putting the rename in a
    script to avoid too much quoting.

    find . -type f -name '*"*' -exec ./renamescript.sh "'"{}"'" \;

    more renamescript.sh #!/bin/sh newname=`echo "$*" | tr -s ' " ' ' '` echo $* $newname sleep 1 if [[ ! -f $newname ]] then echo mv "$*" "$newname" |sh fi
    Bill Hassell comments on leaving spaces in the names of the files:

    Well, leaving spaces in the name will cause many, many problems. The shell
    treats
    all spaces as separators so EVERY time you refer to a file with spaces, you
    MUST
    enclose the name in double quotes or you'll get nasty results. Consider
    something
    like this:
     
      $ vi file with spaces
        file not found
        with not found
        spaces not found
     
      $ cat another file > finalfile
         another not found
         file not found
     
    Yes, you can work around this but trying to mix completely unrelated
    operating systems
    (Unix, MacOS, Windows, mainframes) will always cause problems without
    careful steps.
    As far as the last script (which changes spaces to underscores), just remove
    the last
    step as in:
     
    for OLDNAME in $(find . -type f -name '*"*' -print)
    do
        NEWNAME=$(echo $(echo "$OLDNAME" | tr -s "/" "_")| tr -s "." "_")
        mv "$OLDNAME" $NEWNAME
    done
     
    The basic technique for translating each special character is to use tr -s
    where the first
    char is the one to substitute and the second is the desired character. You
    can continue
    to stack more $(echo ... | tr -s ...) commands as far as you want, or put
    them each on
    a separate line as in:
     
    for OLDNAME in $(find . -type f -name '*"*' -print)
    do
        
        NEWNAME=$(echo "$OLDNAME" | tr -s "/" "_")
        TEMPNAME=$(echo" $NEWNAME"| tr -s "." "_")
        NEWNAME=$(echo "$TEMPNAME" | tr -s " " "_")
        mv "$OLDNAME" $NEWNAME
    done

    > ----------
    > From: Muscat, Tyrone J.
    > Sent: Monday, March 15, 2004 2:28 PM
    > To: 'hpux-admin@dutchworks.nl'
    > Subject: [HPADM] rename files with special characters
    >
    > I have a large number of files that my macintosh people have created with
    > special characters in the name examples:
    >
    > 1/4"-3/4"288A Flows-1.eps
    > 2/valves
    > 3 fig/sensor installation
    > 3/4,1" 008QT Cutaway
    >
    > I would like to change "/" and "," and " " to "_" and remove the " from
    > the name.
    >
    > Does any one have any scripts that can handle the special characters?
    >
    >
    >
    > Ty Muscat
    > Watts Regulator
    > 815 Chestnut Street
    > North Andover, MA 01845
    > Phone: 978-689-6036
    > Fax: 978-689-6115
    >
    >

    --
                 ---> Please post QUESTIONS and SUMMARIES only!! <---
            To subscribe/unsubscribe to this list, contact majordomo@dutchworks.nl
           Name: hpux-admin@dutchworks.nl     Owner: owner-hpux-admin@dutchworks.nl
     
     Archives:  ftp.dutchworks.nl:/pub/digests/hpux-admin       (FTP, browse only)
                http://www.dutchworks.nl/htbin/hpsysadmin   (Web, browse & search)
    

  • Next message: Long, Larry C: "[HPADM] Summary: Addendum to How do I move an installation of JetAdmin"

    Relevant Pages

    • Re: Form mailto and diacritical marks ?
      ... Some may not have their email software configured to send special characters such as the ones you mentioned. ... You need to 'POST' the form to a scrip on YOUR server so that you can deal with these special characters. ... That is a hidden field included in the form. ... But as you're not using a server side script to process form data this field is ignored. ...
      (alt.html)
    • Re: sed problem: variables countaining special symbols
      ... My script is falling over with a sed problem. ... The above sed command works fine when there are no "special characters" ... as substitute delimiters. ... If not, try a control char, ...
      (comp.unix.shell)
    • Re: Filename containing brackets
      ... I was fiddling around with some files containing special characters within a script. ... How do I tell my script to simply use the variable content as-is and not to interpret it? ... When using single quotes, ...
      (comp.unix.shell)
    • Re: Escape special characters in a string
      ... > I'm hurting my brain trying to find a way to escape special characters ... > The shell now waits since there is no closing quote on the echo. ... I assume your user is calling the script as: ...
      (comp.unix.shell)