How to Rename a File in Linux

Introduction Renaming files is a common task in Linux, whether you’re organizing your files or changing a filename for clarity. In Linux, the mv command is primarily used for renaming files via the terminal. This guide will walk you through the steps to rename a file in Linux using the…

Introduction

Renaming files is a common task in Linux, whether you’re organizing your files or changing a filename for clarity. In Linux, the mv command is primarily used for renaming files via the terminal. This guide will walk you through the steps to rename a file in Linux using the terminal, ensuring you can manage your files efficiently.

If you work with the Linux CLI then our Linux Fundamentals course is perfect for you! Check it out below:

Course: Linux Fundamentals

This course is a comprehensive course designed to guide learners through the vast landscape of Linux…

51 Lessons
4 Quizzes
2 Labs
5.5 Hr

In this guide, you’ll learn the following:

  • Open the terminal and navigate to the correct directory.
  • Use the mv command to rename files easily.
  • Handle filenames with spaces or special characters.
  • Rename multiple files efficiently using loops.

Step 1: Opening the Terminal

Renaming files in Linux can be done using both the terminal and a graphical user interface (GUI). If you prefer using a GUI, you can right-click on the file and select “Rename” from the context menu. However, for this guide, we will focus on renaming files using the terminal for better control and efficiency.

To rename a file using the terminal, you first need to open it. Here’s how to do it on different Linux distributions:

  • Ubuntu/Debian: Press Ctrl + Alt + T or search for “Terminal” in the applications menu.
  • Fedora: Press Ctrl + Alt + T or search for “Terminal” in the activities overview.
  • CentOS/RHEL: Press Ctrl + Alt + T or open the terminal from the system menu.

Once the terminal is open, you can proceed to rename your file using the mv command.

Step 2: Using the mv Command

The mv command in Linux is used for moving and renaming files. To rename a file, you need to specify the current filename and the new filename. The basic syntax is:

mv oldfilename newfilename

For example:

mv oldfile.txt newfile.txt

This code renames oldfile.txt to newfile.txt. Now, let’s look at a detailed example of renaming a file.

Step 3: In-Depth Example of Renaming a File in Linux

Let’s take an example where we have a file named oldfile.txt located in the /tmp directory, and we want to rename the file to newfile.txt.

First, navigate to the directory containing the file you want to rename using the cd command. For example, if your file is in the /tmp directory, use:

cd /tmp

Note: To confirm you are in the correct directory, use the pwd command to print the current directory.

Next, list the files in the directory to ensure your target file is there:

ls

You should see a list of files, including oldfile.txt.

Now, rename the file using the mv command. For example:

mv oldfile.txt newfile.txt

This command renames oldfile.txt to newfile.txt while keeping the file in the /tmp directory. Now if you execute the ls command you should see the new file name:

ls

This will list a series of files including the newly renamed newfile.txt.

Step 4: Renaming Files with Spaces or Special Characters

Renaming files with spaces or special characters in their names requires special handling to ensure the command is interpreted correctly by the terminal. Here’s how to manage such files:

  1. Navigate to the Directory:
    First, navigate to the directory containing the file. For example, if the file is in /tmp, use:
   cd /tmp
  1. List the Files:
    List the files in the directory to confirm the presence of the file with spaces or special characters:
   ls

You might see a file named old file.txt.

  1. Renaming with Spaces:
    If the filename contains spaces, enclose the filename in double quotes to ensure it is correctly interpreted:
   mv "old file.txt" "new file.txt"

Alternatively, you can use backslashes to escape the spaces:

   mv old\ file.txt new\ file.txt
  1. Renaming with Special Characters:
    If the filename contains special characters, enclose the filename in double quotes or use backslashes to escape the special characters. For example, to rename old$file.txt to new$file.txt:
   mv "old$file.txt" "new$file.txt"

Or:

   mv old\$file.txt new\$file.txt
  1. Verify the Change:
    List the files again to ensure the renaming was successful:
   ls

You should now see new file.txt or new$file.txt in the directory, confirming the renaming.

Step 5: Renaming Multiple Files

Renaming multiple files in Linux can be efficiently done using loops in the terminal. This method is especially useful when you need to rename files with a specific pattern or extension.

  1. Navigate to the Directory:
    First, navigate to the directory containing the files you want to rename. For example, if your files are in the /tmp directory, use:
   cd /tmp
  1. List the Files:
    List the files in the directory to see the current filenames:
   ls

You might see files like file1.txt, file2.txt, and file3.txt.

  1. Using a Loop to Rename Files:
    Use a for loop to rename multiple files. For example, to rename all .txt files to .bak:
   for f in *.txt; do mv "$f" "${f%.txt}.bak"; done

This loop goes through each .txt file, renaming it by replacing the .txt extension with .bak.

  1. Verify the Change:
    List the files again to ensure all the files have been renamed:
   ls

You should now see files like file1.bak, file2.bak, and file3.bak.

By using loops in the terminal, you can quickly rename multiple files, saving time and reducing the risk of errors. This method is powerful for batch renaming and managing large numbers of files efficiently.

Conclusion

Renaming files in Linux is a straightforward task that can be accomplished using the mv command. Now that you know how to do this you can manage and organize your files in Linux more effectively. Practice these commands to become more proficient in handling file operations in the Linux terminal.

For a deeper understanding of Linux and to enhance your skills, sign up for ServerAcademy’s Linux Fundamentals course. This comprehensive training covers essential Linux commands, file management, and much more. Visit our pricing page to learn more about our training programs and start your journey to becoming a Linux expert today!

CREATE YOUR FREE ACCOUNT & GET OUR

FREE IT LABS

profile avatar

Paul Hill

Paul Hill is the founder of ServerAcademy.com and IT instructor to over 500,000 students online!