4

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.

2
  • This question will probably have an answer for you, as it is a bit similar : superuser.com/questions/16007/…
    – Gnoupi
    Aug 26, 2009 at 20:11
  • 1
    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.
    – ChrisF
    Aug 26, 2009 at 20:16

6 Answers 6

7

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

5
  • 1
    Will that work recursively in all folders? Aug 26, 2009 at 19:03
  • The command prompt still has its uses :) Yeah DOS!
    – Stewbob
    Aug 26, 2009 at 19:06
  • It will not - you'll need to use the 'for' command as demonstrated here: stackoverflow.com/questions/210413/…
    – Jeffrey
    Aug 26, 2009 at 19:14
  • 1
    Thanks Jeffrey; I've not come across "for" before! That's pretty nifty. I added that to the solution body.
    – RJFalconer
    Aug 26, 2009 at 20:20
  • 2
    +1 for a nice answer that didn't involve installing other operating systems or a heap of other programs.
    – Joey
    Aug 26, 2009 at 21:42
3

I did it this way

find . -type f -iregex ".*[^\(\.jpg\)]" -exec mv "{}" "{}.jpg" ";"
1
  • 1
    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.
    – NVRAM
    Sep 28, 2009 at 18:05
2

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

1

On Linux

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

On Windows

I like to use Rename4u which is a freeware utility.

3
  • Just got to be careful not to append a .jpg extension to IMAGE001.jpg
    – Jeffrey
    Aug 26, 2009 at 18:52
  • Wouldn't the Linux version add the extension to all the files? Not all the files are missing the extension. Aug 26, 2009 at 19:00
  • @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?
    – Phoshi
    Aug 26, 2009 at 20:32
0

Extension Renamer does the job.

0

For Linux (or MSWindows w/CygWin)

If you wish to only add a suffix to files that are actually JPEGs, try this:

$ find . -type f  ! -name '*.jpg'  -print | while read f
> do case "$(file "$f")" in
>    *JPEG*) mv -iv "$f" "$f.jpg" ;;
>    esac
> done

Which will:

  1. Print paths that are files w/o a *.jpg suffix (find),
  2. Check those files' contents (file $f),
  3. For JPEG files, rename them with a _.jpg suffix.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .