I have ripped a number of DVDs for a home HTPC (plex server). Some of the file names contain unwanted characters. I want to preserve most of the original filename string but eliminate the undesired text within it. Here are some examples:

  • Existing Filename Apollo 18-1.mp4, remove -1 New filename Apollo 18.mp4 Rule: within a chosen directory find all filenames ending in "-1" and eliminate the character string "-1"
  • Filename ENTOURAGE_DISC1.Title1.avi, remove .Title1 New filename ENTOURAGE_DISC1.avi Rule: within a chosen directory find all filenames ending in ".Title1" and eliminate the character string ".Title1"
  • 1
    This question is not super clear. How do you expect to choose what to remove and where? I assume that mv "Apollo 18-1.mp4" "Apollo 18.mp4" is not what you are after. – Stephen Rauch Mar 27 '17 at 13:57
  • Examples are okay, but you need to give exact rules like, because more than one rules could leed to your examples. For example: "If the filename has a dash and number right before the extension, remove dash and digits." – Philippos Mar 27 '17 at 14:05

With zsh's zmv, extrapolating the requirements from those 2 examples:

autoload zmv # best in ~/.zshrc
zmv '(*)(.Title<->|-<->).(mp4|avi)' '$1.$3'

<-> is any sequence of decimal digits. (one form of <x-y> where neither boundaries are provided). If you only want .Title1 and -1 specifically and any extension, you can always change it to:

zmv '(*)(.Title|-)1(.[^.]##)' '$1$3'

[^.]## matching one or more non-dot characters.

  • I'm fairly computer literate but a novice in Linux. I installed ZSH from the Linux Mint Package manager. It appears to be installed successfully. However, if I enter zsh at a cli prompt my prompt changes to this % and zmv is not recognized. What may I have done wrong? – Guy D'Amico Mar 27 '17 at 15:14
  • @GuyD'Amico, you need the autoload zmv (best in ~/.zshrc) as indicated. You'll find a lot of information online to configure zsh to your taste. – Stéphane Chazelas Mar 27 '17 at 15:41
  • I edited zshrc to include "autoload zmv" under a heading #autoload, then saved the file. Then within the zsh-5.3.1 folder, at the shell prompt, I type ZSH and my prompt changes. Entering zmv returns command not found. I'm obviously making rookie mistakes. – Guy D'Amico Mar 27 '17 at 16:00
  • @Guy, Note that it's ~/.zshrc (note the dot and it has to be in your home directory, not a zshrc file) your zsh customization file. You can also enter that autoload zmv at the prompt of that zsh shell. – Stéphane Chazelas Mar 27 '17 at 16:10
  • I was not aware that the dot preceding zshrc indicated it was a hidden file. So many details for a neophyte. In any event, thanks for your help. I should be good now. – Guy D'Amico Mar 27 '17 at 16:55

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.