Bulk Rename File Extensions in Linux Using Shell Commands

Linux Shell Prompt

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.

Why Rename File Extensions in Bulk?

Some common scenarios where bulk renaming is needed:

  • Standardizing image files – Converting .jpeg to .jpg for compatibility with web applications.
  • Modifying document formats – Renaming .txt files to .csv for data import/export.
  • Updating video formats – Changing .mpeg to .mp4 for 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"; done

How This Command Works

  1. for f in *.jpeg; do ... done – Loops through all .jpeg files in the current directory.
  2. mv "$f" "$(basename "$f" .jpeg).jpg" – Moves (renames) each file:
    • basename "$f" .jpeg removes the .jpeg extension.
    • $(basename "$f" .jpeg).jpg appends .jpg to 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"; done

Now 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 .jpeg files 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
  • Renaming files recursively in subdirectories
    • If you have .jpeg files in multiple folders, use find: find . -type f -name "*.jpeg" -exec sh -c 'mv "$1" "${1%.jpeg}.jpg"' _ {} \;

Why This Is Useful

  • Web developers often need .jpg files 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"; done

How This Command Works

  1. for f in *; do ... doneLoops through all files in the directory.
  2. 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"; done

Now 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 .jpg even to files that already have extensions, creating filenames like photo1.jpg.jpg.
    • To prevent this, use: for f in *; do if [[ ! "$f" == *.* ]]; then mv "$f" "$f.jpg" fi done
  • 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
  • Recursively append extensions in subdirectories
    • If your images are spread across multiple folders, use find: find . -type f ! -name "*.*" -exec mv {} {}.jpg \;

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)"; done

How This Command Works

  1. for f in *.jpg; do ... done – Loops through all .jpg files in the directory.
  2. mv "$f" "$(basename "$f" .jpg)"
    • basename "$f" .jpg removes .jpg from the filename.
    • The mv command 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
  • 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}"' _ {} \;
  • 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

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 rename

RHEL/CentOS:

sudo yum install prename

Arch Linux:

sudo pacman -S perl-rename

Change 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/' *.jpeg

How This Command Works

  • 's/\.jpeg$/.jpg/' – A regular expression (regex) that replaces .jpeg with .jpg at the end of filenames ($ ensures only the extension is changed).
  • *.jpeg – Specifies that only .jpeg files should be renamed.

Example Use Case

Before running the command:

ls
photo1.jpeg  
image2.jpeg  
graphic3.jpeg  

Run the command:

rename 's/\.jpeg$/.jpg/' *.jpeg

Now 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$//' *.jpg

Renaming 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?

Featuremv Commandrename Command
SimplicityEasy for small tasksBetter for large-scale renaming
Regex SupportNo regex, only patternsFull regex support
PerformanceSlower for large filesFaster for bulk renaming
Recursive RenamingNeeds findWorks with find but simpler

When to Use Rename vs. MV

  • Use mv when renaming a small number of files and keeping it simple.
  • Use rename when 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 rename command 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' *.jpeg This 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 CaseBest Command
Change one extension to anothermv or rename
Append an extension to filesmv
Remove an extensionrename
Rename files in subdirectoriesfind + rename
Handle complex filename changesrename 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.

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.