Convert Spaces to Underscores?

Convert Spaces to Underscores?

Post by Matt » Tue, 27 Aug 2002 12:15:58



I have a lot of files with spaces in their names.  I'd like to convert
the spaces to underscores.

Specifically, the files are of the form "Something - Something".  I'd
like to change it to "Something_Something".  This would make the files
more accessible in the shell or from the web.

This would require deleting the hypen and one of the spaces, but I'd be
interested in anything that just changes the spaces to underscores.

I don't have much shell experience beyond the basic commands.  I'm
guessing some combination of 'find' and 'mv.'  Or perhaps 'sed.'

Also, I don't have bash.  I'd like something that works with csh or
zsh.

If anyone has any tips, I'd really appreciate them.  Thanks,

 Matt

 
 
 

Convert Spaces to Underscores?

Post by Chris F.A. Johnso » Tue, 27 Aug 2002 14:19:19



> I have a lot of files with spaces in their names.  I'd like to convert
> the spaces to underscores.

> Specifically, the files are of the form "Something - Something".  I'd
> like to change it to "Something_Something".  This would make the files
> more accessible in the shell or from the web.

> This would require deleting the hypen and one of the spaces, but I'd be
> interested in anything that just changes the spaces to underscores.

> I don't have much shell experience beyond the basic commands.  I'm
> guessing some combination of 'find' and 'mv.'  Or perhaps 'sed.'

> Also, I don't have bash.  I'd like something that works with csh or
> zsh.

    I don't recommend writing scripts with csh.

    This question is answered at least weekly; please search the archives:

        http://groups.google.com/groups?group=comp.unix.shell

    Solutions that work with bash or ksh should also work with zsh. If
    you have any problems, post the code here.

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

Convert Spaces to Underscores?

Post by Erik Max Franci » Tue, 27 Aug 2002 14:40:09



> I have a lot of files with spaces in their names.  I'd like to convert
> the spaces to underscores.

> Specifically, the files are of the form "Something - Something".  I'd
> like to change it to "Something_Something".  This would make the files
> more accessible in the shell or from the web.

Hmmmm, I wonder what you would want this for :-).

Quote:> Also, I don't have bash.  I'd like something that works with csh or
> zsh.

With zsh, you can just write:

        for i in *.mp3 ; mv "$i" "$i:s/ - /_/"

If it's a hierarchy, try

        for i in **/*.mp3 ; ...

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ There is nothing so subject to the inconstancy of fortune as war.
\__/ Miguel de Cervantes
    Church / http://www.alcyone.com/pyos/church/
 A lambda calculus explorer in Python.

 
 
 

Convert Spaces to Underscores?

Post by William Par » Sat, 31 Aug 2002 00:12:57



Quote:> I have a lot of files with spaces in their names.  I'd like to convert
> the spaces to underscores.

> Specifically, the files are of the form "Something - Something".  I'd
> like to change it to "Something_Something".  This would make the files
> more accessible in the shell or from the web.

> This would require deleting the hypen and one of the spaces, but I'd be
> interested in anything that just changes the spaces to underscores.

> I don't have much shell experience beyond the basic commands.  I'm
> guessing some combination of 'find' and 'mv.'  Or perhaps 'sed.'

> Also, I don't have bash.  I'd like something that works with csh or
> zsh.

> If anyone has any tips, I'd really appreciate them.  Thanks,

> Matt

Use Bourne shell.  And, the key insight is one of
    mv $i ${i// - /_}
    mv $i `echo $i | sed 's/ - /_/g'`

--

8-CPU Cluster, Hosting, NAS, Linux, LaTeX, python, vim, mutt, tin

 
 
 

Convert Spaces to Underscores?

Post by Chris F.A. Johnso » Sat, 31 Aug 2002 01:38:26




>> I have a lot of files with spaces in their names.  I'd like to convert
>> the spaces to underscores.

>> Specifically, the files are of the form "Something - Something".  I'd
>> like to change it to "Something_Something".  This would make the files
>> more accessible in the shell or from the web.

>> This would require deleting the hypen and one of the spaces, but I'd be
>> interested in anything that just changes the spaces to underscores.

>> I don't have much shell experience beyond the basic commands.  I'm
>> guessing some combination of 'find' and 'mv.'  Or perhaps 'sed.'

>> Also, I don't have bash.  I'd like something that works with csh or
>> zsh.

>> If anyone has any tips, I'd really appreciate them.  Thanks,

> Use Bourne shell.  And, the key insight is one of
>     mv $i ${i// - /_}

   Which doesn't work in the Bourne shell, or bash1, or ksh88, or pdksh;
   it only works in bash2, ksh93 and zsh.

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

1. How to convert spaces with underscore?

Hi,
I have files with spaces like:
dev bin config CleanUpUrls.txt

I would like to convert this to: dev_bin_config_CleanUpUrls.txt

I tried :

echo "dev bin config CleanUpUrls.txt"|sed 's: :_:'
I got : dev_bin config CleanUpUrls.txt

echo "dev bin config CleanUpUrls.txt"|sed 's: *:_:'

But, that doesn't work.

Any help is appreciated.

2. Syjet 1.5G removable drive and Linux

3. Converting spaces to underscores

4. XFree86 and Intel i740 graphics card

5. converting spaces to underscores

6. Recommendation for dual PIII motherboard ?

7. script to convert filename spaces and tabs to underscores

8. Listserver software for Linux?

9. Converting spaces to tabs, only if >= 3 spaces

10. replace spaces with underscores in file names

11. replacing space with underscore

12. Is there a shell script that can change files names with spaces to underscores?

13. replaceing spaces in filenames with underscores