How To Change File Name In Linux : Renaming Files With Mv Command

If you’re new to Linux, you might wonder how to change file name in linux without a graphical interface. This guide covers the simplest methods, from basic commands to renaming multiple files at once. You’ll learn practical steps that work on any Linux distribution.

Linux offers several ways to rename files, each suited for different situations. Whether you need to rename a single file or batch rename hundreds, the command line gives you full control. Let’s start with the most common method.

Using The Mv Command To Rename Files

The mv command is the primary tool for renaming files in Linux. It stands for “move,” but it also renames files when the source and destination are in the same directory.

Basic syntax: mv [options] source destination

To rename a file, type mv oldname.txt newname.txt and press Enter. The file now has the new name in the same location.

Here are key points about mv:

  • It does not preserve the original file name
  • It works on both files and directories
  • It can move files to different directories while renaming
  • It overwrites existing files without warning by default

Example: mv report.txt final_report.txt renames report.txt to final_report.txt.

Renaming With Full Paths

You can specify full paths to rename files in different directories. For instance, mv /home/user/docs/file.txt /home/user/docs/newname.txt renames file.txt to newname.txt in the same folder.

If you want to move and rename simultaneously: mv /home/user/docs/file.txt /home/user/backups/backup.txt

Using Interactive Mode

To avoid accidental overwrites, use the -i flag: mv -i oldname.txt newname.txt. Linux will ask for confirmation before overwriting an existing file.

Type y to confirm or n to cancel. This is especially useful when renaming files in shared directories.

How To Change File Name In Linux Using The Rename Command

For batch renaming, the rename command is more powerful. It uses Perl expressions to rename multiple files based on patterns.

Installation: On Debian/Ubuntu, run sudo apt install rename. On Red Hat/Fedora, use sudo dnf install prename.

Basic syntax: rename 's/oldpattern/newpattern/' files

Example: rename 's/\.txt$/.md/' *.txt changes all .txt files to .md extension.

Common use cases:

  • Change file extensions: rename 's/\.jpg$/.png/' *.jpg
  • Add prefixes: rename 's/^/backup_/' *.txt
  • Remove suffixes: rename 's/_old$//' *.txt
  • Convert case: rename 'y/a-z/A-Z/' *.txt

The rename command supports dry runs with -n flag: rename -n 's/\.txt$/.md/' *.txt shows what would happen without actually renaming.

Using Regular Expressions With Rename

Regular expressions give you fine control. For example, to rename files with numbers: rename 's/file_(\d+)/document_$1/' file_*.txt changes file_1.txt to document_1.txt.

You can also replace spaces with underscores: rename 's/ /_/g' *.txt

Renaming Files With Graphical File Managers

If you prefer a graphical interface, most Linux file managers support renaming. In Nautilus (GNOME), right-click a file and select “Rename.” Type the new name and press Enter.

In Dolphin (KDE), the process is similar. You can also use F2 key to rename selected files quickly.

For batch renaming in Nautilus:

  1. Select multiple files
  2. Right-click and choose “Rename”
  3. Use the batch rename dialog to add prefixes, suffixes, or replace text
  4. Preview changes before applying

Thunar (Xfce) offers a bulk rename tool under Edit > Rename Multiple Files. It supports numbering, case changes, and string replacement.

Using Midnight Commander

Midnight Commander (mc) is a text-based file manager. Install it with sudo apt install mc. Navigate to the file, press F6 to rename, type the new name, and press Enter.

This method works in terminal environments without a GUI.

Renaming Files With Bash Scripts

For complex renaming tasks, bash scripts offer flexibility. Here’s a simple script to add date stamps:

#!/bin/bash
for file in *.txt; do
    mv "$file" "$(date +%Y%m%d)_$file"
done

Save this as rename.sh, make it executable with chmod +x rename.sh, and run it with ./rename.sh.

Another example: Remove all spaces from filenames:

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

These scripts are useful for repetitive tasks and can be customized for your needs.

Using Find With Exec

The find command combined with exec lets you rename files matching specific criteria. For example, to rename all .jpg files older than 30 days:

find . -name "*.jpg" -mtime +30 -exec mv {} {}.old \;

This appends “.old” to each matching file. You can modify the pattern as needed.

Handling Special Characters In Filenames

Filenames with spaces, hyphens, or special characters require careful handling. Always quote filenames: mv "my file.txt" "my_new_file.txt"

To rename a file starting with a hyphen, use -- to stop option parsing: mv -- -file.txt file.txt

For filenames with newlines or other control characters, use find ... -print0 with xargs -0:

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

This handles any special characters safely.

Common Mistakes And How To Avoid Them

Beginners often make these errors when renaming files:

  • Forgetting to quote filenames with spaces
  • Using wildcards incorrectly (e.g., mv *.txt *.md doesn’t work)
  • Overwriting existing files without backup
  • Using rename without testing first

Always test with echo or rename -n before executing. Keep backups of important files before batch operations.

Recovering From Accidental Renames

If you accidentally rename a file, check your shell history with history | grep mv. You can reverse the operation if you remember the old name.

For batch mistakes, restore from backups or use version control systems like Git.

Renaming Directories

Directories are renamed the same way as files using mv. For example, mv oldfolder newfolder renames the directory.

Be careful: renaming directories can break paths in scripts and configuration files. Update any references after renaming.

To rename multiple directories, use the rename command with appropriate patterns: rename 's/^project_/old_project_/' project_*/

Using Tab Completion For Efficiency

Tab completion speeds up renaming. Type the first few letters of the file and press Tab to autocomplete. This reduces typos and saves time.

For example, type mv rep and press Tab to complete to mv report.txt. Then type the new name.

This works in most Linux terminals and is highly recommended for accuracy.

Permissions And Ownership Considerations

You need write permission on the directory containing the file to rename it. Use ls -l to check permissions.

If you lack permissions, use sudo with caution: sudo mv file.txt newfile.txt. Only use sudo when necessary, as it can cause security issues.

Renaming files does not change ownership or permissions. The file retains its original attributes.

Case Sensitivity In Linux Filenames

Linux filenames are case-sensitive. File.txt and file.txt are different files. Use consistent casing to avoid confusion.

To change case: mv file.txt File.txt renames the file. For batch case changes, use rename 'y/a-z/A-Z/' *.txt to uppercase all .txt files.

Using Wildcards For Pattern Matching

Wildcards simplify renaming multiple files. The asterisk * matches any characters, while ? matches a single character.

Example: mv file?.txt file_new?.txt renames file1.txt to file_new1.txt, file2.txt to file_new2.txt, etc.

Be careful with wildcards to avoid unintended matches. Test with echo first: echo mv file?.txt file_new?.txt

Renaming Files With Dates And Timestamps

Adding dates to filenames helps with organization. Use the date command: mv file.txt "$(date +%Y-%m-%d)_file.txt"

For batch operations, use a script that loops through files and appends dates.

Example script:

#!/bin/bash
for file in *.log; do
    mv "$file" "$(date +%Y%m%d)_$file"
done

This adds today’s date as a prefix to all .log files.

Renaming Files With Incremental Numbers

To add sequential numbers to filenames, use a counter in a script:

#!/bin/bash
counter=1
for file in *.jpg; do
    mv "$file" "image_$counter.jpg"
    ((counter++))
done

This renames image files to image_1.jpg, image_2.jpg, etc.

You can also use rename with Perl expressions for more complex numbering.

Using The Mmv Command For Mass Renaming

The mmv command (mass move) simplifies batch renaming. Install it with sudo apt install mmv.

Syntax: mmv '*.txt' '#1.md' renames all .txt files to .md, preserving the base name.

The #1 represents the part matched by the wildcard. You can use multiple wildcards: mmv 'file_*_*.txt' 'file_#2_#1.txt' swaps two parts of the filename.

Mmv supports dry runs with -n flag.

Renaming Files With Vim Or Emacs

For advanced users, Vim and Emacs offer file renaming capabilities. In Vim, use :!mv % newname.txt to rename the current file.

In Emacs, use M-x dired to open a directory, then use R to rename files interactively.

These methods are less common but useful for users already working in these editors.

Automating Renames With Cron Jobs

You can schedule automatic file renaming using cron. For example, to rename log files daily:

  1. Create a script with renaming commands
  2. Make it executable
  3. Add a cron job: crontab -e and add 0 0 * * * /path/to/script.sh

This runs the script at midnight every day. Adjust the schedule as needed.

Best Practices For File Renaming

Follow these guidelines to avoid problems:

  • Always backup important files before batch operations
  • Use descriptive but concise names
  • Avoid special characters that might cause issues in scripts
  • Test commands with dry runs before execution
  • Document your renaming patterns for future reference
  • Use version control for critical files

Consistent naming conventions improve file management and reduce errors.

Frequently Asked Questions

How Do I Rename A File In Linux Using The Command Line?

Use the mv command: mv oldname.txt newname.txt. This renames the file in the current directory. For other directories, specify full paths.

Can I Rename Multiple Files At Once In Linux?

Yes, use the rename command with Perl expressions, or write a bash script with a loop. The mmv command also handles mass renaming easily.

What Is The Difference Between Mv And Rename Commands?

mv renames single files or directories and is simpler. rename uses regular expressions for batch operations and offers more flexibility for complex patterns.

How Do I Rename A File With Spaces In Its Name?

Quote the filename: mv "my file.txt" "my_new_file.txt". You can also escape spaces with backslashes: mv my\ file.txt my_new_file.txt.

Is It Possible To Undo A File Rename In Linux?

There is no built-in undo. You must manually rename the file back. Check shell history with history | grep mv to find the original name. Always backup before batch operations.

Mastering how to change file name in linux gives you control over your filesystem. Start with simple mv commands, then explore batch renaming as your needs grow. Practice with test files to build confidence before handling important data.