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
  • 207,228
Nick
  • 197

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
  • 207,228
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
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
  • 93,925
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

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,262