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

Nov 22, 2012 - 11 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 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:
  • 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

.

Related articles:

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

11 Comments

» Comments RSS Feed

  1. David says:

    This was a life saver…exactly what I needed. Thanks!

  2. Joe S says:

    If you get an error “mv: rename “?? to “foo.log”: No such file or directory

    just check the double quotes that you pasted in.

  3. Doug says:

    Thanks this saved me!

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

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

    • David Perry says:

      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.

      :)

  6. Vinod says:

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

    Any commands/ help….

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

  8. 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*

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

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

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

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

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site