1

This question already has an answer here:

I downloaded some files with youtube-dl. The name is always like this

foo - baar -PSUHDUWHDAIHDWU.ogg/mp3/...

How can I remove this random letters (cut the last '-*') at the end from all files of a folder so that the name is:

foo - baar.mp3/ogg/...

I tried something with "rename" but I have no idead, what pattern to use. I think it should be something like this: "#-*"

marked as duplicate by karel, Eric Carvalho, Pabi, waltinator, qbi May 23 '15 at 12:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    What are the full names of the files ? GIve some examples.. – heemayl May 20 '15 at 21:05
  • 1
    Are they always in the same format ? foo space - space baar ? – Sergiy Kolodyazhnyy May 20 '15 at 21:08
  • Ton Steine Scherben - Alles verändert sich-3CUz4M3M1r8.ogg Ton Steine Scherben - Der Traum ist aus-WYZCovq71XE.ogg Ton Steine Scherben - Feierabend-BopYtPtjlkI.ogg Ton Steine Scherben - Ich will nicht werden, was mein Alter ist-WpLfJZvnWSw.ogg Ton Steine Scherben - Keine Macht für Niemand-XtMPGhXnzWE.ogg Ton Steine Scherben - Komm schlaf bei mir-Nr9V_UH04eA.ogg – Nick May 20 '15 at 21:35
  • posted an answer, please review – Sergiy Kolodyazhnyy May 20 '15 at 21:42
  • So, you want to change '-[A-Z]+.ogg$' to '.ogg'? Try something like `find . -type f -name '*.ogg' -print0 | xargs -0 rename -v -n 's/-[A-Z]+.ogg$/.ogg/' – waltinator May 22 '15 at 18:04
2

With rename, assuming that a . only appears at the end of the filename for the extension, you can use the following expression:

s/-[^-]*\././

This replaces a hyphen followed by characters other than a hyphen until a ., with ., effectively deleting the last hyphen and the characters following it. Use it thus:

rename -n 's/-[^-]*\././' *

If the changed names look fine, then run without -n.

  • muru, i think OP has asked to append a .mp3 too.. – heemayl May 20 '15 at 22:01
  • 1
    @heemayl I'm pretty sure ogg/mp3/... indicates that extensions may vary. – muru May 20 '15 at 22:02
  • ummm...ok ....then i need to modify my one :) – heemayl May 20 '15 at 22:03
2

Instead of hacking with replacing pattern you can just tell youtube-dl not to include these random letters (that's a video id actually) to the final filename. This can be done with output template, e.g. youtube-dl -o "%(title)s.%(ext)s" 3CUz4M3M1r8 will result in Ton Steine Scherben - Alles verndert sich.mp4.

  • thanks a lot! :) this is the easiest way is this also usable for playlists? – Nick May 21 '15 at 18:40
  • Absolutely. With output template it's also possible to specify arbitrary hierachies, e.g. to put all playlist videos in separated directory inside your home directory -o "~/%(playlist)s/%(title)s.%(ext)s". – dstftw May 21 '15 at 18:50
1

Using bash parameter expansion:

#!/bin/bash
shopt -s extglob
for file in *(*mp3|*ogg); do
    ini="${file%-*}"
    ext="${file##*.}"
    mv "$file" "${ini}.${ext}"
done
  • Here we have used the extglob and parameter expansion feature of bash.

  • ini will contain the initial portion discarding the portion from last - onwards

  • ext will contain the extension e.g. ogg, mp3

Test :

$ ls
scr.sh                                                      Ton Steine Scherben - Ich will nicht werden, was mein Alter ist-WpLfJZvnWSw.ogg
Ton Steine Scherben - Alles verändert sich-3CUz4M3M1r8.ogg  Ton Steine Scherben - Keine Macht für Niemand-XtMPGhXnzWE.mp3
Ton Steine Scherben - Der Traum ist aus-WYZCovq71XE.ogg     Ton Steine Scherben - Komm schlaf bei mir-Nr9V_UH04eA.mp3
Ton Steine Scherben - Feierabend-BopYtPtjlkI.ogg

$ bash scr.sh 

$ ls
scr.sh                                          Ton Steine Scherben - Ich will nicht werden, was mein Alter ist.ogg
Ton Steine Scherben - Alles verändert sich.ogg  Ton Steine Scherben - Keine Macht für Niemand.mp3
Ton Steine Scherben - Der Traum ist aus.ogg     Ton Steine Scherben - Komm schlaf bei mir.mp3
Ton Steine Scherben - Feierabend.ogg
  • Isn't this renaming .ogg files to .mp3? That might not be too good... – Stunts May 20 '15 at 22:02
  • 1
    @Stunts Yeah..i got it wrong..i will edit – heemayl May 20 '15 at 22:03
0

Based on the pattern you provide, you can try this:

perl-rename "s/\-[a-zA-Z0-9]*\././g" *

It uses a perl regexp to replace any uppercase letters after a "-" and before a "." with just a "." in any file. the final "*" tells the command to include all the files in the current dir.

It has to be run inside the dir you have your files.

0

Script

#!/bin/bash
# Author : Serg Kolo
# Description: script for renaming files 
# for http://askubuntu.com/q/626258/295286

for file in *; do

        FILENAME=$( awk -F '-' '{gsub(" ","");print $1"-"$2}' <<< "$file")
        EXTENSION=$( awk -F '.' '{print $2}' <<< "$file")
        mv "$file" ./"$FILENAME.$EXTENSION"

done

Results

enter image description here

  • Just curious: why this roundabout way of printing the filename ... from the filename? – muru May 20 '15 at 21:54
  • @muru I'll explain later in the chat but I guess it could be edited to echo "$ file" – Sergiy Kolodyazhnyy May 20 '15 at 23:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.