Add a File Extension to a Group of Files from the Command Line in OS X

The quickest way to add a file extension to a group of files that don’t currently have one is by using the command line in Mac OS X. In the example below, we’ll add a “.txt” extension to all files in a single directory, but subbing .txt in the command string will add a different extension instead. Before beginning, it’s a good idea to do the following:
- Be sure to have file extensions visible on all files in OS X, that way the extension change will be visible in the Finder in addition to the command line
- Place all files that need the extension added into a single and separate directory
Next, launch Terminal (found in /Applications/Utilities/) and do the following:
- Change to the directory containing the files by typing:
- Once inside the directory, use the following command:
- Confirm the change by typing “ls” to list the directory contents
cd /path/to/directory
for i in *; do mv "$i" "$i.txt"; done
You can also drag and drop a directory from the Finder into the terminal window to print out it’s path, rather than manually entering it.
Below is a complete example showing a change to the directory, listing the original contents, executing the appropriate command to add the extension, and finally another listing showing the original files with the new .txt extension added.
$ mkdir ~/Desktop/FilesThatNeedExtensionsAdded/
$ mv tes* ~/Desktop/FilesThatNeedExtensionsAdded/
$ cd ~/Desktop/FilesThatNeedExtensionsAdded/
$ ls
test test1 test2 test3 test4$ for i in *; do mv “$i” “$i.txt”; done
$ ls
test.txt test1.txt test2.txt test3.txt test4.txt
As mentioned before, to add a different file extension just replace the “.txt” with something else, like “.jpg” or “.rtf”. The wildcards can also be adjusted to match file name commonalities.
Thanks to Thom for the tip idea

Helpful tip I can definitely see when I would need to use this.
BTW not to be overly critical but I think Will is better at writing the command line tips….
zsh, which is essentially a more modern, augmented form of ksh, makes this (and all batch renaming) much more fun with zmv:
zsh-% ls
test0 test1 test2 test3 test4
zsh-% mmv * *.txt
zsh-% ls
test0.txt test1.txt test2.txt test3.txt test4.txt
zsh-% which mmv
mmv: aliased to noglob zmv -W
You can also drag and drop a directory from the Finder into the terminal window to print out it’s path, rather than manually entering it.
its, not it’s
*sigh*
Great tip. Unfortunately exactly 24 hours too late for me. I ran into this problem yesterday and figured it would have taken more time to find the solution than to just do it by hand.
I wanted to add the one level parent folder name to all the files within the directory.
Any commands/ help….
“… add a file extension to a group of files that don’t currently have one …”
This is perhaps misleading. It adds the .txt extension to *every* file in the directory, including those that already have an extension. eg x.rtf becomes x.rtf.txt.