How to Rename Files in Linux Using the mv and rename Commands

A command-line terminal is an essential tool for administrating Linux systems, including a VPS. It lets users execute various Linux commands to simplify management tasks, like renaming files and folders.

However, executing commands may seem daunting for new VPS hosting users with minimal technical knowledge. To help beginners get started, we will explain how to rename files in Linux remote servers.

We will also explore how to use Hostinger VPS features to rename files in Linux efficiently. You will learn how to gain full root access to your server using the built-in Browser terminal and generate commands using our AI assistant, Kodee.

How to Rename Files in Linux With the mv Command

The mv or move command is used for two essential tasks in handling files in Terminal – moving files between locations and renaming them.

To run mv on Linux, connect to your VPS via SSH to access the command-line interface. You can use an SSH client like PuTTY or Terminal. For a local system, you can directly run the mv command via Terminal.

All Hostinger VPS hosting solutions support remote connection via SSH, allowing users to access their servers securely. Our hosting also provides full root support to ensure users can run various management commands without permission issues.

You will also need the VPS IP address and username to log in. For Hostinger users, navigate to the VPS menu on the top bar and select the relevant plan. Then, get the login credentials in the SSH Access tab.

Open Terminal and enter the following command. Remember to replace your-user with the username and your-server with the VPS IP address:

ssh your-user@your-server

Once connected, run the following command to learn more about how the mv command works:

mv --help

mv Command Syntax and Options

The basic mv command syntax is as follows:

mv option SOURCE...DIRECTORY

SOURCE refers to the file’s origin directory, while DESTINATION is its target path.

The option is an additional parameter for modifying the command’s output. Here are several popular mv options:

  • -f – shows no message before overwriting a file.
  • -i – displays warning messages before overwriting a file.
  • -u – only moves a file if it is new or doesn’t exist in the destination.
  • -v – explains what the command does.

Rename a File on Linux Using the mv Command

To rename a file, use the following mv command syntax. Note that this only works if you are in the same directory as the file:

mv oldnamefile1 newnamefile1

If there is a file called file1.txt which you want to rename to file2.txt, enter the following:

mv file1.txt file2.txt

If you are not in the file’s location, you must change the current working directory using the cd command. For example:

cd /home/user/docs/files
mv file1.txt file2.txt

Rename Multiple Files With the mv Command

By default, the mv command can only rename one file. To change multiple files’ names, combine mv with other commands. You must also use the find, for, or while loops to repeat the commands until the desired result is met.

To do so, create a new bash script file with the .sh extension using the touch command. Open it using a text editor like nano and write the loop. For example, enter the following to change all files in your current directory from the TXT extension to PDF:

for f in *txt; do
   mv -- "$f" "${f%.txt}.pdf"
done

This command creates a for loop to examine the list of TXT files in the directory. It will then replace each TXT extension with PDF. After all the files turn into PDF, the loop ends.

To run the bash script, enter the command below. Replace the file name and directory path according to your own:

bash /path/to/file/script.sh

If you encounter an error, ensure your current user account has the execute permission over the file. Otherwise, enter the chmod command to change the privilege:

sudo chmod +x /path/to/file/script.sh

How to Rename Files on Linux Using the rename Command

The rename command gives you more flexibility in modifying the files. Many Linux configurations include this command by default. If your system doesn’t have one, install it via Terminal.

Hostinger’s VPS hosting environment is optimized for users to use Linux commands for server management efficiently. In addition to offering various Linux distributions, we use powerful hardware to provide optimal performance, ensuring smooth command-line operations.

Moreover, we have data centers worldwide. Users can choose the closest one to their location, reducing input latency to improve responsiveness when managing a server remotely.

How to Install the rename Command

Depending on your server operating system, the installation command for rename differs. For Debian, Ubuntu, Linux Mint, and their derivatives, use the following command:

sudo apt install rename

Meanwhile, use this command if you are operating CentOS 9 or RHEL:

sudo yum install rename

For Arch Linux, run this command:

yay perl-rename ## or yaourt -S perl-rename

rename Command Syntax

Once installed, you can start using the rename command. Here’s its basic syntax:

rename 's/old-name/new-name/' files

For example, we will create a new filetorename folder with the mkdir command. Then, we’ll switch to the directory and use the touch command to create five files within the directory. Here are the commands:

mkdir filetorename
cd filetorename
touch file{1..5}.txt
ls

The ls command will list the current directory’s content, namely the five new files.

rename Command Examples

To rename a single file called file1.txt, the command will look like this:

rename ‘s/file1/newfile1/’ file1.txt

To change all files’ extensions, for example, to PHP, use the following commands:

rename ‘s/.txt/.php/’ *.txt
ls

If the file you want to rename is in another location, specify its directory in your command. Here’s what the command might look like:

rename ‘s/.txt/.php/’ FILE/PATH

Pro Tip

The rename command uses a regular expression of Perl, meaning it has extensive possibilities.

To list all the rename command options in Terminal, run the following:

rename –help

The rename command has various use cases. One of the most common uses is converting filenames to uppercase or lowercase. Here are their commands:

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

You can also replace a character with another using the command. For instance, this command will replace spaces in filenames with underscores:

rename 'y/ /_/' *

How to Remove Rename Command

If you want to remove the rename command from your system, uninstall it using Software Manager or Terminal. For this tutorial, we will use Terminal.

Use the following command for Debian, Ubuntu, Linux Mint, and their derivatives:

sudo apt remove rename

For CentOS and RHEL, run this command instead:

sudo yum remove rename

How to Rename Files With Hostinger VPS

Hostinger offers a variety of features that simplify file renaming. In addition to SSH support, our VPS hosting plans are compatible with various operating systems and offer a built-in Browser terminal.

The Browser Terminal location in hPanel's VPS management menu

Our control panel provides a graphical user interface (GUI) that enables users to rename files easily without commands, making it suitable for beginners. Hostinger users can install other control panels with one click via the Operating System menu in the VPS Dashboard.

Meanwhile, Hostinger’s Browser terminal lets you execute the mv or rename commands without Terminal or a third-party SSH client. In addition to convenience, this feature lets you access your remote server when the SSH port is closed due to security reasons.

The Browser terminal feature is available in all our VPS hosting plans and works with any operating system. To access it, open your web browser and follow these steps:

  1. Log in to the Hostinger Members Area and click VPS on the top menu.
  2. Select the VPS plan in question.
  3. Click Browser terminal on the right side of the VPS Dashboard. Your browser may return a pop-up window for permission.
  4. The command-line interface will open. Enter your root username and password to connect.

Once connected, execute bash commands to move or rename your Linux VPS files. We recommend leveraging our AI assistant, Kodee, to quickly generate commands by entering various AI prompts for VPS management. In addition to simplifying server management, this feature also saves you time and effort.

Conclusion

To rename files in Linux, connect to your remote server using Terminal or an SSH client. Then, use the mv or rename command to change the file name. Both work similarly, but rename provides more flexibility.

You can also use these commands to rename multiple files simultaneously. For the mv command, you must combine it with loops to repeat the renaming process for all files. Meanwhile, rename lets you do so in bulk by default.

Hostinger VPS offers various features to simplify renaming tasks. For instance, our Browser terminal lets you execute these commands without Terminal or an SSH client. Moreover, you can also install a control panel to easily manage files via a GUI.

In addition to robust features, Hostinger provides a high-performance VPS hosting environment to ensure responsive command-line operations. Moreover, our multiple data center locations help reduce latency when users connect to their servers remotely.

How to Rename Files In Linux FAQ

In this section, we will answer several common questions about renaming files in Linux using a command line.

What Linux Command Lets You Rename Files?

Use the mv command on Linux to rename files and folders. Since the Linux system understands renaming files as moving them to another location, you can use this command to change a file name.

How Do You Rename Multiple Files In Linux?

There are different ways to rename multiple files in Linux. It’s possible to batch rename using mmv or bulk-rename files with the rename utility. Moreover, you can use Emacs or Thunar file manager to execute the task.

Author
The author

Edward S.

Edward is a content editor with years of experience in IT writing, marketing, and Linux system administration. His goal is to encourage readers to establish an impactful online presence. He also really loves dogs, guitars, and everything related to space.