Ubuntu – How to mass add file extension

batch-renamecommand linefilename

My photo importer moved files from my camera and renamed them, but I forgot to specify that it should keep the file extension. So I have several folders with hundreds of JPG & AVI files that are all missing their proper extension.

There aren't that many video files so I can manually rename them either before of after a general mass-rename to JPG.

To make things more complicated, the filenames contain spaces, so I can't just mv "*" "*.jpg" because, well, that doesn't work (but that's what I'd do, coming from DOS/Windows).

I know Linux is full of powerful commands. How can I mass-rename files to add a .jpg extension, when those files contain spaces?

Please don't say "don't use spaces" because that's really not what I'm asking about. Thanks.

Best Answer

Shell globs should work even with spaces in the names if used properly e.g.

rename -nv -- 's/$/.jpg/' *

or

for file in *; do echo mv -- "$file" "$file.jpg"; done

[NOTE: these are 'no ops' until the n switch or the echo are removed - so you can check the correct replacement before committing]

If you do want to automatically distinguish between jpg and avi files that would also be possible using a more complex loop and the file or mimetype command