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.

Pablo Fernandez
  • 10,871
  • 23
  • 67
  • 100
  • This question will probably have an answer for you, as it is a bit similar : http://superuser.com/questions/16007/how-can-i-mass-rename-files-from-the-command-line – Gnoupi Aug 26 '09 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 '09 at 20:16

6 Answers6

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

RJFalconer
  • 10,195
  • 4
  • 43
  • 51
3

I did it this way

find . -type f -iregex ".*[^\(\.jpg\)]" -exec mv "{}" "{}.jpg" ";"
Pablo Fernandez
  • 10,871
  • 23
  • 67
  • 100
  • 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 '09 at 18:05
2

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

Jeffrey
  • 2,606
  • 4
  • 28
  • 37
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.

djhowell
  • 3,721
  • 1
  • 22
  • 22
  • Just got to be careful not to append a .jpg extension to IMAGE001.jpg – Jeffrey Aug 26 '09 at 18:52
  • Wouldn't the Linux version add the extension to all the files? Not all the files are missing the extension. – Pablo Fernandez Aug 26 '09 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 '09 at 20:32
0

Extension Renamer does the job.

ctzdev
  • 2,360
  • 7
  • 29
  • 48
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.
NVRAM
  • 828
  • 8
  • 20