Results 1 to 5 of 5
I often get files with many spaces as part of their names. I would like to automatically replace these spaces with underscores, but otherwise not change the file name. Is ...
- 01-11-2011 #1Just Joined!
- Join Date
- Sep 2010
- Location
- Ohio
- Posts
- 12
Replace Spaces with Underscores in File Names
I often get files with many spaces as part of their names. I would like to automatically replace these spaces with underscores, but otherwise not change the file name. Is there a way to do this task with just the bash shell? Thanks for the help!
- 01-11-2011 #2Just Joined!
- Join Date
- Nov 2006
- Location
- Hyderabad
- Posts
- 85
in vi editor
:%s/ /_/g
- 01-11-2011 #3Just Joined!
- Join Date
- Sep 2010
- Location
- Ohio
- Posts
- 12
So I understand the 's/ /_/g' portion for 's/old/new/g' as replacing all the occurrences of spaces within the text document with underscores. However, I do not know what the % does in this particular example. Also, It seems like this command replaces all occurrences of a space character with an underscore within the document. I do not want to alter the file contents, but merely the name of the file itself.
For example:
This is my file.pdf ------> This_is_my_file.pdf
Thanks again.
- 01-11-2011 #4Just Joined!
- Join Date
- Dec 2010
- Location
- India
- Posts
- 45
this script will convert the name of all the files present in that directory.
for file in *;
do
mv "$file" "${file// /_}"
done
- 01-11-2011 #5Just Joined!
- Join Date
- Sep 2010
- Location
- Ohio
- Posts
- 12
Thank you very much. That worked perfectly!


Reply With Quote