Add a File Extension to a Group of Files from the Command Line in Mac 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 Mac 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
How to Batch Add File Extensions in Mac OS Command Line
Assuming you met the prior requirements, 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
This was a life saver…exactly what I needed. Thanks!
If you get an error “mv: rename “?? to “foo.log”: No such file or directory
just check the double quotes that you pasted in.
Thanks this saved me!
Very useful tip. Andriod moved my vidoes and photos to LOST.DIR without any extension.. Was able to rename 1000 files in a flash
“… 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.
If you followed all the instructions you would have noticed this…
Place all files that need the extension added into a single and separate directory.
:)
I wanted to add the one level parent folder name to all the files within the directory.
Any commands/ help….
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.
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*
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
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….