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

Nov 22, 2012 - 7 Comments

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

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS X, Tips & Tricks

7 Comments

» Comments RSS Feed

  1. Poi Party says:

    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….

  2. William Scott says:

    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

  3. Bernard says:

    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*

  4. Ciaran says:

    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.

  5. Vinod says:

    I wanted to add the one level parent folder name to all the files within the directory.

    Any commands/ help….

  6. Chris says:

    “… 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.

  7. Arun says:

    Very useful tip. Andriod moved my vidoes and photos to LOST.DIR without any extension.. Was able to rename 1000 files in a flash

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates