LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-24-2011, 07:37 PM   #1
kulkoustubh
LQ Newbie
 
Registered: Sep 2011
Posts: 2

Rep: Reputation: Disabled
Unhappy Renaming files with spaces


Hello,
I am new to shell scripting. I need to rename some files. Following is the format of file name I want to rename.

[ABC.DE] Album - 02 - This is the name.mp3

I want to rename this to 'This is the name.mp3'. I have lots of files of the same name format and that's why I was trying to write the script for the same. I tried using cut and sed command but I am not sure how to handle the whitespaces while using these commands.

Please reply as soon as possible.

Thanks,
Kousutbh.
 
Old 09-24-2011, 07:44 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,933

Rep: Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692Reputation: 3692
Quote:
Originally Posted by kulkoustubh View Post
Hello,
I am new to shell scripting. I need to rename some files. Following is the format of file name I want to rename.

[ABC.DE] Album - 02 - This is the name.mp3

I want to rename this to 'This is the name.mp3'. I have lots of files of the same name format and that's why I was trying to write the script for the same. I tried using cut and sed command but I am not sure how to handle the whitespaces while using these commands.
Shouldn't be a problem...just put a backslash "\" in front of where the whitespace(s) are in the sed command. A backslash makes the system treat the next character as JUST a character, so things like spaces, and punctuation (!#@*, etc.), will be treated as such, and not special characters.
 
1 members found this post helpful.
Old 09-25-2011, 02:16 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
The other option would be to read it into a loop and then quote the variables so white space is preserved.
 
Old 09-25-2011, 05:52 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You know, questions like this about renaming files are incredibly common, and come up with amazing regularly here and elsewhere on the web. In fact, just a quick glance at the "related threads" box at the bottom of the page will give you a few examples.

You did try doing a search or two before you submitted your post, right?

In the meantime, read through these three links so you can fully understand how the shell handles arguments and whitespace:
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
 
1 members found this post helpful.
Old 09-25-2011, 09:38 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
cut and sed handle whitespace normally. The shell doesn't though. Just escape it if it's not in quotes.
 
Old 09-25-2011, 01:41 PM   #6
gary185
Member
 
Registered: Jul 2011
Posts: 113

Rep: Reputation: Disabled
Code:
#!/bin/bash

for file in *
do
mv "$file" $(echo "$file" | sed 's/[^A-Za-z0-9_.]/_/g;s/__*/_/g;s/^_//;s/_$//')
done
that should do it
 
Old 09-25-2011, 03:17 PM   #7
kulkoustubh
LQ Newbie
 
Registered: Sep 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks

Thanks all for you help! It is working now
 
Old 09-25-2011, 08:27 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
@gary185 - How does your solution solve for the OP's original question?

He asked how to move a file called :- [ABC.DE] Album - 02 - This is the name.mp3
To a file called :- This is the name.mp3

Admittedly I have not run your code but I doubt it will give this outcome?
 
Old 09-25-2011, 08:44 PM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,839

Rep: Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822
David the H. (who of course has whitespace in his name .... ) provided good link(s).
This sort of thing is just a can of worms thanks to the shell. Way too easy to mess it up.
 
Old 09-25-2011, 11:55 PM   #10
gary185
Member
 
Registered: Jul 2011
Posts: 113

Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
@gary185 - How does your solution solve for the OP's original question?

He asked how to move a file called :- [ABC.DE] Album - 02 - This is the name.mp3
To a file called :- This is the name.mp3
the real question is why you are now asking on behalf of the OP about what i wrote.
He can of course run it and see what it does and decide for himself if it was usefull.
isn't life fascinating that way ?
if you asked really for an answer there was this seemingly little understood obscure line in the OP's question.
Quote:
I tried using cut and sed command but I am not sure how to handle the whitespaces while using these commands.
actually i thought it WAS the actual question! somehow i doubt he needed code to actually name things "this is the name".
so i thought he needed to handle whitespace.
again
Quote:
not sure how to handle the whitespaces
anyway what i wrote handles whitespace which can be quite proplematic in shell scripting in my opinion.
OK sure it might not do 100% what he wants. I don't even know what he wants or needs accept to figure out how to use sed to handle whitespace. I actually don't even care but that's another issue.
I gave him sed to handle whitespace.
It might be obvious to you guys but i find sed kind of confusing and figured the guy could use some help.
see he could do this tricky thing where he ran what i wrote on a dummy filename and then adjusted it.
outcome of my code
Code:
Album - 02 - This is the name.mp3
converted to
Album_02_This_is_the_name.mp3
OK so i'm an idiot and i thought it was usefull but it's not. i will make sure i don't help you with any of my useless crap in the future.
LOL my file DID actually make a file named "this is the name" WOOT WOOT !!
sort of
so it doesn't do exactly what he wants
Oh no wait a minute perhaps that's up to HIM !!
 
0 members found this post helpful.
Old 09-26-2011, 02:16 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Quote:
the real question is why you are now asking on behalf of the OP about what i wrote.
Well the answer to this is easy, your reply was at about 3am and his a bit after 4:30am ... so surprisingly I was asleep.

As to the rest of your diatribe, I will in future not ask any questions when you present an answer.
Sorry for the inconvenience.
 
Old 09-26-2011, 10:23 PM   #12
gary185
Member
 
Registered: Jul 2011
Posts: 113

Rep: Reputation: Disabled
Ok-whatever -- i get it.
havn't used this forum in quite a while but i see it hasn't changed.

the real answer to this question is that i answered because i have been using Linux on Pc's since about 1983.
my Unix experience dates back to programming on a main frame the size of a house back in the late 70's.
I also have written some programs that at times have been widely used on linux desktops.
I've built Linux from scratch like 25 times over the years.....
and i felt like helping.

But since the people on this forum still "police" the posts..
I don't know -- perhaps i'll answer a few questions again in 2030.

Linux used to be about free speech by the way -- but i see now it's just "group think"
sad.. but not worth wasting my time.
Or appearently i was wasting other peoples time LOL.
 
Old 09-27-2011, 01:30 AM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Running Linux since 1983? That's a pretty good trick!

I don't want to get too involved in debate here, but I will say that if a poster asks about one thing, then you should start by directly addressing that request, without any superfluous additions that could cause confusion. Once that's taken care of you can go on to offer additional advice or suggestions as you see fit.

Also, if you provide some code to an inexperienced user, it's just common courtesy to also provide an explanation of how it works and what it's doing. This is especially true if the code does something that deviates from the original topic in some way. "Just try it and see" is not a safe option when you're dealing with things like renaming files, and doesn't really teach the user much either.


With that said, and with the original request answered, I'm now going to add some additional advice of my own.

External tools like sed/cut/whatever are usually unnecessary here, since parameter expansion can do the same thing. Assuming that the string you want always follows the final "-" and a space, then it can be as simple as:
Code:
for file in *.mp3 ; do
	mv "$file" "${file##*- }"
done
I'd also suggest looking around for one of the bulk renaming programs that are available for doing this kind of thing.
 
  


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
renaming files with spaces and special characters. bowens44 Linux - Newbie 8 06-29-2009 07:52 PM
Renaming files Viablade Linux - Newbie 11 10-10-2008 02:54 PM
help renaming files balistic Linux - Newbie 4 07-29-2007 09:04 AM
renaming files TomalakBORG Linux - Newbie 4 12-24-2005 11:14 AM
Solution: renaming multiple files with spaces HawkeyeCoug Linux - Newbie 0 03-26-2004 11:57 AM


All times are GMT -5. The time now is 09:12 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration