One place for hosting & domains


      How to Rename Files on Linux

      Windows and macOS users often manage their files using the Graphical User Interface (GUI) file manager provided on their systems. Typically Windows File Explorer or macOS Finder, respectively. Linux systems also usually have a GUI file manager, such as Dolphin, Nautilus, or Thunar. However, when managing a remote server, you may not have access to the GUI. It can be incredibly frustrating trying to figure out command line file operations while typing at an SSH, LISH, or other command line prompt. Fortunately, the commands are straightforward once you understand them.

      This tutorial primarily discusses how to use the mv and rename commands to rename one or more files in a terminal session. Creating files and displaying your Linux file system using the touch and ls commands are also covered.

      Before You Begin

      For the purposes of this tutorial, a shared instance with 1 CPU and 1 GB of memory running Ubuntu 22.04 LTS works. Pick a region that is close to your location. Create a strong root password and save it for later. Should you ever forget your root password, you can create a new one on your settings page. Don’t bother creating a SSH key for the account unless you’re already familiar with RSA keys.

      1. If you have not already done so, create a Linode account and Compute Instance. See our
        Getting Started with Linode and
        Creating a Compute Instance guides.

      2. Follow our
        Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access.


      The steps in this guide require root privileges. Be sure to run the steps below as root. For more information on privileges, see our
      Users and Groups guide.
      1. When your new Linode is running, click on the Launch LISH Console button.

      2. Once the Weblish side of the display has stopped scrolling, click on the Glish side of the display.

      3. Log in as root with the password you chose in the previous step.

      Renaming Files Using mv

      Short for “move”, the mv command moves files from one directory to another, but it also renames single files.

      1. From the root directory, type:


        You should have no results, as there are no visible files in the root directory.

      2. However, there are hidden files, to reveal them, type:

        ls -a

        You should now see a handful of hidden files:

        . .. .bashrc .cache .profile .ssh
      3. Create an empty file:

        touch test.txt
      4. View the directory again:


        Your test file should now be listed:

      5. Rename the file:

        mv test.txt test1.txt
      6. View the directory again:


        Your test file should now be listed with a different filename:


      Rename File(s) Using the rename Command

      While the mv command
      can be used inside a shell loop to rename multiple files, that requires some advanced text substitution. Instead, you can use a different command, rename.

      If your Ubuntu Linode is brand new, it probably doesn’t have the rename command installed.

      1. First, update your package sources:

        apt update
      2. Now install rename:

        apt install rename
      3. Once installed, create a second file with touch:

        touch test2.txt
      4. List both of them:


        You should now see both files:

        test1.txt test2.txt


      rename uses a Perl expression to act on the file names. Run man rename for an explanation and several examples.

      1. As an example, let’s rename both text files to backup files:

        rename 's/txt/bak/' *.txt


        On CentOS Stream 9, use this command instead:

        rename .txt .bak *.txt
      2. Now list them:


        You should see the same files as before, but with .bak extensions:

        test1.bak test2.bak

      When you’re done with this exercise, exit the LISH shell. If you don’t need this Linode anymore, delete it from its settings in the … dropdown menu to avoid incurring future charges.


      Renaming a single file on a terminal in Ubuntu Linux is accomplished with the mv command. Renaming multiple files is accomplished with the rename command, which you have to install in a new instance of Ubuntu 22.04 LTS.

      Source link