script change filename with space to underscore

script change filename with space to underscore

Post by cyrustam8 » Thu, 28 Nov 2002 16:56:48



Hi all,

i wrote a script to change filename with space to underscore

#/bin/sh
for name in `find ./ -name "*"|tr ' ' _ |cut -f 2 -d "/"`
do

        old=`echo $name |tr _ ' '`
        new=$name
        count=`echo "$old" |wc |awk -F" " '{print $2}'`
        if [ $count -gt 1 ]; then
                echo "$old  --> $new"
                mv "$old" "$new"
        else
                echo "no change on file $old"
        fi
done
#####################

Cyrus Tam

 
 
 

script change filename with space to underscore

Post by Stephane Chazela » Thu, 28 Nov 2002 18:31:35




> i wrote a script to change filename with space to underscore


whole thread at http://groups.google.com/groups?th=5713d689d832e949

For a safe way to do this.

--
Stphane

 
 
 

script change filename with space to underscore

Post by bta.. » Thu, 28 Nov 2002 21:16:51



Quote:> Hi all,

> i wrote a script to change filename with space to underscore

> #/bin/sh
> for name in `find ./ -name "*"|tr ' ' _ |cut -f 2 -d "/"`
> do

>         old=`echo $name |tr _ ' '`
>         new=$name
>         count=`echo "$old" |wc |awk -F" " '{print $2}'`
>         if [ $count -gt 1 ]; then
>                 echo "$old  --> $new"
>                 mv "$old" "$new"
>         else
>                 echo "no change on file $old"
>         fi
> done
> #####################

> Cyrus Tam

#! /usr/bin/ksh

rename()
{
   startDir=.


      find $startDir -type $arg -name "* *" -print |
      while read old ; do
         new=$(echo "$old" | tr -s '\011' ' ' | tr -s ' ' '_')
         mv "$old" "$new"
      done
   done

Quote:}

rename d f # Renames your directories with spaces first, then files

-Bill

 
 
 

script change filename with space to underscore

Post by William Par » Fri, 29 Nov 2002 01:46:44



> Hi all,

> i wrote a script to change filename with space to underscore

> #/bin/sh
> for name in `find ./ -name "*"|tr ' ' _ |cut -f 2 -d "/"`
> do

>        old=`echo $name |tr _ ' '`
>        new=$name
>        count=`echo "$old" |wc |awk -F" " '{print $2}'`
>        if [ $count -gt 1 ]; then
>                echo "$old  --> $new"
>                mv "$old" "$new"
>        else
>                echo "no change on file $old"
>        fi
> done
> #####################

> Cyrus Tam

find . -name '* *' | while read i; done
    mv "$i" "${i// /_}"
done

--

Linux solution for data management and processing.

 
 
 

script change filename with space to underscore

Post by Chris Coyl » Fri, 29 Nov 2002 01:51:29


[SNIP]

> > Cyrus Tam

> #! /usr/bin/ksh

> rename()
> {
>    startDir=.


>       find $startDir -type $arg -name "* *" -print |
>       while read old ; do
>          new=$(echo "$old" | tr -s '\011' ' ' | tr -s ' ' '_')
>          mv "$old" "$new"
>       done
>    done
> }

> rename d f # Renames your directories with spaces first, then files

> -Bill

Not bad. Small improvement:


      find $startDir -type $arg -name "* *" -print |
      while read old ; do
         new=$(echo "$old" | tr -s '\011' ' ' | tr -s ' ' '_')
         if [ -d "$new" -o -f "$new" ]
         then
            echo "$new exists already"
         else
            mv "$old" "$new"
         fi
      done
   done

 
 
 

script change filename with space to underscore

Post by bta.. » Fri, 29 Nov 2002 04:42:33




> > Hi all,

> > i wrote a script to change filename with space to underscore

> > #/bin/sh
> > for name in `find ./ -name "*"|tr ' ' _ |cut -f 2 -d "/"`
> > do

> >         old=`echo $name |tr _ ' '`
> >         new=$name
> >         count=`echo "$old" |wc |awk -F" " '{print $2}'`
> >         if [ $count -gt 1 ]; then
> >                 echo "$old  --> $new"
> >                 mv "$old" "$new"
> >         else
> >                 echo "no change on file $old"
> >         fi
> > done
> > #####################

> > Cyrus Tam

Slightly better ...

#! /usr/bin/ksh

rename()
{
    startDir=.


    find $startDir \( -name "*     *" -o -name "* *" \) -print |
       while read old ; do
          new=$(echo "$old" | tr -s '\011' ' ' | tr -s ' ' '_')
          mv "$old" "$new"
       done
    done

Quote:}

rename d f # Renames your directories with spaces first, then files

-Bill