Add the extension, jpg, to a lot of files

9,315

Solution 1

I did it this way

find . -type f -iregex ".*[^\(\.jpg\)]" -exec mv "{}" "{}.jpg" ";"

Solution 2

You can do it through cmd on windows.

rename * *.jpg


Edit:
To apply to nested folders, you can use;

for /r %x in (*) do rename "%x" *.jpg

Solution 3

If using powershell is an option, then this post from SO should be exactly what you want.

Solution 4

On Linux

ls | while read file ; do mv $file $file.jpg; done

On Windows

I like to use Rename4u which is a freeware utility.

Share:
9,315

Related videos on Youtube

Kurru
Author by

Kurru

You can find my blog at https://pupeno.com where I publish about coding and other stuff.

Updated on September 17, 2022

Comments

  • Kurru
    Kurru over 1 year

    I have a folder with a lot of folders with a lot of files and maybe more folders with more files, where some files lost their extension. I believe they are all jpgs, but I could be wrong. Any ideas how to re-add the extensions to all these files without doing it one by one?

    I can do it on Windows 7 or Ubuntu 8.10.

    • Gnoupi
      Gnoupi over 14 years
      This question will probably have an answer for you, as it is a bit similar : superuser.com/questions/16007/…
    • ChrisF
      ChrisF over 14 years
      Did you check that you haven't turned off the display of the extension on those folders before embarking on the rename? Basic question I know, but you never know.
  • Jeffrey
    Jeffrey over 14 years
    Just got to be careful not to append a .jpg extension to IMAGE001.jpg
  • Kurru
    Kurru over 14 years
    Wouldn't the Linux version add the extension to all the files? Not all the files are missing the extension.
  • Kurru
    Kurru over 14 years
    Will that work recursively in all folders?
  • Stewbob
    Stewbob over 14 years
    The command prompt still has its uses :) Yeah DOS!
  • Jeffrey
    Jeffrey over 14 years
    It will not - you'll need to use the 'for' command as demonstrated here: stackoverflow.com/questions/210413/…
  • RJFalconer
    RJFalconer over 14 years
    Thanks Jeffrey; I've not come across "for" before! That's pretty nifty. I added that to the solution body.
  • UNK
    UNK over 14 years
    @J Pablo, .jpg.jpg still opens as a jpg ;) In windows I'd do: ren * *.jpg but you could hack something together with a batch file to only do that if something didn't have an extension - I'm too lazy, myself. If it works, it works, right?
  • Joey
    Joey over 14 years
    +1 for a nice answer that didn't involve installing other operating systems or a heap of other programs.
  • NVRAM
    NVRAM over 14 years
    Nice. FWIW, a simpler method than -iregex is to use the negative of a name match: find . -type f \! -name '.jpg' (...). Also, I try always to use an absolute path (*/bin/mv) when using find/-exec.