1

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: "#-*"

muru
  • 197,895
  • 55
  • 485
  • 740
Nick
  • 197
  • 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

5 Answers5

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
  • 197,895
  • 55
  • 485
  • 740
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.

dstftw
  • 298
  • 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
heemayl
  • 91,753
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

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
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.

Stunts
  • 2,182
  • 1
  • 16
  • 27