up vote 3 down vote favorite
share [g+] share [fb]

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.

link|improve this question

68% accept rate
This question will probably have an answer for you, as it is a bit similar : superuser.com/questions/16007/… – 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
ChrisF, yes, I did. Thank you. – J. Pablo Fernández Aug 27 '09 at 5:38
feedback

6 Answers

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

link|improve this answer
1  
Will that work recursively in all folders? – J. Pablo Fernández Aug 26 '09 at 19:03
The command prompt still has its uses :) Yeah DOS! – Stewbob Aug 26 '09 at 19:06
It will not - you'll need to use the 'for' command as demonstrated here: stackoverflow.com/questions/210413/… – Jeffrey Aug 26 '09 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 '09 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 '09 at 21:42
feedback
up vote 3 down vote accepted

I did it this way

find . -type f -iregex ".*[^\(\.jpg\)]" -exec mv "{}" "{}.jpg" ";"
link|improve this answer
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
feedback

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

link|improve this answer
feedback

On Linux

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

On Windows

I like to use Rename4u which is a freeware utility.

link|improve this answer
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. – J. Pablo Fernández 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
feedback

Extension Renamer does the job.

link|improve this answer
feedback

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.
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.