Changing Filenames with Blank spaces.

Changing Filenames with Blank spaces.

Post by oaklande » Thu, 31 May 2001 20:30:04



I want to change many file names with blank spaces and replace
blank spaces with underbar character such examples:
File One   change to   File_One
File Two   change to   File_Two

Here is my script:
for name in `ls *" "*`
do
new=`echo $name | tr " " "_"`
mv $name $new
echo $new
done    

Please advise what is wrong with my script that isnt
working.

--
Posted via CNET Help.com
http://www.help.com/

 
 
 

Changing Filenames with Blank spaces.

Post by eric » Thu, 31 May 2001 21:18:45



> I want to change many file names with blank spaces and replace
> blank spaces with underbar character such examples:
> File One   change to   File_One
> File Two   change to   File_Two

> Here is my script:
> for name in `ls *" "*`
> do
> new=`echo $name | tr " " "_"`
> mv $name $new
> echo $new
> done

> Please advise what is wrong with my script that isnt
> working.

> --
> Posted via CNET Help.com
> http://www.help.com/

The problem is the expansion happening in your 'for in' causes the
filenames to be broken up, just reverse your logic a little.  If you do
the translation right away the for loop happens on the correct new
filenames, then just do an additional tr to get back the old filenames

#!/bin/sh
for name in `ls *" "* | tr " " "_"`
do
old=`echo $name | tr "_" " "`
mv "$old" $name
echo $name
done

Eric Estabrooks
BrainBench MVP for Unix Programming
http://www.brainbench.com

 
 
 

Changing Filenames with Blank spaces.

Post by Jens.Toerr.. » Thu, 31 May 2001 21:26:17



> I want to change many file names with blank spaces and replace
> blank spaces with underbar character such examples:
> File One   change to   File_One
> File Two   change to   File_Two
> Here is my script:
> for name in `ls *" "*`
> do
> new=`echo $name | tr " " "_"`
> mv $name $new
> echo $new
> done    

for file in *\ *;                # while ls also works it's not needed here
do
new=`echo $file | tr ' ' '_'`;
mv "$file" $new;                 # here's the problem
echo $new;
done

If your file is e.g. "a b" mv will see
mv a b a_b
and mis-understand this as "move the files a and b to the directory a_b".
Of course, then it will start to complain about a_b not being a directory
(mv's of multiple files only work when the destination is a directory).
By protecting the file name you want to move by enclosing it in quotes
you remove the ambiguity concerning the file name, i.e. if you mean two
files with names a and b or just one file named "a_b".
                                                          HTH, Jens
--
      _  _____  _____

  _  | |  | |    | |          AG Moebius, Institut fuer Molekuelphysik
 | |_| |  | |    | |          Fachbereich Physik, Freie Universitaet Berlin
  \___/ens|_|homs|_|oerring   Tel: ++49 (0)30 838 - 53394 / FAX: - 56046

 
 
 

Changing Filenames with Blank spaces.

Post by Chris Tophe » Fri, 01 Jun 2001 11:34:42



> I want to change many file names with blank spaces and replace
> blank spaces with underbar character such examples:
> File One   change to   File_One
> File Two   change to   File_Two

> Here is my script:
> for name in `ls *" "*`
> do
> new=`echo $name | tr " " "_"`
> mv $name $new
> echo $new
> done    

> Please advise what is wrong with my script that isnt
> working.

Something like this works in pdksh:

IFS=
for file in *; do
    new=$(echo $file|tr ' ' '_')
    mv "$file" "$new"
    echo "'$new'"
done

You should have first tried something like this to see your problem:

for file in `ls *" "*`; do
    echo "'$file'"
done

 
 
 

1. Change blank space in Filename

I want to change many file names with blank spaces and replace
blank spaces with underbar character.
Example:
File One   change to   File_One
File Two   change to   File_Two

Here is my script:
for name in `ls *" "*`
do
new=`echo $name | tr " " "_"`
mv $name $new
echo $new
done    

Please advise what is wrong with my script that isnt
working.

--
Posted via CNET Help.com
http://www.help.com/

2. FreeBSD demon softtoy

3. Using tar with filenames that have blank spaces

4. yppasswd error!

5. Blank space in Filename

6. Writing FS driver docs?

7. script change filename with space to underscore

8. MandrakePPC color depth?

9. FYI: Change blank to Tab but keep spaces between quotation marks

10. How to change Windows-compliant filenames to Unix filenames

11. Change Upper case filename to lower case filename

12. filenames with blanks, newlines, punctuations, and escape characters.

13. How to create filename with date and not have blanks