LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 02-21-2005, 05:41 PM   #1
rosslaird
Member
 
Registered: Jul 2003
Location: Vancouver
Posts: 76

Rep: Reputation: 16
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).
 
Old 02-21-2005, 06:31 PM   #2
320mb
Senior Member
 
Registered: Nov 2002
Location: pikes peak
Distribution: Slackware, LFS
Posts: 2,577

Rep: Reputation: 48
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............
 
Old 02-21-2005, 06:36 PM   #3
rosslaird
Member
 
Registered: Jul 2003
Location: Vancouver
Posts: 76

Original Poster
Rep: Reputation: 16
Learning...

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.
 
Old 02-21-2005, 06:59 PM   #4
win32sux
LQ Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
the guy on this thread posted a shell script (at the bottom) which should do what you want:

http://www.linuxquestions.org/questions/history/285966

of course it will require a little tweaking...

Code:
#!/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...


Last edited by win32sux; 02-21-2005 at 07:09 PM.
 
Old 02-22-2005, 01:08 AM   #5
rosslaird
Member
 
Registered: Jul 2003
Location: Vancouver
Posts: 76

Original Poster
Rep: Reputation: 16
worked!

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.

Ross

Last edited by rosslaird; 02-22-2005 at 01:18 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
filenames with spaces antony.booth Programming 5 11-01-2005 04:49 AM
ls and filenames with spaces rose_bud4201 Programming 10 07-01-2005 08:28 AM
Spaces in filenames with BASH edenning Programming 12 01-27-2005 07:10 AM
Filenames with spaces and symbols... Thewyzewun Linux - Newbie 2 03-13-2004 10:59 AM
spaces in filenames ebone Linux - General 2 11-12-2001 11:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration