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 02-25-2011, 05:08 AM   #1
mainstream
Member
 
Registered: Oct 2010
Location: localhost
Distribution: Ubuntu / Linux Mint
Posts: 61

Rep: Reputation: 0
Remove a part from a filename


Hello everybody,

I would like to remove a part from wiz_khalifa-black_&_yellow-(82_bpm).mp3
The part to be removed is -(*_bpm)

so that makes wiz_khalifa-black_&_yellow.mp3

Also a problem is that sometimes multiple "(" occur in a filename (wiz_khalifa-black_&_yellow-(remix)-(82_bpm)), so how can i only remove from the last "("

Thanks allot
 
Old 02-25-2011, 05:12 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Are you talking about using just a simple command, or a script? You could manually change it by hand too, but I take it that you posted this in regards as a command. Correct?
 
Old 02-25-2011, 05:26 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Some characters such as ( and ' and whitespace characters don't play well in bash because they either split up a filename or have a special meaning. If you are going to change the names of a bunch of files in a script, it is a good idea to first echo the command, or use `set' to see how the shell evaluates your variables.

Code:
file=wiz_khalifa-black_\&_yellow-\(82_bpm\).mp3 
set mv -v "$file" "${file/-\(*bpm\).mp3/}.mp3"
echo $@
mv -v wiz_khalifa-black_&_yellow-(82_bpm).mp3 wiz_khalifa-black_&_yellow.mp3
Ready to write a three liner which echos the command.
Code:
for file in *-*\(*bpm\).mp3; do
echo mv -v "$file" "${file/-\(*bpm\).mp3/}.mp3"
done
After check the output, you are ready to remove the echo to carry out the command.

Last edited by jschiwal; 02-25-2011 at 05:28 AM.
 
Old 02-25-2011, 05:30 AM   #4
prowla
Member
 
Registered: Feb 2011
Location: UK
Distribution: RHEL 5 & 6, Ubuntu 10
Posts: 93

Rep: Reputation: 3
[root@vmlxysvr base-pkg]# echo 'wiz_khalifa-black_&_yellow-(82_bpm).mp3' | sed 's/-(.*)//'
wiz_khalifa-black_&_yellow.mp3
[root@vmlxysvr base-pkg]#
 
Old 02-25-2011, 05:31 AM   #5
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Quote:
Originally Posted by jschiwal View Post
Some characters such as ( and ' and whitespace characters don't play well in bash because they either split up a filename or have a special meaning. If you are going to change the names of a bunch of files in a script, it is a good idea to first echo the command, or use `set' to see how the shell evaluates your variables.

Code:
file=wiz_khalifa-black_\&_yellow-\(82_bpm\).mp3 
set mv -v "$file" "${file/-\(*bpm\).mp3/}.mp3"
echo $@
mv -v wiz_khalifa-black_&_yellow-(82_bpm).mp3 wiz_khalifa-black_&_yellow.mp3
Ready to write a three liner which echos the command.
Code:
for file in *-*\(*bpm\).mp3; do
echo mv -v "$file" "${file/-\(*bpm\).mp3/}.mp3"
done
After check the output, you are ready to remove the echo to carry out the command.
Wow, you beat me to that one. I was just about finished a little script similar to that to format it....
 
Old 02-25-2011, 05:31 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
To match only the last group if there are multiple parentheses:
Code:
$ filename="wiz_khalifa-black_&_yellow-(remix)-(82_bpm).mp3"
$ echo ${filename/-([0-9]*_bpm)/}
wiz_khalifa-black_&_yellow-(remix).mp3
This matches the numbers preceding the _bpm string so that it can't match the "remix)-" part.
 
Old 03-01-2011, 06:39 AM   #7
mainstream
Member
 
Registered: Oct 2010
Location: localhost
Distribution: Ubuntu / Linux Mint
Posts: 61

Original Poster
Rep: Reputation: 0
Wow thanks guys for all the replies
Making my own script to organize my mp3 collections with > 30.000 mp3's so doing this by hand is no option
 
Old 03-25-2011, 01:10 PM   #8
mainstream
Member
 
Registered: Oct 2010
Location: localhost
Distribution: Ubuntu / Linux Mint
Posts: 61

Original Poster
Rep: Reputation: 0
Quote:
#!/bin/sh
for file in *-*\(*bpm\).mp3; do
echo mv -v "$file" "${file/-\(*bpm\).mp3/}.mp3"
done
How can i integrate this in a script?
I need to escape the special char?

Quote:
~/Music $ ./bpm
./bpm: 4: Bad substitution
 
Old 03-28-2011, 03:03 PM   #9
mainstream
Member
 
Registered: Oct 2010
Location: localhost
Distribution: Ubuntu / Linux Mint
Posts: 61

Original Poster
Rep: Reputation: 0
Hmmz, i got something do to with the shell i'm using.
Without #!/bin/sh it works..?

Last edited by mainstream; 03-28-2011 at 03:09 PM.
 
Old 03-29-2011, 03:01 AM   #10
prowla
Member
 
Registered: Feb 2011
Location: UK
Distribution: RHEL 5 & 6, Ubuntu 10
Posts: 93

Rep: Reputation: 3
The Bourne shell (/bin/sh) is a very simple shell.

Either "#!/bin/ksh" (if you have Korn shell) or "#!/bin/bash" (Linux Bourne-Again shell - haha!) should make it work.

Taking out the "#!/bin/sh" would make it use whatever shell is default for where the script is being run from, which I guess is bash in your command line.
 
Old 03-29-2011, 08:27 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Indeed; the default shell in Linux is usually bash, but you can check using
Code:
echo $SHELL

# or check your entry (last field) in passwd file
grep <yourusername> /etc/passwd
 
  


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
replacing part of filename berrance Linux - Newbie 9 04-25-2010 11:10 AM
Extracting part of a filename wtaicken Programming 7 01-02-2009 09:01 AM
Storing part of a filename as a variable VTGuitarMan Programming 5 03-12-2008 07:47 AM
Getting the first part of a filename in a BASH script trevelluk Programming 3 02-15-2005 02:06 AM
put date as part of filename box_l Programming 3 07-27-2004 07:17 AM


All times are GMT -5. The time now is 11:01 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration