1

I've downloaded dozen of files from youtube using youtube-dl. Nice feature, btw. After I searched through them all, I've realized that there are lots of ending with random numbers and letters of different case. Like video_name_number_of_video_-_the_name_of_episode_part_number-12BUInjas32d1.avi.

$ rename -n 's/-.*avi/.avi/i' *.avi – picks up the first - and leaves me with video_name_number_of_video_.avi name.

Is there any options to delete this mix of letters and numbers? I would appreciate any helps. And moreover there are only 13 digits in it.

Added: Someone asked me what exactly do I want. So obviously I want to delete those 13 digits from names of files and live happily the rest of my life. Made the actual question that leads to my aim bold.

user
  • 28,901
  • What is exactly form do you want? – cuonglm May 17 '13 at 06:55
  • @Gnouc, added some guide for you. Thanks! – Alexander May 17 '13 at 07:11
  • @jasonwryan, nope. That doesn't help me a lot. Because as I said those 13 digits are different all the time. They are completely random. I can't specify what to rename in rename command. But I can say between what we should rename. And another "but", rename picks the first - in name, not the second -. – Alexander May 17 '13 at 07:16

3 Answers3

1

I assume those are YouTube video IDs. You can remove them by using a regular expression like:

s/-[^-.]{13}\.avi$/.avi/

Breaking this down into its parts, we have:

  • - - just a hyphen
  • [^-.]{13} - exactly 13 characters which are neither hyphens nor periods
  • \.avi - matches simply ".avi" in the file name
  • $ anchor at the end of the string (file name)
user
  • 28,901
0
rename -n 's/-[^-]*\.avi$/.avi/i' ./*

The idea being to match - followed by a sequence (*) of non-dash characters ([^-]) followed by .avi at the end of the string ($). -.*avi would match from the first - to the last avi.

With zsh:

autoload zmv # in ~/.zshrc as it's a damn useful command

zmv -n '(#i)(*)(-*)(.avi)' '$1$3'

(#i) for case insensitive globbing.

  • Dang it, you beat me to it by about 20 seconds. Mine is a bit more specific, however. – user May 17 '13 at 07:20
  • Works perfect for me. Thanks a lot. Could you give some explanation to this, or some helpful links to understand how this magic happened? – Alexander May 17 '13 at 07:28
0

Here is one way to do it. Put all your file name in a text file, i.e filename.txt:

video_name_number_of_video_-_the_name_of_episode_part_number-12BUInjas32d1.avi video_name_number_of_video_-_the_name_of_episode_part_number-12BUInjas32d2.avi video_name_number_of_video_-_the_name_of_episode_part_number-12BUInjas32d3.avi

Write a script:

#/bin/bash -

while IFS= read -r line 
do
    mv -- "$line" "${line%-*}.avi"
done < filename.txt
cuonglm
  • 153,898