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
g33k!
g33k!
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
*menunggu edisi november*
*siul siul*
*menunggu edisi november*
*siul siul*
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!
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!
Well it is interesting, certification
and what can I say to it but it doesn’t agreed me cent by cent.
Well it is interesting, certification
and what can I say to it but it doesn’t agreed me cent by cent.
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
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
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.
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)
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.