LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Remove a part from a filename (https://www.linuxquestions.org/questions/linux-newbie-8/remove-a-part-from-a-filename-864950/)

mainstream 02-25-2011 05:08 AM

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 :)

corp769 02-25-2011 05:12 AM

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?

jschiwal 02-25-2011 05:26 AM

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.

prowla 02-25-2011 05:30 AM

[root@vmlxysvr base-pkg]# echo 'wiz_khalifa-black_&_yellow-(82_bpm).mp3' | sed 's/-(.*)//'
wiz_khalifa-black_&_yellow.mp3
[root@vmlxysvr base-pkg]#

corp769 02-25-2011 05:31 AM

Quote:

Originally Posted by jschiwal (Post 4270699)
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....

colucix 02-25-2011 05:31 AM

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.

mainstream 03-01-2011 06:39 AM

Wow thanks guys for all the replies :eek:
Making my own script to organize my mp3 collections with > 30.000 mp3's so doing this by hand is no option :p

mainstream 03-25-2011 01:10 PM

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

mainstream 03-28-2011 03:03 PM

Hmmz, i got something do to with the shell i'm using.
Without #!/bin/sh it works..?

prowla 03-29-2011 03:01 AM

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.

chrism01 03-29-2011 08:27 PM

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



All times are GMT -5. The time now is 08:13 AM.