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  4 Free Ways to Convert a PDF to Text File on Linux
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. For questions or feedback, he can be reached at sood@heatware.net.