HeatWare.net header image
HeatWare.net
TwitterRSSEmail
  • Home
  • Windows
    • Windows 8
    • Windows 7
    • Windows Vista
    • Windows XP
  • Linux/Unix
  • Mac
  • Mobile
    • Android
    • iOS
    • Phones (Help / Resources)
  • Software / Programming
    • Free Software
    • Programming – General
    • PHP
    • Ruby/Rails
    • Quality Assurance (QA)
    • Software – General
    • Software Help
    • Databases
  • Cool Websites
  • Other
    • Deals & Bargains
    • News
    • Video Games
    • Hardware
    • Electronics
  • About

7 comments / January 20, 2009 / sood / Linux/Unix

Shell command to bulk change file extensions in a directory (Linux)

This article will explain how to change the file extension for all files in a directory in Linux using a simple bash shell command.

1. Change from one extension to another

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;

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

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

Related Posts

  • Using Linux ‘head’ command to preview a file
  • Find the number of files in a directory (Linux)
  • Free Linux utility to convert PDF to Text file
  • How to Find Files Owned by Group(s) in Linux
  • Scripting the Linux 'passwd' command easily without interactive prompt

Linux Shell

7 comments… add one
  1. Jeff October 10, 2011, 1:59 am

    Actually, #1 above should read:

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

    Try it for yourself!

  2. sood October 10, 2011, 8:41 am

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

    Thanks!
    heat

  3. Boop April 16, 2013, 1:41 am

    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.

  4. Joseph June 21, 2013, 8:13 pm

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

  5. isimless July 20, 2014, 12:03 am

    working script to remove extension is in my case:

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

  6. Mansoor June 16, 2015, 12:26 am

    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?

  7. David Kim October 14, 2015, 10:35 am

    Thanks! That is very concise and helpful! 🙂

Cancel reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • Why You Should Consider Diving Apps
  • How to Buy a Smartphone that Fits Your Budget
  • How to Overcome Frustrating PDF Stress
  • Convert PDF to Word: Easy, Reliable and Quality Conversion
  • PDF to Excel App ­ – A New Way of Handling Tricky Documents

Recent Comments

  • Valarie Walter on Basic Troubleshooting Steps for your Cell Phone
  • John Mists on A Brief History of Android OS
  • syarif on PostgreSQL: How to reload config settings without restarting database
  • Raghu on How to SSH to a server using Ruby – Part I
  • francisco clemente on Basic Troubleshooting Steps for your Cell Phone

Tags

ACSLS Android Cell Phones Cool Software Cool Websites Databases Facebook File System Free Software G2x Galaxy S5 Hot Deals iOS iPhone ISO LG Linux Linux/Unix Mac Mobile mysql OpenStack OS X PHP Postgres PostgreSQL Printers Programming ruby Samsung Galaxy S6 Shell Smartphones Sun T-Mobile Tips Tips & Tricks Ubuntu Unix Virtualization VMWare Windows Windows 7 Windows 8 Windows Vista Windows XP

Latest Tweet

Follow @HeatwaredotNet

SP
@HeatwaredotNet

  • Why You Should Consider Diving Apps https://t.co/Is41cdUv2I #diving-apps
    about 4 years ago

All Categories

Other Links

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2015 — HeatWare