Skip to main navigation Skip to main content Skip to page footer
Bash tip: How to change file extensions in batch mode

Bash tip: How to change file extensions in batch mode

Have the article read aloud.

Loading the Elevenlabs Text to Speech AudioNative Player...
| Web Development | Estimated reading time : min.

If you have a whole series of files whose endings you want to change, renaming them manually can be quite tedious. Fortunately, this can be done super easily and quickly in the shell - with a single command. Whether it's .txt to .xlf, .csv to .json or any other combination, the trick works for all file extensions.

Here is an example of how you can rename all .txt files in a directory to .xlf:

for file in *.txt; do mv "$file" "${file%.txt}.xlf"; done

What exactly happens here?

  • for file in *.txt: This part of the command searches the current directory for all files ending with .txt. You can of course replace .txt with any extension.
  • mv "$file" "${file%.txt}.xlf": The mv command takes over the renaming. ${file%.txt} removes the old ending and .xlf is added. Again, .txt and .xlf are just placeholders for the endings you want to change.

If you want to see which files are affected first, you can also test the whole thing without renaming the files straight away. Simply use this command:

for file in *.txt; do echo mv "$file" "${file%.txt}.xlf"; done

This lists all changes without actually making them. This way you can make sure that everything fits before you execute the command correctly.

This simple shell trick saves you a lot of time and makes editing lots of files at once really efficient.

Back

Who writes here?

Hi, I am Wolfgang.

Since 2006, I've been diving deep into the fascinating world of TYPO3 - it's not only my profession, but also my passion. My path has taken me through countless projects, and I have created hundreds of professional video tutorials focusing on TYPO3 and its extensions. I love unraveling complex topics and turning them into easy-to-understand concepts, which is also reflected in my trainings and seminars.

As an active member of the TYPO3 Education Committee, I am committed to keeping the TYPO3 CMS Certified Integrator exam questions current and challenging. Since January 2024, I am proud to be an official TYPO3 Consultant Partner!

But my passion doesn't end at the screen. When I'm not diving into the depths of TYPO3, you'll often find me on my bike, exploring the picturesque trails around Lake Constance. These outdoor excursions are my perfect balance - they keep my mind fresh and always provide me with new ideas.

Der TYPO3 Newsletter

TYPO3-Insights direkt in dein Postfach! 
Hol dir monatliche Updates, praktische Tipps und spannende Fallstudien. 
Übersichtlich, zeitsparend, ohne Spam. 
Bist du dabei? Jetzt für den Newsletter anmelden!

Trage dich hier ein, um den Newsletter zu erhalten.