I have a bunch of video-files in one folder , that has a double suffix avi.avi.

find /home/alex/Filme/  -type f -name '*.avi.avi' 
/home/alex/Filme/Super 8.avi.avi
/home/alex/Filme/Exit - A Night From Hell.avi.avi
/home/alex/Filme/Der Plan.avi.avi

I try to remove the double string with following "Script"

    for i in `find $HOME/Filme -type f -name '*avi.avi' -print0` 

        sed -e 's/'*.avi.avi'/'*.avi'/g'

Or is a better way to achive this with rename, like this?

find $HOME/Filme -type f -name '*avi.avi' -print0 -exec  sh -c rename -v 's/*.avi.avi/*.avi/g' {} \;

I'am not sure if the foldername will be preserved. Yes I found several questions and answers for this, but I have problems to adapt it to my case.

example for Question

Does one of my approach work?


You were almost there.

find $HOME/Filme -type f -name '*avi.avi' -exec \
  sh -c "rename -v 's/\.avi\.avi$/.avi/'" {} \;

(The argument to rename is a regular expression substitution; and the /g option would only be useful if you wanted to substitute every occurrence in a matching file name; but by definition, there can only be one match at the very end.)

Perhaps more usefully you can batch this, and maybe just use mv for improved portability.

find $HOME/Filme -type f -name '*avi.avi' -exec \
  sh -c 'for f; do mv -v "$f" "${f%.avi}"; done' _ {} +

If there is no need to traverse subdirectories,

for f in $HOME/Filme/*.avi.avi; do
    mv -v "$f" "${f%.avi}"
  • 1
    find "$HOME/Filme" -type f -name \*.avi.avi -exec rename -v 's/.avi$//' {} + as rename can take multiple arguments. – user218374 Jul 14 '17 at 10:26
  • Perl rename isn't available on all systems so the sh is better suited. + there is GNUism but can easily be replaced by \; for compatibility reasons – Valentin Bajrami Jul 14 '17 at 10:53

With simple rename (Perl implementation) command:

rename 's/(.*\.avi)\.avi$/$1/' /home/alex/Filme/*.avi.avi

Or with just removing the last .avi section:

rename 's/\.avi$//' /home/alex/Filme/*.avi.avi

I use this code to rename such files

rename -v "s/\.avi$//g" *
  • 1
    ... you shouldn't, since it will remove all .avi suffixes, as well as non-suffix matches such as ravishankar – steeldriver Jul 14 '17 at 12:00
  • This is rename command, not removing anything. Should he change the format in comply with his needs – Ermos Jul 14 '17 at 12:10
  • It is removing any character followed by "avi" in all file names, which is not what the OP is asking. Fixing your regex so it doesn´t do unwanted damage isn't hard, but the other answers here already sort of cover that. – tripleee Jul 14 '17 at 14:39

Your Answer

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