Add a file extension to all files recursively

I have several directories and subdirectories containing files without a file extension. I want to add .jpg to all files contained in these directories. I saw bash scripts to change the file extension, but not to add it. It must also be recursive, can anyone help?

+78
command-line file bash shell file-extension
Jul 10 '09 at 9:04
source share
6 answers

Alternative command without an explicit loop ( man find ):

 find . -type f -exec mv '{}' '{}'.jpg \; 

Explanation: this recursively finds all files ( -type f ), starting from the current directory ( . ) And applies the move command ( mv ) to each of them. Also, look at the quotation marks around {} so that file names with spaces (and even newlines ...) are processed correctly.

+167
Jul 10 '09 at 9:14
source share

it will find files without extension and add your .jpg

 find /path -type f -not -name "*.*" -exec mv "{}" "{}".jpg \; 
+47
Jul 10 '09 at 9:27
source share

This is a bit late, but I thought I would add that a better solution (although perhaps less readable) than those that may still be:

 find /path -type f -not -name "*.*" -print0 | xargs -0 rename 's/(.)$/$1.jpg/' 

Using the find | xargs pattern find | xargs find | xargs usually leads to more efficient execution, since you do not need to fork a new process for each file.

Please note that this requires a renaming version found in distributions with the Debian extension (aka prename), not a traditional renaming. This is just a tiny perl script, so it would be easy enough to use the command above on any system.

+9
Jul 14 '09 at 3:51
source share

like this,

 for f in $(find . -type f); do mv $f ${f}.jpg; done 

I don’t expect you to have names separated by spaces,
If you do, the names will need to be processed a bit.

If you want to execute a command from another directory,
you can replace find . on find /target/directory .

+4
Jul 10 '09 at 9:07
source share

rename

not sure if it can rename files without extensions (now I'm on Windows 7)

0
Jul 10 '09 at 9:28
source share

To rename all files without extension in Windows basic you can do ren * *.jpg Since the file is not an extension, just use *, or if you want to change png to jpg use ren *.png *.jpg

0
Nov 08 '15 at 17:25
source share



All Articles