MS Windows mucking up my UNIX files.

MS Windows mucking up my UNIX files.

Post by Jack Shost » Wed, 07 Apr 2004 02:59:09



Scripting gurus,

I have a situation where I have users saving files from MS Windows to
my Solaris machine.  The problem is that when I try to write UNIX
shell scripts to operate on those files that wierd characters such as
blanks get in my way.  So, I wrote a wee shell script to rename blanks
with underscores like this:

#!/bin/csh
# Script:  fixspace
# Author:  Jack Shostak
# Purpose: This script removes all blank spaces from unix filenames.
#          Given a path, this script recursively runs down through the
#          tree renaming filenames with blanks in them to filenames w/
#          "_".  So, a file called f i l e.txt would become
f_i_l_e.txt.
#          This also works on directory names under the root folder.
# To run:  Simply copy the script to a directory and type
#          source fixspace path
#          where path is a valid UNIX path
#
#
# load each directory into 'd'
foreach d (`find $1 -type d -print`)
  # load each file info 'f' - directories included
  foreach f ("`find $d -type d -print`"  "`find $d -type f -print`")
    # if the file contains blank, translate it to underscore via move
    if ("$f" =~ *" "*) then
        mv "$f" `echo "$f" | tr ' ' '_' `
    endif
  end  
end

I have two problems with the above:

1) Microsoft Word files have a * habbit of leaving dollar signs in
work files which get scattered about.  The above script gags on dollar
signs since it sees them as a variable.
2) I just ran across a case where the find statement in the second
foreach clause above returns a "too many words" error.  Perhaps there
are too many files returned for foreach?  I didn't know there was a
limit there.

If anyone can help me resolve either of the above or can point me to a
good script that will clean up MS Windows file naming irregularities I
would be very appreciative.

Thanks!

Jack

 
 
 

MS Windows mucking up my UNIX files.

Post by those who know me have no need of my nam » Wed, 07 Apr 2004 09:17:07


in comp.unix.shell i read:

Quote:>I have a situation where I have users saving files from MS Windows to
>my Solaris machine.  The problem is that when I try to write UNIX
>shell scripts to operate on those files that wierd characters such as
>blanks get in my way.  So, I wrote a wee shell script to rename blanks
>with underscores like this:

write better scripts instead.  mostly that just means using ./ before
relative pathnames and "s around variable expansions and command
substitutions -- i.e., the same things your script probably needs.

Quote:>#!/bin/csh

at the least you should use -f here.  a different language would be even
better.

Quote:>foreach d (`find $1 -type d -print`)

this will suffer if there are spaces in the directory names.

Quote:>    mv "$f" `echo "$f" | tr ' ' '_' `

the following *might* solve the first issue you have [$'s in filenames],
though i cannot remember if quote nesting would be an issue for csh:

        mv "$f" "`echo "$f" | tr ' ' '_' `"

Quote:>2) I just ran across a case where the find statement in the second
>foreach clause above returns a "too many words" error.  Perhaps there
>are too many files returned for foreach?  I didn't know there was a
>limit there.

csh has severe limits on the number of words allowed in various places
(1024 iirc), and it would seem that you've hit it.

--
a signature

 
 
 

MS Windows mucking up my UNIX files.

Post by Michael Tos » Wed, 07 Apr 2004 16:38:54



Quote:> Scripting gurus,

> I have a situation where I have users saving files from MS Windows to
> my Solaris machine.  The problem is that when I try to write UNIX
> shell scripts to operate on those files that wierd characters such as
> blanks get in my way.  So, I wrote a wee shell script to rename blanks
> with underscores like this:

> #!/bin/csh
> # Script:  fixspace
> # Author:  Jack Shostak
> # Purpose: This script removes all blank spaces from unix filenames.
> #          Given a path, this script recursively runs down through the
> #          tree renaming filenames with blanks in them to filenames w/
> #          "_".  So, a file called f i l e.txt would become
> f_i_l_e.txt.
> #          This also works on directory names under the root folder.
> # To run:  Simply copy the script to a directory and type
> #          source fixspace path
> #          where path is a valid UNIX path
> #
> #
> # load each directory into 'd'
> foreach d (`find $1 -type d -print`)
>   # load each file info 'f' - directories included
>   foreach f ("`find $d -type d -print`"  "`find $d -type f -print`")
>     # if the file contains blank, translate it to underscore via move
>     if ("$f" =~ *" "*) then
>    mv "$f" `echo "$f" | tr ' ' '_' `
>     endif
>   end  
> end

> I have two problems with the above:

> 1) Microsoft Word files have a * habbit of leaving dollar signs in
> work files which get scattered about.  The above script gags on dollar
> signs since it sees them as a variable.
> 2) I just ran across a case where the find statement in the second
> foreach clause above returns a "too many words" error.  Perhaps there
> are too many files returned for foreach?  I didn't know there was a
> limit there.

> If anyone can help me resolve either of the above or can point me to a
> good script that will clean up MS Windows file naming irregularities I
> would be very appreciative.

> Thanks!

> Jack

1. quote variables: "$d" rather than $d
2. avoid space-separated lists, e.g. foreach in csh or for in sh
3. avoid csh

Proposal:

#!/bin/sh
set -f # no wildcard expansion
find "$1" -depth -print |
while read f
do
 # space after the last slash ?
 if expr "$f" : '.* [^/]*$' > /dev/null
 then
  dirname=`dirname "$f"`
  basename=`basename "$f"`
  mv "$f" "$dirname"/`echo "$basename" | tr ' ' '_'`
 fi
done

But I would rather fix all other scripts to allow spaces.

--
Michael Tosch
IT Specialist
HP Managed Services Germany
Phone +49 2407 575 313

 
 
 

MS Windows mucking up my UNIX files.

Post by Michael Tos » Wed, 07 Apr 2004 16:50:16




> > Scripting gurus,

> > I have a situation where I have users saving files from MS Windows to
> > my Solaris machine.  The problem is that when I try to write UNIX
> > shell scripts to operate on those files that wierd characters such as
> > blanks get in my way.  So, I wrote a wee shell script to rename blanks
> > with underscores like this:

> > #!/bin/csh
> > # Script:  fixspace
> > # Author:  Jack Shostak
> > # Purpose: This script removes all blank spaces from unix filenames.
> > #          Given a path, this script recursively runs down through the
> > #          tree renaming filenames with blanks in them to filenames w/
> > #          "_".  So, a file called f i l e.txt would become
> > f_i_l_e.txt.
> > #          This also works on directory names under the root folder.
> > # To run:  Simply copy the script to a directory and type
> > #          source fixspace path
> > #          where path is a valid UNIX path
> > #
> > #
> > # load each directory into 'd'
> > foreach d (`find $1 -type d -print`)
> >   # load each file info 'f' - directories included
> >   foreach f ("`find $d -type d -print`"  "`find $d -type f -print`")
> >     # if the file contains blank, translate it to underscore via move
> >     if ("$f" =~ *" "*) then
> >       mv "$f" `echo "$f" | tr ' ' '_' `
> >     endif
> >   end  
> > end

> > I have two problems with the above:

> > 1) Microsoft Word files have a * habbit of leaving dollar signs in
> > work files which get scattered about.  The above script gags on dollar
> > signs since it sees them as a variable.
> > 2) I just ran across a case where the find statement in the second
> > foreach clause above returns a "too many words" error.  Perhaps there
> > are too many files returned for foreach?  I didn't know there was a
> > limit there.

> > If anyone can help me resolve either of the above or can point me to a
> > good script that will clean up MS Windows file naming irregularities I
> > would be very appreciative.

> > Thanks!

> > Jack

> 1. quote variables: "$d" rather than $d
> 2. avoid space-separated lists, e.g. foreach in csh or for in sh
> 3. avoid csh

> Proposal:

> #!/bin/sh
> set -f # no wildcard expansion
> find "$1" -depth -print |
> while read f
> do
>  # space after the last slash ?
>  if expr "$f" : '.* [^/]*$' > /dev/null
>  then
>   dirname=`dirname "$f"`
>   basename=`basename "$f"`
>   mv "$f" "$dirname"/`echo "$basename" | tr ' ' '_'`
>  fi
> done

> But I would rather fix all other scripts to allow spaces.

Even faster:
#!/bin/sh
set -f # no wildcard expansion
find "$1" -depth -name "* *" -print |
while read f
do
  dirname=`dirname "$f"`
  basename=`basename "$f"`
  mv "$f" "$dirname"/`echo "$basename" | tr ' ' '_'`
done

--
Michael Tosch
IT Specialist
HP Managed Services Germany
Phone +49 2407 575 313

 
 
 

MS Windows mucking up my UNIX files.

Post by Bruce Barnet » Thu, 08 Apr 2004 02:42:47



> 1. quote variables: "$d" rather than $d

and use $f:q rather than "$f"

Quote:> 2. avoid space-separated lists, e.g. foreach in csh or for in sh

If you do, use :q as a modifier for variables. However, csh has 7
kinds of variables, and :q doesn't work on all of them the same way.

Quote:> 3. avoid csh

Yup. see
        http://www.grymoire.com/Unix/CshTop10.txt
for my opinion...
--
Sending unsolicited commercial e-mail to this account incurs a fee of
$500 per message, and acknowledges the legality of this contract.
 
 
 

MS Windows mucking up my UNIX files.

Post by rakesh shar » Sun, 11 Apr 2004 14:16:38



> Scripting gurus,

> I have a situation where I have users saving files from MS Windows to
> my Solaris machine.  The problem is that when I try to write UNIX
> shell scripts to operate on those files that wierd characters such as
> blanks get in my way.  So, I wrote a wee shell script to rename blanks
> with underscores like this:

> I have two problems with the above:

> 1) Microsoft Word files have a * habbit of leaving dollar signs in
> work files which get scattered about.  The above script gags on dollar
> signs since it sees them as a variable.
> 2) I just ran across a case where the find statement in the second
> foreach clause above returns a "too many words" error.  Perhaps there
> are too many files returned for foreach?  I didn't know there was a
> limit there.

find "$1" -name '* *' -exec sh -c '
IFS=
newname="`tr [ ] [_] <<[EOF]
$1
[EOF]
`"
mv "$1" "$newname" || echo "Error in renaming \"$1\""
' {} {} \;