Replace Space With Underscore in Filename

This is merely my personal note. I was about to rename all my files under a directory. I need to replace the space with underscore character ('_'). My first thought was a simple bash script to do that. Apparently, it’s been very long time since my last bash coding session. I spent 15 minutes reading how to read all files and rename them. And I got nothing.

Luckily, I know python. Stupid me. Why didn’t I think it at first time. It was couple minutes of python and all the spaces were replaced by underscores. Thanks to python. All I did were

$ python
Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> files = os.listdir('./')
>>> for f in files:
...     os.rename(f, f.replace(' ', '_'))
... 
>>> 

Or if you want to save in a script, you could you this

#!/usr/bin/python

import os
import sys

files = os.listdir(sys.argv[1])
for f in files:
    os.rename(f, f.replace(' ', '_'))

The script takes the directory path as the argument. You could modify the script to use regex to have a better rename rule :)

About these ads
Tagged , ,

14 thoughts on “Replace Space With Underscore in Filename

  1. little brain says:

    no no no. geek is when you spend hours to find how to do in bash scripting since it’s, by logic, the right thing to do :P

  2. ZaQ says:

    *menunggu edisi november*
    *siul siul*

  3. ZaQ says:

    *menunggu edisi november*
    *siul siul*

  4. just_a_remark says:

    for f in *; do mv “$f” `echo $f | tr ‘ ‘ ‘_’`; done

    (not mine, taken from http://linuxconfig.wordpress.com/2008/01/04/remove-white-space-from-file-name-and-rename-it-with-bash-command/)

    BUT – I like python too much!

  5. just_a_remark says:

    for f in *; do mv “$f” `echo $f | tr ‘ ‘ ‘_’`; done

    (not mine, taken from http://linuxconfig.wordpress.com/2008/01/04/remove-white-space-from-file-name-and-rename-it-with-bash-command/)

    BUT – I like python too much!

  6. marklee says:

    Well it is interesting, certification
    and what can I say to it but it doesn’t agreed me cent by cent.

  7. marklee says:

    Well it is interesting, certification
    and what can I say to it but it doesn’t agreed me cent by cent.

  8. RiCON says:

    In Ubuntu, at least:
    rename ‘y/ /_/’ *

    rename is a perl script using perl regex. Therefore:

    “y/{0}/{1}/”: Search list for all occurrences of {0} and substitute by {1}
    * : Feeds all files in current dir

  9. RiCON says:

    In Ubuntu, at least:
    rename ‘y/ /_/’ *

    rename is a perl script using perl regex. Therefore:

    “y/{0}/{1}/”: Search list for all occurrences of {0} and substitute by {1}
    * : Feeds all files in current dir

  10. sell gold says:

    Do you have an outdoor space that you dream of entertaining in but it is lacking that certain something that makes it a place that you want to be? If so, there are some simple things that you can do to make the space your very own.

  11. Guest says:

    Thanks for the code snippet. I had to alter it, because as is it will rewrite all files in a directory regardless if they have spaces or not. This includes hidden files, which can be dangerous. Here’s my much more verbose version. 

    #!/usr/bin/pythonimport osimport sys files = os.listdir(sys.argv[1])for f in files: if f[0] == ‘.’: print(‘Skipping ‘+f) continue if ‘ ‘ in f: skip = raw_input(‘Replacing ‘+f+’. Hit enter to continue or type s to skip:  ‘) if not skip: print(f+’ -> ‘+f.replace(‘ ‘, ‘_’)) os.rename(f, f.replace(‘ ‘, ‘_’)) else: print(‘Skipping ‘+f)

  12. Guest says:

    Thanks for the code snippet. I had to alter it, because as is it will rewrite all files in a directory regardless if they have spaces or not. This includes hidden files, which can be dangerous. I just filtered for f[0] != ‘.’ and ‘ ‘ in f. 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: