I have a number of video files using a naming scheme like


which is my preference. Unfortunately I have a lot of files in this format


I need to remove automagically remove everything after the Exx portion.

  • Should we assume the xx stand for something - like pairs of digits? – steeldriver Jan 22 '19 at 18:55
  • Do you have the rename script installed? – Jeff Schaller Jan 22 '19 at 19:09
  • Yes, the xx stand for a pair of digits. – Terry Romphf Jan 22 '19 at 19:26
  • Yes, I do have perl rename installed – Terry Romphf Jan 22 '19 at 19:27
  • Lots of answers, and quickly I might add. I was up all night so I'm going to hit the sack for a bit. I'll be sure to close after I've made my choice. Thanks for all the great input. – Terry Romphf Jan 22 '19 at 20:46

Assuming the x's are placeholders, zmv should be able to do that with zmv -nv '(*S??E??)*.ext' '$1.ext'

% touch foo-S01E02-garbage.ext bar-S07E17-moregarbage.ext
% zmv -nv '(*S??E??)*.ext' '$1.ext'
mv -- bar-S07E17-moregarbage.ext bar-S07E17.ext
mv -- foo-S01E02-garbage.ext foo-S01E02.ext

You could also use '(*S[0-9][0-9]E[0-9][0-9])*.ext' to just match digits in place of x.

Or if you want to use sed:

for f in *.ext; do
    echo mv -n "$f" "$(echo "$f" | sed -e 's/\(S[0-9][0-9]E[0-9][0-9]\).*\.ext/\1.ext/')"

or with the Perl rename command (which is apparently now called file-rename in Debian):

file-rename -n -v 's/(S[0-9]{2}E[0-9]{2}).*\.ext/$1.ext/' *.ext

(Remove the echo from front of mv and the -n flags from zmv and file-rename to actually do the renames. mv -n on the other hand tells it to not overwrite existing files, this works at least in GNU mv.)

| improve this answer | |

How about

$ echo 'some-words-SxxExx-words-I-want-to-remove.extension' | sed 's/\(S..E..\)[^.]*/\1/'

(although I expect a zsh aficionado would be able to do it more simply using shell substitution).

| improve this answer | |
  • Yes, I do prefer zsh, but I'm no expert. I'm clinically depressed and my thought processes are slowed to about the speed of someone with a bad hangover. Not stupid, but I do have trouble retaining stuff. Oh, well - just another challenge. – Terry Romphf Jan 22 '19 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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