LinuxQuestions.org
Review your favorite Linux distribution.
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 02-25-2011, 04: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, 04:12 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
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, 04:26 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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 04:28 AM.
 
Old 02-25-2011, 04: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, 04:31 AM   #5
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
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, 04:31 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 05: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, 12: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, 02: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 02:09 PM.
 
Old 03-29-2011, 02: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, 07:27 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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



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

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

All times are GMT -5. The time now is 12:15 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