[HPADM] SUMMARY: rename files with special characters

From: Muscat, Tyrone J. (MUSCATTJ@wattsind.com)
Date: Fri Mar 19 2004 - 12:46:46 EST


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)


This archive was generated by hypermail 2.1.7 : Sat Apr 12 2008 - 11:02:39 EDT