How to replace spaces in filenames with underscores
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to replace spaces in filenames with underscores
On the web, I have seen various cobbled-together ways of removing spaces in filenames and replacing the spaces with the underscore character. Is there an application that accomplishes this task well, or am I stuck with somebody's perl script that I don't rally understand (and that therefore could do damage to my system).
Re: How to replace spaces in filenames with underscores
Quote:
Originally posted by rosslaird or am I stuck with somebody's perl script that I don't rally understand (and that therefore could do damage to my system).
well then, you should learn what the perl script is doing...............look at the syntax and commands used and read up on what they do..............this way your not left in the dark............
Well, yes, I could use this as an opportunity to learn perl, and to figure out all the nitty gritty details of the command structure with which I am not familiar. But frankly, I don't want to do that. I want something that "just works," as we are all so fond of saying. Usually I'm up for studying and research and making mistakes, but not today. Too many other things going on (like having to spend two hours this morning figuring out arcane details in apt.)
But thanks for the suggestion.
#!/bin/sh
for i in *
do
if [ -d "$i" ] # if * is a directory
then
cd "$i" # descend into the directory
for y in *
do
# tempa=$(echo $y | sed 's/ /_/g')
tempa=$(echo $y | sed 's/ /_/g' | sed -e 's/\&//g' | sed 's/__/_/g')
if [ "$y" != "$tempa" ]
then
mv "$y" "$tempa"
fi
if [ -d "$tempa" ] # if this is also a directory, call this program
then
cd "$tempa"
sr; # this is the name of THIS program, must be in your PATH
cd ..
fi
done
cd ..
fi
# tempa=$(echo $i | sed 's/ /_/g')
tempa=$(echo $i | sed 's/ /_/g' | sed -e 's/\&//g' | sed 's/__/_/g')
if [ "$i" != "$tempa" ]
then
mv "$i" "$tempa"
fi
done
keep in mind that this guy wasn't ONLY replacing the spaces with underscores... he was also replacing ampersands...
Thanks. That worked perfectly (once I added "PATH=$PATH:/example/dir" then "export PATH").
Next up: something to remove capitalization in filenames.
Thanks again for the help.
By the way, it looks like krename (for KDE, obviously) will do what I wanted from the comfort of the gui.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.