Rename File in Terminal from Linux

Rename File in Terminal using Linux: Renaming files in Linux using the terminal might not be immediately obvious, but it's quite simple with the mv (move) command. This powerful command allows you to rename files, rename directories, and move files to different locations.

Whether you need to rename a single file, batch rename multiple files, or automate file renaming using scripts, this guide will show you the best ways to do it.

Rename Files in Linux Using mv Command

Rename files in Linux using the mv command

The mv command is primarily used to move files, but when used correctly, it also serves as a file renaming tool. Below are some common scenarios where you may need to rename files:

Why Rename Files in Linux?

  • Standardizing file names – Keeping files well-organized by following a consistent naming convention.
  • Fixing typos – Correcting mistakes in file names.
  • Changing file extensions – Converting files from one format to another.
  • Batch renaming – Using scripts or wildcard characters to rename multiple files.
  • Removing spaces or special characters – Ensuring compatibility with scripts and applications.
  • Updating version numbers – Keeping track of different versions of files.
  • Enhancing security – Renaming files to obscure their contents.
  • Automating renaming tasks – Using scripts to rename files systematically.

Basic Syntax of mv Command

The basic syntax of the mv command is:

mv [options] old_filename new_filename

For example, to rename a file called document.txt to new_document.txt:

mv document.txt new_document.txt

This command renames the file without changing its location.

How to Rename File in Terminal from Linux

Follow these steps to rename a file in Linux:

  1. Open the terminal (Ctrl+Alt+T for Ubuntu/Debian Linux).
  2. Navigate to the directory containing the file using the cd command:
cd /path/to/directory
  1. Rename the file using the following command:
mv oldfilename.txt newfilename.txt

This renames oldfilename.txt to newfilename.txt.

Rename a File While Moving it to a Different Directory

To rename and move a file at the same time, use:

mv filename.txt /new_directory/new_filename.txt

This moves filename.txt to /new_directory/ and renames it to new_filename.txt.

How to Rename a Folder in Linux

To rename a directory (folder) in Linux, use the following command:

mv old_folder new_folder

This changes the name of old_folder to new_folder.

Advanced File Renaming Techniques

Batch Rename Multiple Files Using mv and Shell Scripts

Renaming multiple files manually can be time-consuming. You can use a simple shell script to rename files in bulk.

For example, to rename all .txt files in a directory by adding a prefix new_, run:

for file in *.txt; do mv "$file" "new_$file"; done

This script loops through all .txt files and renames them.

Batch Rename Files Using rename Command

Linux also provides the rename command for bulk renaming:

rename 's/old/new/' *.txt

This renames all .txt files, replacing "old" with "new" in filenames.

Using find and mv for Complex Renaming

To rename all .jpg files in subdirectories and convert them to lowercase:

find . -type f -name "*.JPG" -exec rename 'y/A-Z/a-z/' {} \;

This command finds all .JPG files and renames them in lowercase.

mv Command Options

The mv command comes with several useful options:

  • -i : Prompts before overwriting an existing file.
  • -v : Displays each file being moved.
  • -u : Moves only if the source file is newer than the destination.
  • -f : Forces move, overwriting without confirmation.

Example with options:

mv -i oldfile.txt newfile.txt

This prompts before renaming if newfile.txt already exists.

Common Errors & Troubleshooting

Permission Denied

If you get a "Permission Denied" error, try running the command as sudo:

sudo mv oldfile.txt newfile.txt

File or Directory Not Found

Ensure you are in the correct directory by using:

ls

If the file is in another directory, specify the full path:

mv /home/user/oldfile.txt /home/user/newfile.txt

mv Command Not Found

If you see mv: command not found, ensure your system has core utilities installed:

sudo apt-get install coreutils   # Debian/Ubuntu
sudo yum install coreutils      # CentOS/RHEL

Conclusion

The mv command is a powerful tool for renaming files and directories in Linux. Whether renaming single files, batch renaming multiple files, or automating file renaming with scripts, Linux provides multiple ways to get the job done efficiently.

Need more Linux command tips? Check out Loop Commands in Shell or Batch Scripts to automate file renaming tasks!