How to Rename a File or Directory in Linux
If you want to rename a file or directory in Linux, there is no specific rename command to do so, but there is the move command which can be used for both, moving a file/directory to a new location or to rename the file or directory.
Well moving a file is renaming the file in a way. How?
Let's take a simple example. Consider the following directory structure:
We have two directories with name dir1 and dir2 and then we have sub-directories in these files and inside one of the subdirectory subdir1 we have a file somefile.txt, a normal text file.
The complete path or the name of the file somefile.txt is dir1/subdir1/somefile.txt, right. Now, if I want to move this file to the directory subdir2, then the complete path or the fully qualified name for this file will become dir2/subdir2/somefile.txt.
Now, when we move a file from one location to another, in a way, its fully qualified name changes, or its path changes, which is also part of its name.
Another important point to note here is that in Linux files or directories are managed by their fully qualified names. So we can rename a file using the move command in Linux which can be used to update the fully qualified path of any file or directory, which is nothing but the name of the file.
Rename a File/Directory in Linux - mv
Command:
We will be using the mv
command to rename any file or directory in Linux. The mv
command is used to move a file or a directory to a different location, which is similar to renaming the file, which we just understood in the above paragraph.
The basic syntax of using the mv
command is:
mv [Option] <SOURCE_FILE> <DESTINATION_FILE>
In the command above, the source file is the fully qualified name of the file or directory you want to rename and the destination file is the new path with the changed name.
There are many options available which we can use while running the mv
command.
The mv
command in Linux is pretty simple to use. We can use this command for:
-
Renaming a single File or directory
-
Renaming multiple files or directories in one go.
So let's start with the easier one, which is renaming a single file or a single directory.
Rename single File/Directory
If you want to rename the file somefile.txt as described in the directory structure explained above, to have the new name as mynewfile.txt, we can run the following command:
mv somefile.txt mynewfile.txt
without any option and it should rename the directly. Now run the ls -l command to list down the files in your directory and you will see that your file is renamed.
Instead of the somefile.txt, we can also provide the fully qualified path of the file and instead of mynewfile.txt we can provide the updated path, and then use the mv
command, it will rename and move the file to the new path.
Similarly, you can rename directory too:
mv dir1 mydir
and the directory with name dir1 will be renamed to mydir.
Rename multiple Files/Directories
To rename multiple files in one go, we cannot use the mv
command directly. We will have to write a small bash script to do this.
Let's take an example, consider a directory with many files stored in it with different extensions. We want to change the extension of files with .txt extension and make it .log extension. How will we do it?
We can use the following bash script to do it:
for f in *.txt; do mv -- "$f" "${f%.txt}.log"
In the above bash script, we used a for
loop to iterate around all the files with .txt extension and then run the mv
command to rename them and replace the .txt extension with .log extension.
Similarly you can rename directories too.
Does Renaming Directory affects what is in the Directory?
When you rename a directory using the mv
command, the components of the directory remains as it is. The only change is that now they get a new updated path with the changed directory name.
After renaming a directory, use the ls -l
command to list down its files and subdirectories and you will so no changes there.
Summary
If you are new to Linux, do not get confused by the fact that we use the mv
command to rename the file. Because renaming a file and moving a file, is more or less the same.