Rename Directory Linux - 4 Easy Methods
3 min. read
Published on
Read our disclosure page to find out how can you help MSPoweruser sustain the editorial team Read more
Want to rename a directory on Linux? It’s crucial for maintaining an organized file structure.
In this guide, I’ll discuss 4 simple methods to achieve this. Let’s go!
How To Rename a Directory on Linux
To rename a directory on Linux, you can use:
1. mv Command
In Linux-based operating system, the mv command is primarily designed to move files and directories. However, you can also utilize it to rename directories.
1. Press CTRL+ALT+T to open the terminal.
2. Run cd to navigate to the parent directory of the one that you want to rename.
3. List the content of the current directory with ls.
4. Type “mv old_directory_name new_directory_name” to rename the desired directory. In my case, I’ll rename the “important_files” directory as “tests”.
5. For verification, again execute ls.
2. rename Command
rename command is a Linux utility that’s specifically designed for batch renaming of files and directories. Moreover, it supports regular expressions to perform complex renaming tasks across multiple files.
1. First, install rename with “sudo apt install rename“.
2. Then, type “rename ‘s/old_directory_name/new_directory_name/’ *”.
In the above command, “‘s/old_directory_name/new_directory_name/’ *” is a regular expression, where:
- s/ represents a substitution operation.
- old_directory_name indicates the original pattern or name that you want to replace.
- new_directory_name is the replacement pattern or new name.
- * is a wildcard character that refers to all files and directories in the current directory.
3. find Command
The find command can locate files based on the specified criteria. In addition, you can combine it with the mv command to rename a directory when you don’t know its exact location.
For this, type “find . -depth -type d -name old_directory_name -execdir mv {} new_directory_name \;” and hit Enter.
Here:
- find . searches starts the search from the current directory.
- -depth processes directory contents before the directory itself.
- -type d instructs find command to include only directories.
- -name old_directory_name searches for the specified directory.
- -execdir executes the mv command for each matched directory.
- {} is a placeholder for the current directory name, replaced with the actual name during execution.
- \; indicates the end of the -execdir option.
4. File Manager (GUI Method)
You can also try File Manager if you aren’t comfortable with the command-line interface:
1. Open the Activities menu and type “Files“.
2. Move to the directory location.
3. Right-click on the desired directory and select the “Rename” option from the context menu.
4. Type the new name and hit Enter.
5. Verify.
You may also be interested in:
So, now you know all essential rename directory Linux methods. Feel free to share your favorite approach in the comments below!
User forum
0 messages