- How do I rename a directory via the command line?
- 7 Answers 7
- How to Rename a Directory in Linux
- Renaming Directories With the mv Command
- Renaming Directories With the rename Command
- Renaming a Single Directory With the rename Command
- Renaming Multiple Directories With the rename Command
- Renaming Directories With the find Command
- Renaming Directories With Bash Scripts
- How to Rename a Directory in Linux
- How to Rename a Folder Through “mv” Command
- How to Rename a Folder Through “rename” Command
- Conclusion
- About the author
- Syeda Wardah Batool
How do I rename a directory via the command line?
I have got the directory /home/user/oldname and I want to rename it to /home/user/newname . How can I do this in a terminal?
7 Answers 7
mv /home/user/oldname /home/user/newname
This will not work if the new name is already an existing directory. Instead, it will move the old directory inside the new one.
If the directory name is the same with capitalization you will get No such file or directory . To avoid this do something like mv /home/user/Folder /home/user/temp; mv /home/user/temp/ /home/user/folder .
To just rename a file or directory type this in Terminal:
with space between the old and new names.
To move a file or directory type this in Terminal.
it will move the file to the desktop.
mv -T /home/user/oldname /home/user/newname
That will rename the directory if the destination doesn’t exist or if it exists but it’s empty. Otherwise it will give you an error.
mv /home/user/oldname /home/user/newname
One of two things will happen:
- If /home/user/newname doesn’t exist, it will rename /home/user/oldname to /home/user/newname
- If /home/user/newname exists, it will move /home/user/oldname into /home/user/newname , i.e. /home/user/newname/oldname
Doesn’t work if you want to capitalize the directory name in a case-insensitive filesystem (likely on MacOS). mv -T $PWD/analisys $PWD/Analisys returns mv: ‘/Users/sixtykeys/Projects/murphy/tmp/analisys’ and ‘/Users/sixtykeys/Projects/murphy/tmp/Analisys’ are the same file . I worked around this by using an intermediate name (i.e. analisys_ ).
The command may not have been successful due to the limitations of the filesystem, but from another perspective it was successful in interpreting your intentions (renaming a directory, not moving it) 🙂
How to Rename a Directory in Linux
Renaming a directory is one of the most basic tasks you will perform on any operating system. The Linux terminal offers several different ways to rename directories using commands and scripts.
In this tutorial, we will go over the different methods you can use to rename a directory in Linux through the terminal window.
- A system running a Linux distribution
- An account with sudo privileges
- Access to the terminal window/command line
- Access to a text editor, such as Vim or Nano
Renaming Directories With the mv Command
The primary function of the mv command in Linux is moving files and directories from one place to another. It uses the following command syntax:
mv [options] [source] [destination]
Note: To learn more about using the mv command, check out our guide to moving directories in Linux.
If the destination directory does not exist, the mv command renames the source directory instead. In this case, the syntax changes to:
mv [options] [current directory name] [new directory name]
As an example, let’s say we have Directory1, Directory2, and Directory3 in our Home directory:
To rename Directory1 into Example_Directory with the mv command, use:
mv Directory1 Example_Directory
There is no output if the command is successful, so we need to use the ls command to verify the name change:
Renaming Directories With the rename Command
The rename command in Linux is a dedicated command used to change the names of files and directories. Using this command makes it easier to rename multiple directories at the same time.
Note: The rename command is not included in all Linux distributions by default. If your system is missing the rename command, install it with:
- For Ubuntu and Debian, use sudo apt install rename
- For CentOS and Fedora, use sudo yum install prename
- For Arch Linux, use sudo pacman -S rename
Renaming a Single Directory With the rename Command
The rename command uses the following syntax:
rename [options] 's/[expression]/[replacement]/' [file name]
The command renames the file by replacing the first occurrence of the expression with the replacement. For example, if we want to rename Directory1 to Example_Directory:
rename 's/Directory1/Example_Directory/' *
In this example, we can see that the rename command syntax consists of several sections:
- rename : Invokes the rename command.
- s : Short for substitute, indicates that we are replacing the expression with the replacement.
- / Directory1 : Specifies the expression or the part of the old directory name that you want to replace.
- /Example_Directory /: Defines the replacement or the new directory name.
- * : Searches the Home directory for names matching the provided expression.
Verifying the Home directory content with the ls command shows that the directory now has a new name:
Renaming Multiple Directories With the rename Command
The rename command provides a way to rename multiple directories at the same time without using bash scripts. For instance, if we want to rename Directory1, Directory2, and Directory3 to Folder1, Folder2, and Folder3:
rename -v 's/Directory/Folder/' *
- -v : Invokes the verbose output, listing each step of the process.
- ‘s/Directory/Folder/’ : Replaces Directory in the names of the search results with Folder.
- * : Searches the Home directory for names matching the provided expression.
The rename command can also translate file names by using the y argument instead of the s argument. In this case, it translates one string of characters into another, character-for-character.
The command above translates every a character into d, every b into e, and every c into f.
In the example below, we translated blank spaces in directory names to underscores (_).
Renaming Directories With the find Command
In case you are not sure where the directory you want to rename is located, using the find command with the mv command lets you search for it and rename it when it’s found:
find . -depth -type d -name [current directory name] -execdir mv <> [new directory name] \;
In the example above, -execdir executes the mv command once the find command locates the directory.
For instance, the command below finds and renames Directory1 into Example_Directory:
find . -depth -type d -name Directory1 -execdir mv <> Example_Directory \;
Renaming Directories With Bash Scripts
Using bash scripts is another way of renaming multiple directories at the same time. Unlike the rename command, bash scripts allow you to save a template for future use.
Start by creating the script with a text editor, such as Nano:
sudo nano rename_directories.sh
The following example is a bash script that searches for directories and appends the current date to their name:
#!/bin/bash #Searches for directories and renames them according to the specified pattern for d in * do if [ -d "$d" ] then mv -- "$d" "_$(date +%Y%m%d)" fi done
- The first line instructs the script to go through all files and directories in the current location.
- Lines 2 and 3 check for directories.
- Lines 4 and 5 append the current date to the name of any directory found.
Press Ctrl+X, type Y, and press Enter to close and save the script.
As an example, let’s use the script above to change the names of Directory1, Directory2, and Directory3, located in the Example directory.
Start by moving in to the Example directory:
Learn more about Linux cd command in our guide on how to change and move to different categories.
Next, execute the script by using the sh command:
Using the ls command allows us to verify the name change:
After reading this article, you should know how to rename directories in Linux using commands and bash scripts.
Learn more about using Linux commands in our Linux Commands All Users Should Know .
How to Rename a Directory in Linux
In Linux and Unix-like systems, we are always amazed to see several ways for a single operation. Whether to install something or to perform through the command-line, you will get multiple utilities and commands.
Even if you want to move, copy or rename a directory, it is quite handy to perform these functions with commands; you don’t need to install any specific tool.
In Linux distributions, everything is in the form of directories. So, it is good to keep all of them in a structured way. Sometimes, we need to create temporary folders to save data and later on, to keep them permanently, we have to rename those directories.
There are no traditional commands to rename a folder/directory; it can be done using several ways. We will discuss in this guide how to change the directory name using the “mv” command and “rename” command. It might shock you that this operation can be performed using the “mv” command. The “mv” command is not only used to move one directory to another; it is a multi-purpose command which helps to rename a directory as well.
So, let’s check how we can do use the “mv” command and “rename” command:
How to Rename a Folder Through “mv” Command
To rename a folder through the “mv” command is the simplest way you have ever seen.
Create a directory in the terminal named “temp”:
To move the “temp” directory, make another directory with the name “temp 2”:
You can see in the home directory, two directories with the given names are created:
Now, move the “temp” to the “temp2” directory using the “mv” command:
Open the “temp 2” directory to check if the “temp” directory is successfully moved:
After moving, use the “mv” command again to rename a “temp2” directory:
So, the temp2 directory has been renamed successfully to the “new_Dir” directory:
You can also confirm it using a terminal to active a “new_Dir” directory in it, and check if the “temp” directory (we created first and moved to temp2 folder is present in the “new_Dir” directory or not):
To activate a “new_Dir” folder in the terminal, use the “cd” command:
Now, to display the list of files present in the “new_Dir” folder, type the “ls” command:
How to Rename a Folder Through “rename” Command
The “rename” command is a built-in tool in most Linux distributions that helps to rename folders and directories. It uses regular expressions to perform functions.
If it is not present in your system. Use the following command:
The syntax used for the “rename” command is:
Consider the given examples to check if it’s working:
Example 1:
To rename the directories from lowercase to uppercase, run the ls command to display directories in the desktop directory:
Use the rename command with the following expressions to change letter case:
To confirm it, type “ls” again:
Example 2:
To rename the text files present in the desktop directory as pdf files, run the command:
Type the “ls” command to display the output:
You can also rename a directory through GUI by simply right click on the desired folder and navigate to the “rename” option:
Click on the “rename” option, type the name you want to update, and click to the “rename” button:
And the directory name will be changed:
Conclusion
In this write-up, we have seen how to rename a directory in Linux operating system. There are multiple ways to do it, but why choose a difficult way when the simplest approach is available.
We have learned from this guide to rename directories using the “mv” command and “rename” command. The “mv” command is considered a multi-tasking command tool whereas, using the “rename” command directories can be changed through regular expressions. We have also checked it through the GUI approach.
About the author
Syeda Wardah Batool
I am a Software Engineer Graduate and Self Motivated Linux writer. I also love to read latest Linux books. Moreover, in my free time, i love to read books on Personal development.