How to Rename Files or Directories in Linux

One of the most fundamental tasks you’ll perform regularly is renaming files as a Linux user. Fortunately, Linux provides methods for renaming files both from the desktop environment, or GUI, and the command line.

Note: We’re using Zorin OS 16, an Ubuntu-based distro, for this guide. All Linux commands featured in this article are distro-independent, meaning they will work on any Linux distro, except where otherwise noted.

Renaming Files and Directories from the Linux GUI

Renaming files via the GUI is straightforward in Linux.

  1. Navigate to the folder containing the file or directory you wish to rename on your distro’s file manager.

    Initial folder to rename a file.
    Initial folder to rename a file.
  2. Select the file you wish to rename. Next, right-click your mouse button and select the Rename option from the menu.

    Rename orange.txt to purple.txt.
    Rename orange.txt to purple.txt.
  3. Enter the desired new filename, and click Rename. In this example, we’re renaming the file orange.txt to purple.txt. (Take care not to rename the file extension if that’s not your intention.)

    Enter desired filename and click rename.
    Enter the desired filename and click Rename.
  4. You can see the file orange.txt has been renamed to purple.txt.

    The directory after the file has been renamed.
    The directory after the file has been renamed.

Renaming a folder or directory via the GUI works the same way as renaming a file.

  1. Navigate to the folder containing the directory you wish to rename on your Linux distro’s file manager.

    Initial folder to rename a directory..
    Initial folder to rename a directory.
  2. Select the directory you wish to rename. Next, right-click your mouse button and select the Rename option from the menu.

    Rename directory sample_directory to Sample-Directory.
    Rename directory sample_directory to Sample-Directory.
  3. When presented with the option to rename your directory, enter the desired new directory, and click Rename. In this example, we’re renaming the directory sample_directory to Sample-Directory.

    Enter the desired directory name and click Rename.
    Enter the desired directory name and click Rename.
  4. You can see your directory has been renamed.

    The folder after the file has been renamed.
    The folder after the directory has been renamed.

Renaming files and directories using the mv command

The mv command is Linux’s default command to rename files and directories.

mv Command Syntax

# mv [options] <current-filename> <new filename>

The mv command is used to move or rename a file.

Renaming a file with the mv Command

Open the terminal. Change to the directory containing the file you wish to rename.

# cd techobservatory.com

Use the mv command to rename the file you want to your desired name. In this example, we’re renaming the file orange.txt to purple.txt.

# mv orange.txt purple.txt

List the directory listing to ensure the file has been renamed.

# ls -al
Using the mv command to rename a file.
Using the mv command to rename a file.

You can also use the -v or verbose option with the mv command to explain what is being done when renaming the file. In this example, we’re renaming the purple.txt file back to orange.txt.

# mv -v purple.txt orange.txt

 

Using the mv command with the -v option to rename a file.
Use the mv command with the -v option to rename a file.

Renaming a Directory with the mv Command

Open the terminal.

Change to the directory containing the directory you wish to rename

# cd techobservatory.com

Use the mv command to rename the directory you want to your desired directory name. In this example, we’re renaming the directory sample_directory Sample-Directory.

# mv sample_directory Sample-Directory

List the directory to ensure the directory has been renamed.

# ls -al
Using the mv command to rename a directory.
Using the mv command to rename a directory.

As the name suggests, the mv command can also move files and directories.

Renaming files and directories using the rename command

One of the significant drawbacks of the mv command to rename files is that, without scripting knowledge, a user can only rename one file at a time. To help combat this problem, Larry Wall, creator of the PERL programming language, created rename, a command-line utility.

Installing the rename utility

Most Linux distros do not come with the rename utility, so it must be installed manually.

To install the rename utility on Debian, Linux Mint, and Ubuntu-based Linux distros, run the following command:

# sudo apt install rename

 

Install the rename utility on a Debian-based system with sudo apt install rename.
Install the rename utility on a Debian-based system with apt install rename.

To install the rename utility on RHEL-based distros like CentOS, Rocky Linux, and Fedora, run:

# sudo yum install rename

To install the rename utility on Arch-based distros:

# sudo pacman -S rename

rename Syntax

# rename [options] [PERLEXPRESSION] [files]

The rename command renames the filenames supplied according to the rule specified in the first argument ([options]). The [PERLEXPRESSION] argument is a Perl expression that is expected to modify the $_ string in Perl for at least some of the filenames specified. If the expression does not change a given filename, it will not be renamed.

Renaming Multiple Files with the rename command

  1. Open the terminal.
  2. Change to the directory containing the files you wish to rename.
    # cd techobservatory.com
  • Use the rename command to rename the files you want to rename. We’re renaming all files with a .txt extension to a .htm extension in this example.
    # rename 's/\.txt$/.htm/' *
  • List the directory to ensure all files with a .txt extension now have a .htm extension.
    # ls -al
Renaming multiple files with the rename utility.
Renaming multiple files with the rename utility.

The rename utility is a powerful utility that considerably eases many repetitive renaming tasks for Linux users, such as changing the case of multiple files or renaming file extensions, as in the above example. However, much damage can be done with the rename utility. Care should be taken when using it.

Commands Used in This Guide

  • ls – list directory structures.
  • mv – rename or move a file or directory.
  • rename – rename multiple files.

Was this article helpful?

Leave a Comment