How to Rename a File or Directory in Linux



Linux


In this article, we show how to rename a directory in linux.

So in linux, on the shell, you can do many versatile things, including copying, deleting, moving, and renaming files or directories.

In linux, we can rename a directory with the mv command.

So how we use the mv command is we specify 2 parameters after the mv command separated by spaces.

The first parameter is the current name of the file or directory.

The second parameter is the name that we want to change the file or directory to (the new name).

So, for example, if we want to change a directory from its current name of db to dropbox, we use the following code shown below.



So the code is very simple.

With the following code, we are able to change the directory name from db to dropbox.

This is shown below.


Renaming a directory in linux


Let's write one more code.

Let's now rename a file, but instead of changing the name of the file, let's change the file extension, changing the type of file it is.

We are going to convert a text file (.txt) to an HTML file (.html).

This is shown in the following code below.



So now we've converted a plain text file to an HTML file.

So running this code, we get the following output shown below.


Renaming a file in linux


So you see the power of the mv command in linux. Not only can we rename a file or directory, but we can change the type of file it is by changing the file extension.

And this is how to rename a file or directory in linux.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...