Add file extension to files with bash

LinuxBash

Linux Problem Overview


What is the good way to add file extension ".jpg" to extension-less files with bash?

Linux Solutions


Solution 1 - Linux

for f in *.jpg; do mv "$f" "${f%.jpg}"; done
for f in *; do mv "$f" "$f.jpg"; done

Solution 2 - Linux

You can use rename:

rename 's/(.*)/$1.jpg/' *

Solution 3 - Linux

Another way - without loops

find . -type f -not -name "*.*" -print0 |\
xargs -0 file |\
grep  'JPEG image data' |\
sed 's/:.*//' |\
xargs -I % echo mv % %.jpg

Breakdown:

  • find all files without extension
  • check the file type
  • filter out only JPG files
  • delete filetype info
  • xargs run the "mv" for each file

the above command is for dry run, after it you should remove the "echo" before mv

EDIT Some people suggesting that here is needed "Wrap path arguments in quotes; avoids argument splitting on paths with spaces".

Usually, this recommendation is true, in this case isn't. Because, here the % is got replaced not by shell expansion but by the xargs internally (directly), so the % will be substituted correctly even with spaces in filenames.

Simple demo:

$ mkdir xargstest
$ cd xargstest

# create two files with spaces in names
$ touch 'a b' 'c d'

$ find . -type f -print
./c d
./a b
# notice, here are spaces in the above paths

#the actual xargs mv WITHOUT quotes
$ find . -type f -print | xargs -I % mv % %.ext

$ find . -type f -print
./a b.ext
./c d.ext
# the result is correct even in case with spaces in the filenames...

Solution 4 - Linux

Simple, cd to the directory where your files are and:

for f in *;do mv $f $f.jpg;done

Solution 5 - Linux

dry run:

 rename -n s/$/.jpg/ *

actual renaming:

 rename s/$/.jpg/ *

Solution 6 - Linux

find . | while read FILE; do if [ $(file --mime-type -b "$FILE") == "image/jpeg" ]; then mv "$FILE" "$FILE".jpg; fi; done;

Solution 7 - Linux

In my case i was not aware of the filetype so i used the mv command with the help of the file command to examine and possibly find the file type. This solution might not be perfect for all files since the file command might not recognize the filetype but it worked mostly good for me.

for f in *; do ext=$(file $f | awk '{print $2;}'); mv -n "$f" "$f.$ext"; done

The use of awk is to strip the second word of the string returned from the command file that is actually the extension.

Solution 8 - Linux

rename --dry-run * -a ".jpg" # test  
* -a ".jpg" # rename

Solution 9 - Linux

You can use move multiple files. I am a maintainer of this project. The syntax is simple.

mmf files*

It will open your $EDITOR with all files names, or vim by default and you can simply highlight the end of all file names using Ctrl+v+G in vim , save the file,quit and that it , all your files are renamed

Solution 10 - Linux

Ryan Li

The correct syntax for adding a file extension to multiple files within a directory which do not have a file extension is

find . | while read FILE; do if [[ -n `file --mime-type "$FILE" | grep 'message/rfc822'` ]]; then  mv "$FILE" "$FILE".eml; fi; done;

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
QuestionAdrianView Question on Stackoverflow
Solution 1 - LinuxSeth RobertsonView Answer on Stackoverflow
Solution 2 - LinuxKim StebelView Answer on Stackoverflow
Solution 3 - Linuxjm666View Answer on Stackoverflow
Solution 4 - LinuxbharathView Answer on Stackoverflow
Solution 5 - LinuxAlexeyView Answer on Stackoverflow
Solution 6 - LinuxRyan LiView Answer on Stackoverflow
Solution 7 - LinuxPaoloView Answer on Stackoverflow
Solution 8 - LinuxRoman Rhrn NesterovView Answer on Stackoverflow
Solution 9 - LinuxAlmas AbdrazakView Answer on Stackoverflow
Solution 10 - LinuxDillon BeresfordView Answer on Stackoverflow