This post may contain affiliate links/ads and I may earn a small commission when you click on the links/ads at no additional cost to you. As an Amazon Affiliate, I earn from qualifying purchases. Techsphinx also participates in the StationX Affiliate program. You can read my full disclaimer here.
In this post, I will show you how to rename directories (folders) in Linux using the mv and rename command.
Renaming files and directories are basic tasks that you need to perform every now and then.
Renaming a single directory in Linux using a terminal is easy but renaming multiple directories can get a little complicated.
Note: mv and rename commands can be used to rename both files and directories. In my other post, I have shown how you can use them to rename files in Linux.
Renaming Directories in Linux using mv Command
The mv command is officially made for moving files and directories, but when it moves a file/directory from one location to another, you can give the file/directory a new name.
Using this little side-effect, you can rename any directory easily using the mv command.
Here’s the basic syntax of the mv command:
mv <source> <destination>
mv: Name of the command.
<source>: Name of the source file/files. Yes! The source can be single or multiple files or even directories (folders).
<destination>: The destination can be a single file or directory. If you specify multiple files as source, then the destination must be a directory so that all the source files are moved to the destination directory.
Also, if you specify a single directory as the source and the destination is an existing directory, then the source directory is moved into the destination directory.
So, to rename a directory, you need to specify a single directory as a source and make sure there is no existing directory with the new name you want to give to your source directory.
Let’s start with renaming a single directory using the mv command.
Renaming Single Directory in Linux using mv command
To rename dir1 to dir2, use the mv command followed by the source and destination directory names separated by a space.
mv dir1 dir2
The above command will rename the dir1 to dir2.
(If dir2 already exists, then dir1 will be moved into dir2.)
Also, you can use the full path of the directory you want to rename, if the directory is not in the present working directory.
mv /home/user/Desktop/dir1 /home/user/Desktop/dir2
Renaming Multiple Directories using mv Command
To rename multiple directories with mv you have to use in conjunction with a for loop or while loop.
Here’s an example of renaming directories with mv using the “for loop”.
for d in *; do if [ -d "$d" ]; then mv -- "$d" "${d}_temp"; fi done
for d in *; – Start a for loop.
do if [ -d “$d” ]; – Check if it’s a directory.
then mv — “$d” “${d}_temp”; – Use mv command to add “_temp” to all directory names.
fi done – End the for loop.
Renaming Directories in Linux using rename Command
The mv command is handy if you want to rename a single directory. If you want to rename multiple directories in Linux using terminal, then you’ll have to use a command that is made for this.
Rename command is used to rename multiple files and directories in Linux. Using this command requires a little knowledge of regular expressions.
First, let’s install the rename command. There are 2 versions of rename command, the one I am going to show you is the Perl version.
Install Rename command in Linux
To Install Rename on Ubuntu
sudo apt install rename
Install Rename on CentOS / RHEL / Fedora
sudo dnf install prename
(Its prename (Perl rename))
Install Rename on Arch Linux / Manjaro Linux
sudo pacman -Syu perl-rename
Now, the rename command is installed let’s have a look at its basic syntax.
rename <options> <perlexpr> <files>
rename: Name of the command.
options: The options you can use with rename command.
Perlexpr: Regular expression in Perl syntax.
Files: The files to rename.
If you want to know about Perl regular expressions, then you can check the perldoc.
Rename Command usage
Let’s see rename command in action through some examples.
Filter directories and rename
Rename command is used to rename both files and directories. If you don’t want to accidentally rename a file, it’s recommended to filter the directories and use the rename command.
ls -d *_images | rename 's/_images/_assets/'
The first part of the above command (Before |) will filter out all the directories that end with “_images” in the current location.
After that, the rename command will replace all the “_images” with “_assets”.
Print name of the directories to be renamed, without actually renaming them.
First, filter out all the directories that end with “_assets” and execute the rename command with “-n” option.
ls -d *_assets | rename -n 's/_assets/_images/'
output:
rahul@techsphinx:~/Desktop/myfolder$ ls -d *_assets | rename -n 's/_assets/_images/'
rename(temp_assets, temp_images)
rename(unused_assets, unused_images)
rename(used_assets, used_images)
This a good way to check beforehand that everything is going to work as expected.
If everything is ok, then run the same command again without the “-n” option.
Replace spaces with underscores and dashes.
You can also replace space in directory names with underscores or hyphens.
To replace space with underscores, filter out all the directories and run the rename command, like mentioned below:
ls -d */ | rename 'y/ /\_/'
You can run the below command for replacing space in directory names with hyphens.
ls -d */ | rename 'y/ /\-/'
Convert filenames to uppercase
Filter out directories and use rename command to change the directory name to uppercase.
ls -d */ | rename 'y/a-z/A-Z/'
Convert filenames to lowercase
To change the directory name to lowercase, just filter out all the directories and use rename command like this:
ls -d */ | rename 'y/A-Z/a-z/'
There are many other options you can use with rename command. Check the manual of rename for the same.
man rename
Conclusion
I have shown you how to use the mv and rename command to rename directories in Linux.
Renaming multiple directories will seem a little harder at first, but once you get the hang of it, you can do many complicated renaming tasks very easily and quickly.
I hope this tutorial helped you to learn something new, if you have any suggestions or feedback then feel free to leave a comment.
If you like this post, then follow Techsphinx on Facebook and Twitter for more reviews, tricks, tips and tutorials.
This article needs update or correction? Report the issue here so I can update it.