Renaming file extensions in Linux is a common task, especially when handling large numbers of files in web development, data processing, or system administration. Whether you’re standardizing file formats, converting image types, or preparing files for a batch process, the command line offers a fast and efficient way to rename file extensions in bulk.
This guide will show you how to change, append, and remove file extensions using simple Linux shell commands. We’ll cover potential errors, alternative methods, and best practices to ensure your bulk renaming tasks are smooth and error-free.
Table of Contents
Why Rename File Extensions in Bulk?
Some common scenarios where bulk renaming is needed:
- Standardizing image files – Converting
.jpegto.jpgfor compatibility with web applications. - Modifying document formats – Renaming
.txtfiles to.csvfor data import/export. - Updating video formats – Changing
.mpegto.mp4for better support across media players. - Transitioning from old file extensions – Some software may require modernized file formats.
Before renaming, you may want to filter files by size or type first using this method.
Change File Extensions from .jpeg to .jpg in Linux
Many image processing tools and web applications prefer .jpg over .jpeg for consistency. If you want to bulk rename files in Linux, the command line offers a fast way to accomplish this. Before renaming files, you may also want to count the number of files in a directory to verify the total files that will be modified.
for f in *.jpeg; do mv "$f" "$(basename "$f" .jpeg).jpg"; doneHow This Command Works
for f in *.jpeg; do ... done– Loops through all.jpegfiles in the current directory.mv "$f" "$(basename "$f" .jpeg).jpg"– Moves (renames) each file:basename "$f" .jpegremoves the.jpegextension.$(basename "$f" .jpeg).jpgappends.jpgto the filename.
Example Use Case
Before running the command, list the files in your directory using the ls command:
ls
photo1.jpeg
image2.jpeg
graphic3.jpeg Run the command:
for f in *.jpeg; do mv "$f" "$(basename "$f" .jpeg).jpg"; doneNow check the updated files:
ls
photo1.jpg
image2.jpg
graphic3.jpg Handling Errors & Edge Cases
- Suppressing errors when no matching files are found
- If no
.jpegfiles exist, the command may throw an error. Use this to avoid issues:shopt -s nullglob for f in *.jpeg; do mv "$f" "$(basename "$f" .jpeg).jpg"; done shopt -u nullglob
- If no
- Renaming files recursively in subdirectories
- If you have
.jpegfiles in multiple folders, usefind:find . -type f -name "*.jpeg" -exec sh -c 'mv "$1" "${1%.jpeg}.jpg"' _ {} \;
- If you have
Why This Is Useful
- Web developers often need
.jpgfiles for browser compatibility. - Some content management systems (CMS) and image editors prefer
.jpg. - If you’re working with APIs that require strict filename formats, this bulk rename method saves time.
Add an Extension to All Files in a Directory
Sometimes, you may have files without extensions that need a specific format added. This is common when working with logs, text files, or media files that lack proper extensions.
For example, if you have a folder of images missing the .jpg extension, you can add it to all files using this command:
for f in *; do mv "$f" "$f.jpg"; doneHow This Command Works
for f in *; do ... done– Loops through all files in the directory.mv "$f" "$f.jpg"– Renames each file by appending.jpg.
Example Use Case
Let’s say you have a folder of images without extensions:
ls
photo1
image2
graphic3 Run the command:
for f in *; do mv "$f" "$f.jpg"; doneNow check the updated files:
ls
photo1.jpg
image2.jpg
graphic3.jpg Handling Errors & Edge Cases
- Avoid renaming files that already have an extension
- If you run the command as-is, it will append
.jpgeven to files that already have extensions, creating filenames likephoto1.jpg.jpg. - To prevent this, use:
for f in *; do if [[ ! "$f" == *.* ]]; then mv "$f" "$f.jpg" fi done
- If you run the command as-is, it will append
- Only rename specific file types
- If you only want to rename certain types of files (e.g., images without extensions), use:
for f in *; do if file "$f" | grep -q "image"; then mv "$f" "$f.jpg" fi done
- If you only want to rename certain types of files (e.g., images without extensions), use:
- Recursively append extensions in subdirectories
- If your images are spread across multiple folders, use
find:find . -type f ! -name "*.*" -exec mv {} {}.jpg \;
- If your images are spread across multiple folders, use
Why This Is Useful
- Some old software, scripts, or APIs require explicit file extensions.
- If you download images from the internet, they may not have proper extensions.
- File extensions help prevent compatibility issues in automated workflows.
Remove File Extensions from All Files in a Directory
There are cases where you need to remove file extensions rather than change them. This can happen when:
- You need raw filenames for a specific application or script.
- You’re preparing files for batch processing where extensions are unnecessary.
- You want to standardize filenames by stripping redundant extensions.
The following command removes the .jpg extension from all .jpg files in a directory:
for f in *.jpg; do mv "$f" "$(basename "$f" .jpg)"; doneHow This Command Works
for f in *.jpg; do ... done– Loops through all.jpgfiles in the directory.mv "$f" "$(basename "$f" .jpg)"–basename "$f" .jpgremoves.jpgfrom the filename.- The
mvcommand renames each file without the extension.
Example Use Case
Before running the command:
ls
photo1.jpg
image2.jpg
graphic3.jpg Run the command:
for f in *.jpg; do mv "$f" "$(basename "$f" .jpg)"; done
Now check the updated files:
ls
photo1
image2
graphic3 Handling Errors & Edge Cases
- Avoid removing extensions from files that have multiple dots
- Some filenames have multiple extensions, like
photo1.tar.gz. To only remove the last extension, use:for f in *.*; do mv "$f" "${f%.*}"; done
- Some filenames have multiple extensions, like
- Recursively remove extensions from all subdirectories
- If files are spread across multiple folders, use
find:find . -type f -name "*.jpg" -exec sh -c 'mv "$1" "${1%.jpg}"' _ {} \;
- If files are spread across multiple folders, use
- Avoid renaming hidden files
- Hidden files in Linux (starting with
.) can be mistakenly renamed. To exclude hidden files, modify the loop:for f in *.jpg; do [[ "$f" == .* ]] && continue mv "$f" "$(basename "$f" .jpg)" done
- Hidden files in Linux (starting with
Why This Is Useful
- Some server-side scripts require filenames without extensions.
- File organization scripts may work better with raw filenames.
- Removing incorrect extensions avoids confusion in data processing.
Using the Rename Command for Bulk File Renaming
While mv works well for simple renaming tasks, the rename command (also called prename or perl-rename on some systems) provides a more powerful and flexible way to rename files in bulk using regular expressions.
Installing the Rename Command
Most Linux distributions don’t include rename by default. You can install it with:
Ubuntu/Debian:
sudo apt install renameRHEL/CentOS:
sudo yum install prenameArch Linux:
sudo pacman -S perl-renameChange File Extensions from .jpeg to .jpg Using Rename
Instead of looping through each file, you can use a single rename command to bulk rename .jpeg to .jpg:
rename 's/\.jpeg$/.jpg/' *.jpegHow This Command Works
's/\.jpeg$/.jpg/'– A regular expression (regex) that replaces.jpegwith.jpgat the end of filenames ($ensures only the extension is changed).*.jpeg– Specifies that only.jpegfiles should be renamed.
Example Use Case
Before running the command:
ls
photo1.jpeg
image2.jpeg
graphic3.jpeg Run the command:
rename 's/\.jpeg$/.jpg/' *.jpegNow check the updated files:
ls
photo1.jpg
image2.jpg
graphic3.jpg Appending an Extension Using Rename
Sometimes, you may want to remove file extensions instead of changing them, especially when working with automated scripts. If you need to further process these files, you can also pass them as command-line arguments to a script for additional modifications.
rename 's/$/.jpg/' *Removing an Extension Using Rename
To remove .jpg from all files, use:
rename 's/\.jpg$//' *.jpgRenaming Files Recursively (Including Subdirectories)
If your images are stored in subdirectories, you can rename them recursively:
find . -type f -name "*.jpeg" -exec rename 's/\.jpeg$/.jpg/' {} +Why Use Rename Instead of MV?
| Feature | mv Command | rename Command |
|---|---|---|
| Simplicity | Easy for small tasks | Better for large-scale renaming |
| Regex Support | No regex, only patterns | Full regex support |
| Performance | Slower for large files | Faster for bulk renaming |
| Recursive Renaming | Needs find | Works with find but simpler |
When to Use Rename vs. MV
- Use
mvwhen renaming a small number of files and keeping it simple. - Use
renamewhen handling large batches of files or needing regex-based transformations.
Best Practices for Bulk Renaming Files in Linux
Before renaming files in bulk, follow these best practices to avoid mistakes:
1. Always Back Up Important Files
- If you’re renaming large numbers of files, create a backup first:
cp -r /path/to/files /path/to/backup
2. Use the Dry-Run Option Before Renaming
- The
renamecommand has a dry-run mode that simulates renaming without making actual changes. Use this before executing the final command:rename -n 's/\.jpeg$/.jpg/' *.jpeg
3. Verify File Names Before and After Renaming
- To see all files before renaming, use:
ls -l *.jpeg - After renaming, confirm the changes with:
ls -l *.jpg
4. Handle Spaces and Special Characters in Filenames
- If your filenames contain spaces or special characters, wrap them in double quotes:
for f in *.jpeg; do mv "$f" "$(basename "$f" .jpeg).jpg"; done - For
rename, you can handle whitespace issues by escaping spaces:rename 's/ /_/g' *.jpegThis replaces spaces with underscores (_) before renaming.
5. Be Cautious with Recursive Renaming
- If renaming files in subdirectories, test first:
find . -type f -name "*.jpeg" - Then apply renaming with confidence:
find . -type f -name "*.jpeg" -exec rename 's/\.jpeg$/.jpg/' {} +
Final Thoughts: Choosing the Best Method for Bulk Renaming in Linux
Which Method Should You Use?
| Use Case | Best Command |
|---|---|
| Change one extension to another | mv or rename |
| Append an extension to files | mv |
| Remove an extension | rename |
| Rename files in subdirectories | find + rename |
| Handle complex filename changes | rename with regex |
Linux provides multiple ways to rename file extensions in bulk, and the best method depends on your specific needs. The mv command works well for simple tasks, while the rename command provides regex-based transformations for large-scale renaming.
By following best practices, testing commands before execution, and using backup strategies, you can safely and efficiently rename thousands of files in seconds.
