recursively add file extension to all files

BashFileShellCommand LineFile Extension

Bash Problem Overview


I have a few directories and sub-directories containing files with no file extension. I want to add .jpg to all the files contained within these directories. I've seen bash scripts for changing the file extension but not for just adding one. It also needs to be recursive, can someone help please?

Bash Solutions


Solution 1 - Bash

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. Note also the quotes around {}, so that filenames with spaces (and even newlines...) are properly handled.

Solution 2 - Bash

this will find files without extension and add your .jpg

find /path -type f -not -name "*.*" -exec mv "{}" "{}".jpg \;

Solution 3 - Bash

This is a little late, but I thought I would add that a better solution (although maybe less readable) than the ones so far might be:

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

Using the find | xargs pattern generally results in more efficient execution, as you don't have to fork a new process for each file.

Note that this requires the version of rename found in Debian-flavored distros (aka prename), rather than the traditional rename. It's just a tiny perl script, though, so it would be easy enough to use the command above on any system.

Solution 4 - Bash

like this,

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

I am not expecting you have space separated file names,
If you do, the names will need to be processed a bit.

If you want to execute the command from some other directory,
you can replace the find . with find /target/directory.

Solution 5 - Bash

For renaming all files with no extension in Windows basic you can do ren * *.jpg Since the file as no extension, just use the *, or if you want to change png to jpg use ren *.png *.jpg

Solution 6 - Bash

rename

not sure that it can rename files without extensions (I'm on windows 7 right now)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrobjmillsView Question on Stackoverflow
Solution 1 - BashStephan202View Answer on Stackoverflow
Solution 2 - Bashghostdog74View Answer on Stackoverflow
Solution 3 - BashChad HuneycuttView Answer on Stackoverflow
Solution 4 - BashnikView Answer on Stackoverflow
Solution 5 - BashNunoView Answer on Stackoverflow
Solution 6 - BashdfaView Answer on Stackoverflow