LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 08-30-2012, 03:14 PM   #1
kokoras63
LQ Newbie
 
Registered: Aug 2012
Posts: 5

Rep: Reputation: Disabled
Smile Deleting the last part of a series of filenames


Hi all.

I would like to ask your assistance on the following.
I have in a directory filenames like:
Pink_Floyd_-_Wish_You_Were_Here_-_Live_(Pulse)-UTeXkHfWYVo.mp3

and I want to rename them like:
Pink_Floyd_-_Wish_You_Were_Here_-_Live_(Pulse).mp3

actually, removing the part of the filename that exists between the last dash before the file extension and the . (period) of it (-UTeXkHfWYVo, dash included)

Could this be achieved with ls and sed commands somehow?
Any assitance will be appreciated.

Thanks a lot
 
Old 08-30-2012, 05:25 PM   #2
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Rep: Reputation: 24
Assuming the filenames contain no spaces, in the directory of interest
Code:
a=($(ls))
b=($(ls|sed 's/-[a-zA-Z]\+\././'))
and a for loop containing
Code:
 mv "${a[$i]}" "${b[$i]}"
 
Old 08-30-2012, 06:43 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
The bulk renaming of files is a question that comes up regularly here. I suggest you do a search of previous threads and read some of the hundreds of discussions that have already taken place.

Code:
a=($(ls))
b=($(ls|sed 's/-[a-zA-Z]\+\././'))
No, this is both wrong and inefficient. To start with, do not use ls to get a list of files. Just use regular shell globbing. And instead of sed, use the built-in parameter substitution or other string manipulation techniques.

This appears to handle the above example, at least:
Code:
for fname in Pink*.mp3 ; do

	mv "$fname" "${fname%-*}.mp3"

done
 
Old 08-30-2012, 07:53 PM   #4
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Rep: Reputation: 24
Quote:
Originally Posted by David the H. View Post
The bulk renaming of files is a question that comes up regularly here. I suggest you do a search of previous threads and read some of the hundreds of discussions that have already taken place.

Code:
a=($(ls))
b=($(ls|sed 's/-[a-zA-Z]\+\././'))
No, this is both wrong and inefficient. To start with, do not use ls to get a list of files. Just use regular shell globbing. And instead of sed, use the built-in parameter substitution or other string manipulation techniques.

This appears to handle the above example, at least:
Code:
for fname in Pink*.mp3 ; do

	mv "$fname" "${fname%-*}.mp3"

done
There are umpteen possible solutions to the original question. I gave an answer using ls and sed because kokoras63 asked for such a solution. Granted it is less efficient than other methods, but it does indeed work with the example filename given, as does your method.

My sed solution depends on the assumption that only letters follow the final hyphen up to the period. If that is not true it would fail. Your solution depends on the assumption that the directory contains only .mp3 files. It fails for any other type of music file, thus
Code:
~/b $ ls
Pink_Floyd_-_Wish_You_Were_Here_-_Live_(Pulse)-UTeXkHfWYVo.flac
Pink_Floyd_-_Wish_You_Were_Here_-_Live_(Pulse)-UTeXkHfWYVo.mp3
~/b $ for fname in Pink*.mp3 ; do
> mv "$fname" "${fname%-*}.mp3"
> done
~/b $ ls
Pink_Floyd_-_Wish_You_Were_Here_-_Live_(Pulse)-UTeXkHfWYVo.flac
Pink_Floyd_-_Wish_You_Were_Here_-_Live_(Pulse).mp3
The information you provide is good, but, as here, it is often delivered in a damned offensive manner. You would do well to take a course in common courtesy.

Last edited by porphyry5; 08-30-2012 at 07:55 PM. Reason: typo
 
Old 08-31-2012, 06:29 AM   #5
kokoras63
LQ Newbie
 
Registered: Aug 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Filename part deleted OK

Thank you all.
The problem was solved with your quick responses and helpful links.

Thanks a lot again for your time.
 
Old 08-31-2012, 04:55 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
No, my solution doesn't depend on there being only .mp3 files, although I only wrote the example code with them in mind, since an example is all I intended it to be.

In any case it can be modified to cover other file types very easily. Just save the extension first and add it back when needed. The only real assumption here is that we want to remove everything from the final hyphen on. The links I provided should have enough info for him to figure out how to further modify it as necessary.

Code:
for fname in Pink* ; do

	ext=${fname##*.}
	mv "$fname" "${fname%-*}.$ext"

done

And sure, there's nothing wrong with using sed, but since there's already a fully capable mechanism built in to the shell, why not use it?

FWIW, I don't usually pay too much heed to the tools the OP's suggest using, as it generally simply indicates that the XyProblem effect is at work. If the OP really does specifically want a sed solution, he should state explicitly that he only wants that, and why.

Edit: BTW, I apologize if my tone sometimes seems discourteous. I do admit that I can sometimes be a bit brusque, particularly when I see common errors being made. I'm a no-nonsense kind of person who likes to get to the point, and I often don't really know how my posts are viewed by others. I mean no offense by it.

Last edited by David the H.; 08-31-2012 at 05:05 PM. Reason: as stated
 
Old 09-01-2012, 04:59 PM   #7
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Rep: Reputation: 24
Quote:
Originally Posted by David the H. View Post
Edit: BTW, I apologize if my tone sometimes seems discourteous. I do admit that I can sometimes be a bit brusque, particularly when I see common errors being made. I'm a no-nonsense kind of person who likes to get to the point, and I often don't really know how my posts are viewed by others. I mean no offense by it.
Thank you, no offense taken.

As to the X-Y problem: granted the questioner usually has another objective in mind behind the question he actually asks, but he needs to solve that main problem himself, no matter how badly he may do it. I'm not far from being a newbie myself, and my over-riding concern then was not the questions I asked, but the question "Can I actually do this? Learn this OS? Devise a solution for my current problem and write a script that will solve it?"

If I took Greg's X-Y conclusion to the ultimate, the only question I should ever ask would be of the form "Would someone please write me a script to do such and such?" From which one learns nothing, might as well have stayed with windows, its all canned solutions there.

All my scripts come back to haunt me, because they are all "badly" written. I'm forever tinkering with them, and one I have rewritten from scratch 3 times. But they are all mine, I defined the problem, devised the solution, and got it going. May have needed help with an occasional detail, but nothing more.

That is why people ask questions that aren't related to their real problem. They want to solve that for themselves, they just want help with the occasional detail. They can always come back later and do it again better, but if they don't get to do it badly the first time they probably never will.
 
Old 09-02-2012, 11:37 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Don't get me wrong. I don't ignore the original request. But I do try to look past it to see what the OP is really interested in. If possible I often try to include a literal solution as well, after explaining why it's not the best choice. If somebody else doesn't beat me to it first, of course.

I try to link to other sources too so that they can follow up on it themselves.

And yes, sometimes people do ask questions in order to advance their knowledge of a specific tool or concept. But in my experience it's generally easy tell these apart from the naive XY types. Usually they will say so directly, either up front or in a quick follow-up.

Quote:
All my scripts come back to haunt me, because they are all "badly" written. I'm forever tinkering with them, and one I have rewritten from scratch 3 times. But they are all mine, I defined the problem, devised the solution, and got it going. May have needed help with an occasional detail, but nothing more.
I'm sure we've all experienced the same. I've been embarrassed to the point of pain looking back at some of my early scripts, and often feel quite a bit of personal satisfaction when I get something working.

But after almost a decade of scripting I finally feel confident enough to be able pass some of my hard-earned knowledge back to others. And if I seem to always be harping on errors and poor code, that's only because I want to help others avoid some of the mistakes I had to learn first-hand. My criticisms are always intended to be in the "constructive" category.
 
1 members found this post helpful.
Old 09-04-2012, 01:01 PM   #9
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Rep: Reputation: 24
Quote:
Originally Posted by David the H. View Post
Don't get me wrong. I don't ignore the original request. But I do try to look past it to see what the OP is really interested in. If possible I often try to include a literal solution as well, after explaining why it's not the best choice.
That is probably the best approach, but as yet not something I will be able to achieve very often. To be honest, I've started answering other people's questions from a quite selfish motive, as exercises to advance my own knowledge. So I'm probably going to get a lot of flak for generating heterodox solutions.
Quote:
But after almost a decade of scripting I finally feel confident enough to be able pass some of my hard-earned knowledge back to others. And if I seem to always be harping on errors and poor code, that's only because I want to help others avoid some of the mistakes I had to learn first-hand. My criticisms are always intended to be in the "constructive" category.
Actually, I value your responses highly, they are of consistently excellent quality, which far outweighs that you were not cut out to be a diplomat. I only reacted adversely on this occasion because you declared my solution to his problem to be wrong, when it did in fact work. If you'd described it as, say, 'non-kosher' I would not have been concerned.
 
Old 09-04-2012, 09:31 PM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I truly do appreciate the feedback. A diplomat I'm certainly not, but I'll try to keep your comments in mind in the future.

I think it was mostly the use of ls that I reacted to as actually wrong, for the reasons given in my link. The use of sed for string manipulation is just inefficient.

As for your posts here, I'd say you're at about the stage I was 6-7 years ago. Some of my early attempts to "help" were as bad or worse. But it did give me the experience I needed. Hopefully it will take you less time to learn all the tricks.
 
1 members found this post helpful.
Old 09-05-2012, 02:53 PM   #11
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Rep: Reputation: 24
Quote:
Originally Posted by David the H. View Post
I think it was mostly the use of ls that I reacted to as actually wrong, for the reasons given in my link. The use of sed for string manipulation is just inefficient.
I read the 'objections to ls' page earlier, but I should have read the globbing page earlier too. I had no idea you could use globs with so many different effects. Many thanks.
 
  


Reply



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
remove part of multiple filenames? steve51184 Linux - Software 9 01-19-2011 02:47 AM
C binary files: Deleting part of the file and resizing xemous Programming 8 03-27-2008 08:49 AM
LXer: LDAP Series Part VI - Directory Service Modeling LXer Syndicated Linux News 0 12-02-2006 05:33 AM
LXer: LDAP Series Part III - The Historical Secrets LXer Syndicated Linux News 0 10-29-2006 08:03 PM
LXer: LDAP Series Part II - Netscape Directory Server LXer Syndicated Linux News 0 10-01-2006 12:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04: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
Open Source Consulting | Domain Registration