How To Rename In Linux : Applying Basic Linux Commands

Renaming files and folders in Linux follows the same core command, but each has its own considerations. If you are searching for how to rename in linux, you have come to the right place. This guide will walk you through every method, from simple single file renames to bulk operations, with clear examples and practical tips.

Linux offers several ways to rename items, and the best method depends on what you need. The most common command is mv, which stands for “move.” It works for both files and directories. You can also use rename for batch jobs, or a simple file manager for visual tasks.

How To Rename In Linux

Before we get into the details, let us cover the absolute basics. The mv command is your primary tool. It is simple, fast, and works everywhere. You do not need extra software or permissions beyond what you own.

Using The Mv Command For Single Files

The syntax is straightforward: mv oldname newname. You run this in the terminal. For example, to rename report.txt to final_report.txt, type:

mv report.txt final_report.txt

That is it. The file is now renamed. There is no confirmation message unless something goes wrong. If the new name already exists, mv will overwrite it without asking. To avoid accidents, use the -i flag for interactive mode:

mv -i report.txt final_report.txt

This will prompt you before overwriting. Press y to confirm or n to cancel.

Renaming Directories With Mv

Directories work the same way. To rename a folder from project to old_project, run:

mv project old_project

Be careful though. If old_project already exists, mv will move the source directory inside it instead of renaming. This is a common mistake. Always check that the target name does not exist if you want a rename.

Moving Vs Renaming: The Same Command

In Linux, renaming is essentially moving a file to a new name in the same location. The mv command handles both. When you move a file to a different directory, you are also renaming it if you give it a new name there. For instance:

mv file.txt /home/user/docs/newfile.txt

This moves file.txt to the docs folder and renames it to newfile.txt. Understanding this helps you avoid confusion.

Renaming Multiple Files One By One

If you have several files to rename individually, you can run mv multiple times. But that is slow. A better approach is to use a loop in the shell. For example, to rename all .txt files to .bak:

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

This loop goes through each .txt file, removes the extension, and adds .bak. The ${file%.txt} syntax strips the suffix. You can adapt this for other patterns.

Using The Rename Command For Bulk Operations

For more complex batch renames, the rename command is powerful. It uses Perl expressions to transform filenames. Note that there are two versions: rename from util-linux and prename from Perl. The Perl version is more common on Debian-based systems.

To rename all .jpeg files to .jpg, use:

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

The s/old/new/ is a substitution pattern. The $ anchors the end of the filename. This command is case-sensitive. For case-insensitive, add the i flag: s/\.jpeg$/.jpg/i.

Practical Examples With Rename

  • Convert all filenames to lowercase: rename 'y/A-Z/a-z/' *
  • Remove spaces from filenames: rename 's/ /_/g' *
  • Add a prefix to files: rename 's/^/backup_/' *
  • Change file extensions from .htm to .html: rename 's/\.htm$/.html/' *.htm

Always test with the -n flag first. This shows what would happen without making changes:

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

This is a safe way to avoid mistakes.

Renaming With A File Manager (GUI)

If you prefer a graphical interface, most Linux file managers support renaming. In Nautilus (GNOME), right-click a file and select “Rename.” You can also press F2. For batch renaming, select multiple files and choose “Rename” from the context menu. A dialog appears where you can replace text, add numbers, or change case.

This method is intuitive but limited. For advanced patterns, the terminal is more flexible.

Handling Special Characters In Filenames

Filenames with spaces, hyphens, or special characters need careful handling. Always quote the filename in the terminal:

mv "my file.txt" "my_file.txt"

Alternatively, use escape characters with a backslash:

mv my\ file.txt my_file.txt

Tab completion helps avoid typos. Press Tab after typing part of the name to auto-complete.

Renaming Files With Dates Or Numbers

Often you need to add timestamps to filenames. You can combine mv with the date command. For example, to rename log.txt to log_2025-03-15.txt:

mv log.txt "log_$(date +%F).txt"

The $(date +%F) inserts the current date in YYYY-MM-DD format. This works in scripts for automated backups.

Renaming Files In Subdirectories

To rename files inside subdirectories, you need to traverse the directory tree. Use the find command combined with mv. For instance, to rename all .txt files to .bak recursively:

find . -type f -name "*.txt" -exec mv {} {}.bak \;

This finds every .txt file and appends .bak to its name. The {} is a placeholder for the filename. The \; ends the command.

For more control, use a loop with find:

find . -type f -name "*.txt" | while read file; do mv "$file" "${file%.txt}.bak"; done

This handles filenames with spaces safely.

Renaming With A Script For Complex Tasks

When the built-in tools are not enough, write a small shell script. For example, to rename files by removing a prefix:

#!/bin/bash
for file in prefix_*; do
    newname="${file#prefix_}"
    mv "$file" "$newname"
done

Save this as rename_script.sh, make it executable with chmod +x rename_script.sh, and run it. Scripts give you full control over renaming logic.

Common Mistakes And How To Avoid Them

  • Overwriting files: Always use -i or -n flags.
  • Renaming directories into existing ones: Check the target does not exist.
  • Forgetting to quote filenames: Use quotes for spaces or special chars.
  • Running rename without testing: Use -n first.
  • Using wildcards incorrectly: Test with echo to see what matches.

Renaming Files With A Graphical Bulk Renamer

For users who want a GUI for batch operations, tools like gprename or krrename are available. Install them via your package manager:

sudo apt install gprename

These apps let you preview changes before applying. They are great for beginners but less flexible than the command line.

Renaming With The Mv Command In A Loop

Let us revisit loops for a moment. You can rename files by index numbers. For example, to rename images as img_001.jpg, img_002.jpg, etc.:

i=1; for file in *.jpg; do mv "$file" "img_$(printf "%03d" $i).jpg"; i=$((i+1)); done

The printf "%03d" pads the number to three digits. Adjust the padding as needed.

Renaming Files With The Mmv Command

Another tool is mmv (mass move). It uses wildcards to rename multiple files. For instance, to rename file1.txt, file2.txt to doc1.txt, doc2.txt:

mmv "file*.txt" "doc#1.txt"

The #1 captures the wildcard part. Install mmv if not present: sudo apt install mmv.

Renaming With Zsh Built-In Features

If you use Zsh, you have powerful renaming capabilities. The zmv function is part of Zsh. Enable it with:

autoload -U zmv

Then rename files with patterns. For example, to change .txt to .md:

zmv '(*).txt' '$1.md'

Zsh also supports glob qualifiers for advanced selection.

Renaming Files With Case Conversion

To convert filenames to lowercase, use rename or a loop. With rename:

rename 'y/A-Z/a-z/' *

With a loop:

for file in *; do mv "$file" "${file,,}"; done

The ${file,,} syntax converts to lowercase in Bash 4+. For uppercase, use ${file^^}.

Renaming Files With A Prefix Or Suffix

Adding a prefix is easy with rename:

rename 's/^/new_/' *

To add a suffix before the extension:

rename 's/\./_backup$&/' *.txt

This changes file.txt to file_backup.txt.

Renaming Files With A Counter

For numbered sequences, use a loop as shown earlier. You can also use rename with a Perl expression that increments a counter. But loops are simpler for most users.

Renaming Files With A Date Stamp

To add a date to multiple files, combine for and date:

for file in *.log; do mv "$file" "${file%.log}_$(date +%Y%m%d).log"; done

This appends the date before the extension.

Renaming Files With A Random String

For security or uniqueness, add random strings:

for file in *.txt; do mv "$file" "${file%.txt}_$(openssl rand -hex 4).txt"; done

The openssl rand -hex 4 generates an 8-character hex string.

Renaming Files With A Specific Pattern

Suppose you have files like photo-2025-01-01.jpg and want to remove the date. Use rename:

rename 's/-2025-\d{2}-\d{2}//' *.jpg

This removes the date pattern. Test with -n first.

Renaming Files With A GUI: Thunar

Thunar (XFCE file manager) has a built-in bulk rename tool. Select files, right-click, and choose “Rename.” You can insert numbers, remove text, or change case. It is simple and effective.

Renaming Files With A GUI: Dolphin

Dolphin (KDE) also supports batch renaming. Select files, press F2, and use the dialog. Options include search and replace, numbering, and date insertion.

Renaming Files With A GUI: Nemo

Nemo (Cinnamon) has similar features. Right-click and select “Rename” for batch operations. It supports basic patterns.

Renaming Files With A Script: Python

For complex logic, Python is a good choice. A simple script:

import os
for filename in os.listdir('.'):
    if filename.endswith('.txt'):
        newname = filename.replace('.txt', '.bak')
        os.rename(filename, newname)

Save as rename.py and run with python3 rename.py. Python handles Unicode and complex patterns well.

Renaming Files With A Script: Perl

Perl is the language behind the rename command. You can write standalone scripts for advanced tasks. For example:

perl -e 'for (<*.txt>) { $new = $_; $new =~ s/\.txt$/.bak/; rename $_, $new }'

This does the same as the rename command.

Renaming Files With A Script: Awk

Awk can process filenames but is less common. Use it for pattern-based renames in pipelines.

Renaming Files With A Script: Sed

Sed is useful for modifying filenames in a loop. For example:

for file in *.txt; do mv "$file" "$(echo $file | sed 's/\.txt$/.bak/')"; done

This is similar to the rename approach.

Renaming Files With A Script: Find And Xargs

For large numbers of files, xargs improves performance:

find . -name "*.txt" -print0 | xargs -0 -I {} mv {} {}.bak

The -print0 and -0 handle spaces safely.

Renaming Files With A Script: Parallel

For even faster execution, use parallel:

find . -name "*.txt" | parallel mv {} {}.bak

Install parallel if needed: sudo apt install parallel.

Renaming Files With