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.
- Navigate to the folder containing the file or directory you wish to rename on your distro’s file manager.
- Select the file you wish to rename. Next, right-click your mouse button and select the Rename option from the menu.
- 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.)
- You can see the file orange.txt has been renamed to purple.txt.
Renaming a folder or directory via the GUI works the same way as renaming a file.
- Navigate to the folder containing the directory you wish to rename on your Linux distro’s file manager.
- Select the directory you wish to rename. Next, right-click your mouse button and select the Rename option from the menu.
- 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.
- You can see your 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
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
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
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
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
- Open the terminal.
- 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
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.