Shell command to bulk change file extensions on Linux

Linux Shell Prompt

This article will explain how to change the file extension for all files in a directory in Linux using a simple bash shell command. While you are on a learning path, be sure to checkout other useful Linux articles such as Insert/Add String to Beginning of a File and Linux – How to find the number of files in a folder.

Change from one extension to another in Linux

The command below will rename all files with the extension .php4 to .php
for f in *.php4; do mv $f `basename $f .php4`.php; done;

Add (append) an extension to all files

The command below add the extension .txt to all files in the directory
for f in *; do mv $f `basename $f `.txt; done;

Remove (delete) an extension from all files

The command below remove the extension .txt from all files in the directory
for f in *.txt; do mv $f `basename $f .txt`; done;

See also  Tutorial - How to Open & Extract a .tar.bz2 File
Photo of author
As Editor in Chief of HeatWare.net, Sood draws on over 20 years in Software Engineering to offer helpful tutorials and tips for MySQL, PostgreSQL, PHP, and everyday OS issues. Backed by hands-on work and real code examples, Sood breaks down Windows, macOS, and Linux so both beginners and power-users can learn valuable insights.

7 thoughts on “Shell command to bulk change file extensions on Linux”

  1. Jeff,
    Thank you very much for the correction. I have updated the article to reflect what you wrote.

    Thanks!
    heat

  2. For the sake of correctness, everything should be surrounded with quotes.
    So:
    for f in “*.php4”; do mv “$f” “`basename “$f” .php4`.php”; done;

    Those pesky spaces will trash all that data if we’re not careful.

  3. Thank you for this beautiful and functional piece of code it did exactly what I needed it to do. 🙂

  4. working script to remove extension is in my case:

    for f in *.php4; do mv “$f” “`basename “$f” .php4`”; done;

  5. hi i want to know how to change one file extension into another extension.
    Example: i have text file which is by defalt geditor file so i want to open it through libre Office file, so how can i change the extension of a file through CLI (Command) mode?

Leave a Comment