Renaming a folder in Linux is possible with Graphical User Interface (GUI) file managers as well as with powerful command-line utilities such as mv, rename, find, and rsync. Be it a novice utilizing Ubuntu, Debian, CentOS, Fedora, or Kali Linux or an expert dealing with bulk renaming in the terminal, Linux has more than one way of renaming directories with ease.
In this article, we provides information on how to rename a folder in Linux through GUI, command-line procedures such as mv, rename, and rsync, and how to work with permissions with the use of sudo for root folders. Batch renaming methods, renaming folders that have spaces, and renaming folders within Git repositories through git mv are also explained.
How to Rename a Folder in Linux (Command & GUI Methods) Rename a Folder in Linux Using Graphical User Interface (GUI)If you prefer more visual approach to Rename a Folder in linux than most Linux distributions come with file managers. Here’s how to do it using file manager
Step 1: Open the File ManagerAt first open your file manager application. This could be Nautilus (GNOME Files), Dolphin (KDE), Thunar (XFCE), or any other file manager depending on your Linux distribution.
Step 2: Navigate to the FolderAfter that navigate to the directory that contains the folder you want to rename.
Step 3: Rename the FolderYou should see the folder name updated in the file manager
Click on rename option Use Command Line to Rename a Folder in LinuxThere are various commands to rename a folder or the directory in Linux, such as the commonly used mv
, the advanced rename
command for batch renaming, and mv
with sudo
for folders requiring elevated permissions.
mv
(move) command : It is the simplest way to rename directories in Linux. It also allows to move files and directories within the file system
Follow these steps to rename a folder using the command line:
Step 1 : Before rename a folder in linux firstly we need to navigate to the directory that contains the folder you want to rename. Use the ` cd `(change directory) command to do this
cd ~/Documents # It changes your directory in linux distribution
Step 2 : After navigating we need to rename a folder using the mv
Command. Use the following syntax to rename a folder:
mv old_folder_name new_folder_name
Example:
mv commandHere:
a)Rename a Folder in a Different Directory
Provide the the full or relative path to the folder.
Syntax:
mv /home/user/Documents/project_old /home/user/Documents/project_newRenames the folder project_old to project_new
b)Rename a Folder with Spaces in its Name
You need to use quotes or escape spaces with a backslash (\
), if the folder name contains spaces or special characters, :
Using quotes:
Syntax:
mv "my folder" "new folder"
Using backslashes:
mv my\ folder new\ folderStep 3: Verify the Folder Name Change
To confirm the folder has been renamed, list the directory contents:
ls # This will display the updated folder name.2. Batch Rename a Folder in Linux Using the
rename
Command
`rename` command: It is used to rename multiple folders at once using the regular expressions. This method is particularly useful for batch renaming.
Syntax:
rename 's/old_pattern/new_pattern/' folder_nameRename files or folders in Linux by replacing a specified pattern in their names
For example: If you want to rename all folders or directories that start with "old_" to "new_", use:
rename 's/^old_/new_/' old_*
^
symbol ensures that match occurs at the beginning of the folder name.rename
Command:
If rename
is not installed in your linux system than, you can install it based on your Linux distribution:
sudo apt install rename #For Debian/Ubuntusudo yum install rename # For CentOS/RHELInstalling the rename Why Use
rename
Instead of mv
?
If we use the sudo command with mv than we allow the root level modification so that we can't face the permission errors
Syntax:
sudo mv old_folder_name new_folder_nameescalated privileges
Example:
if we rename a system directory (e.g., renaming /var/www/old_site
to /var/www/new_site
), use:
sudo mv /var/www/old_site /var/www/new_site4. Advanced Folder Rename in Linux Using
rsync
(Copy & Delete Method)
rsync: If we do the renaming process and if some especial complex scenarios comes like symbolic links or folder contents than we can use the `rsync` to copy the folder to a new name and then delete the old one.
Syntax:
rsync -a old_folder_name/ new_folder_name/ # Copies the contents ofShowing the processold_folder_name
tonew_folder_name
rm -r old_folder_name # Deletes the originalold_folder_name
Why UseNote: The `rsync -a` command copies the folder and its contents to the new folder . Once confirmed, you can delete the old folder with `rm -r`.
rsync
for Renaming?
find
and rename
If you need to rename directories recursively (i.e., rename folders inside subdirectories), you can combine the find
and rename
commands. This method is particularly useful for batch renaming folders in deeply nested structures, making it a powerful technique for Linux directory management.
Syntax:
find /path/to/parent_directory -type d -name "old_pattern" -exec rename 's/old_pattern/new_pattern/'{} \;
Example:
If you want to rename all directories named "old_folder" to "new_folder" inside a parent directory and its subdirectories, run:
Searches for files and directoriesThis will search for all directories (-type d) named old_folder and rename them to new_folder recursively.
NOTE : Ensure the rename command is installed on your system. You can install it using:
sudo apt install rename6. Rename a Folder in Linux Using `
mv`
with Wildcards
You can use wildcards with the mv
command to rename multiple folders at once. This method is particularly useful when you need to update folder names with a common prefix or suffix.
For example, if you want to rename project1
, project2
, and project3
to 2025_project1
, 2025_project2
, and 2025_project3
, use the following command in your terminal:
Syntax:
for dir in project*; doThis command loops through all folders that start with project and renames them by adding the prefix 2025_ Why Use Wildcards for Renaming Folders in Linux?
mv "$dir" "2025_$dir"
done
rename
with Perl Expressions
The`rename`command can also be used with Perl expressions for more complex renaming tasks. This is particularly useful for advanced users who want to perform sophisticated renaming operations. This is particularly useful for bulk renaming tasks, such as changing letter cases, replacing patterns, or modifying multiple directory names efficiently.
To rename folders by changing their case, you can use:
NOTE : Ensure the rename command is installed on your system. You can install it using:
sudo apt install rename
Syntax:
rename 'y/a-z/A-Z/' old_folder*
Example:
Converts all lowercase letters in folder names matching old_folder* to uppercase.This command will convert the names of all folders starting with old_folder from lowercase to uppercase.
8. Renaming Folders Usingfind
and xargs
For more complex renaming scenarios, you can use `xargs` to process multiple folders.
Using `find` with `xargs`allows you to efficiently perform operations on multiple files or directories that match specific criteria. For example, to rename directories starting with old_folder to new_folder, you can use:
Syntax :
find . -type d -name "old_folder*" | xargs -I {} mv {} new_folder{}
Example : If you have multiple directories named old_folder1
, old_folder2
, and old_folder3
, and you want to rename them to new_folder1
, new_folder2
, and new_folder3
, the above command automates the process efficiently
The `git mv ` command is used to rename files or directories within a Git repository while preserving version history. When you rename a directory using `git mv `, Git automatically stages the change for the next commit.
For example, to rename a directory from old_folder to new_folder, you would use:
Syntax :
git mv old_folder new_folder
This makes it simpler to handle changes over time by guaranteeing that the rename is recorded in the repository.
Why Usegit mv
?
In Linux, renaming directories with spaces in their names is difficult as spaces serve as command separators. If not done properly, one may face errors or unusual results. Below is a detailed process on how to rename directories with spaces within them by using the mv command alongside possible solutions for permission problems:
Step 1: Use Quotes or Escape the SpacesWhen dealing with folders that have spaces within their names, you can either:
\
).For example:
Using quotes:
mv "Old Folder Name" "New Folder Name"
Escaping spaces:
mv Old\ Folder\ Name New\ Folder\ NameStep 2: Verify the Folder Names
Next, you may type ls command in order to confirm the exact name of the folder you want to rename. Bear in mind that if the name consists of special characters or spaces, you have to type it exactly as it appeared on the output of ls.
Example:
ls -l # This helps avoid typos and ensures you're renaming the right folder.Step 3: Rename the Folder
After this, you can rename the folder by writing the below command:
mv "Old Folder Name" "New Folder Name"Step 4: Handle Permission Issues (If Required)
In the case where ‘Permission Denied’ error pops up, try the below command with sudo so the command is run with higher privileges:
sudo mv "Old Folder Name" "New Folder Name"
These steps ensure that folders with spaces in their names are renamed accurately without errors.
Also Read:There are several methods to rename a folder in Linux and it is a crucial task that has to be understood. Every Linux user needs to enhance their skills by learning how to use the GUI components or command line tools like mv, rename, find, rsync, or git. It is possible to perform folder renaming tasks using either the command line or GUI and both methods have their own advantages.
Basic renaming can be accomplished with mv command, which is perhaps the most popular method. When batch renaming folders that contain spaces, root folders, or other folders within folders, tools such as rename, find, xargs, and rsync are much better suited for the task than mv.
These techniques allow users to rename directories swiftly and improve the organization of the file system, streamline version control on Git repositories, and so much more. One can easily carry out smooth Linux directory management on different distributions of Linux such as Debian, Ubuntu, Red Hat, Arch Linux, etc., by knowing these techniques.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4