Sunday, May 19, 2024
23
rated 0 times [  23] [ 0]  / answers: 1 / hits: 26982  / 1 Year ago, thu, may 11, 2023, 7:23:02

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.


More From » command-line

 Answers
7

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


[#24545] Thursday, May 11, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
feeous

Total Points: 102
Total Questions: 122
Total Answers: 119

Location: Netherlands
Member since Thu, Jul 1, 2021
3 Years ago
;