Ads 468x60px

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


Add file extension to a group of files 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 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:
  • cd /path/to/directory
  • Once inside the directory, use the following command:
  • for i in *; do mv "$i" "$i.txt"; done
  • Confirm the change by typing “ls” to list the directory contents
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