Shell command to bulk change file extensions in a Linux folder/directory

bulk, extensions, linux, ubuntu, shell

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  Top 5 Ways to Fix "No Space Left on Device" Error in Linux

7 thoughts on “Shell command to bulk change file extensions in a Linux folder/directory”

  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