To rename a file in Linux is a primary administrative task. We can use the Command Line (CLI) and Graphical User Interface (GUI) to rename the files. In this article, the common methods to rename files in Linux will be discussed. Let’s start!
The content of this post is as under:
- How to Rename Files and Directories in Linux Using Terminal?
- How to Rename Files and Directories in Linux Using GUI?
Let’s start from the Terminal(CLI) method first!
Method 1: How to Rename Files and Directories in Linux Using Terminal?
We can change the name of a single file/directory and multiple files/directories at once using the command line.
Method 1.1: Using the mv Command
The “mv” command is an important utility for renaming files and directories in Linux. The general syntax of the command to rename a file/directory or directories is as follows:
$ mv [options] <source-file> <destination-file>
The source file will be replaced with the name of the original file, while the destination file is the newly renamed file.
Rename a File
To check the files in a folder, use the “ls” command and rename the “file1.txt” to “newfile1.txt” utilizing the below-mentioned command:
$ mv file1.txt newfile1.txt
Rename a Folder
You can also change the name of the folder using the “mv” command. For instance, to change a folder name from “MyFolder1” to “NewFolder1”, utilize the following command:
$ mv MyFolder1 NewFolder1
Method 1.2: Using the rename Command
The command-line method can be utilized to rename multiple files using the “rename” package. If the “rename” package is not installed, you can install it on various Linux distributions following one of the below-mentioned commands:
$ sudo apt install rename #to install in Ubuntu $ sudo pacman -S rename #to install in Arch Linux $ sudo yum install prename #to install in Fedora
The general syntax to replace the multiple files is given below:
$ rename [options] 's/[filename element]/[replacement]/' [filename]
The files to be renamed will be added after every forward slash “/” sign, and the name of the file which you want to use for renaming will be replaced with [filename] in the above command.
Rename Multiple Files
To change the name of files from “test1.txt, text2.txt,….” to “file1.txt,file2.txt,…..”, we can use the command mentioned below:
$ rename -v 's/test/file/' *.txt
Delete specific part of a file name like, to rename the file1.txt, file2.txt,.. to f1, f2,…, the expression “ile” is replaced with a space:
$ rename -v 's/ile//' *.txt
If you want to change the extension of the all the files from “txt” to a different extension “pdf”, use the following command:
$ rename -v 's/.txt/.pdf/' *.txt
The error-free output shows the successful change of the “.txt” file extension to “.pdf” extension.
Method 2: How to Rename Files and Directories in Linux Using GUI?
We can rename a single file or multiple files and directories in Linux using the Graphical User Interface (GUI) approach. Let’s see how it can be performed using
Rename a File or Directory
Renaming a file or directory/folder via GUI is quite simple. To rename a file or folder/directory, we will follow the below steps:
Step 1: Select the File
Select the files, and press the right side of the mouse to open the available options. Choose the “ Rename” option:
Note: After selecting the file, press the shortcut key “F2” to rename the file.
Step 2: Rename the File
After clicking the “Rename” option, a new window will open for renaming the selected file. Set your desired name and Click on “Rename” to save the new file name:
Step 3: Verify the Name
File is saved with the new name in the system.
Rename Multiple Files or Directories
Renaming a single file or folder in the previous step has the same method, similarly, changing the name for multiple files or directories/folders has the same procedure. The following steps will show you how to rename multiple files or directories:
Step 1: Select Multiple Files/Directories
Select the various files you want to rename and press the shortcut key “F2” to open the “Rename” options. Or Select the renaming files to open the “Rename” options:
Step 2: Rename the Files/Directories
A new “Rename” window will open up as shown below.
We can change the multiple Files/Directories names together in different ways that are described below.
Add any Change (text) at the End of the Directory Name
If we want to put something at the end of the name for original files (in my case “-test”), set it after “[Original file name]”. The changed name will appear in the right-side column as shown in the below figure. To save the changed name click “Rename”:
Add any Change (text) Before the Directory Name
If you want to add something before the names of files/directories, you can follow the below approach:
To rename the files with different sequence numbers at the end or at the start, click on the “Add” button. A new window with different sequence formats will open up as shown below:
We can add the sequence numbers at the start or end of the files/directories. To add the selected sequence at the start of the file/folder name will be as follows:
Replace the New Characters With Existing
To change the common words/characters in the name of multiple selected files/directories, we will go to the “Find and replace text” tab. Search the common characters in the “Existing Text” search bar, the related names will be highlighted, and enter the replaced characters/word in the “Replace with” search bar. The new name will show up in the next column. Click “Rename” to save the changes.
Step 3: Verify the Change
The files will save with the new names as shown below:
That’s the end of this guide.
To rename a file and a directory, use the “mv <source-file> <destination-file>” command or use “mv <source-folder> <destination-folder>” command. Moreover, the “Rename” package can be used to rename multiple files using “rename -v ‘s/filename/replacement]/’ [filename]” command. This post has demonstrated all the possible methods to change the name of the files and directories in Linux.