How to Rename a Directory in Linux Command Line
When you are new to something, even the simplest of the tasks could be confusing.
Take renaming a directory in the Linux command line. There is a rmdir command but it is for removing directories, not renaming them.
In Linux, you can use the same command that you use for renaming files for renaming directories also:
mv old_dir new_dir
Yes! That's the move command and while its original purpose was to move (or cut-paste) a file from one location to another, it can be used to rename a file and directory.
All the files inside the directory remain as it is. They are not impacted.
Let's take a deeper dive into this.
Rename directory using the mv command
When you rename an item, you erase the old item name and write a new name for it.
If you were to split the working of mv command, it acts in the following fashion (but obviously, this is done more efficiently). Assume that I want to rename 'old_file' file to 'new_file' file.
cp old_file new_file
rm old_file
This is what a move operation is at its core - for a better or worse analogy.
This means that if you don't change the directory of source and destination files, the only change will be in the filename.
Everything is a file in Linux. Even the directory is a special type of file that stores the index of files that are inside the folder.
This means that we can also rename a directory in the same fashion.
$ ls -l
total 4
drwxrwxr-x 2 team team 4096 Feb 16 15:34 old_dir
$ mv -v old_dir new_name
renamed 'old_dir' -> 'new_name'
$ ls -l
total 4
drwxrwxr-x 2 team team 4096 Feb 16 15:34 new_name
As you can see, the directory has been renamed from 'old_dir' to 'new_name'.
This will not negatively affect any files that are inside the directory. The only change which will occur is the rename operation on the directory name.
The rename operation is just a move operation and that is what we do. There is no 'rn' command in GNU coreutils. Though there is a rename command-line utility that is specifically used for batch renaming files but it doesn't come preinstalled on most distributions.