How to Rename Files and Directories in Linux


In today’s blog, I’m going to show you how to a rename files. Within the Linux operating system you should be able to use these commands, because there are no real tricks with these particular commands other than it is a little bit weird with the renaming process.

So, an important thing to understand in the Linux world, at least at the command-line, if you were going to rename a file you use the move command instead. This may seem just a little odd. What you do is you use the move command and essentially you move the name of the file to be a different name of the file. Basically, if you just want to rename a file you do mv what the old filename is – what the new file name is, and that is the renaming process. That’s the only kind of weirdness in the Linux world.

To rename files and directories is one of the basic tasks you often need to perform on a Linux system. Renaming a single file is easy, but renaming multiple files at once can be a challenge, especially for entry level Linux users. You can rename files using a GUI file manager or using the command-line terminal.

In this scenario, I’ll show you how to use the mv command to rename files and directories.

How to rename files with mv Command

The mv command (short from move) is used to rename or move files from one location to another. The syntax for the mv command is as follows:

The source can be one or more files or directories and destination can be a single file or directory.

  • If you specify multiple files as source, the destination must be a directory. In this case, the source files are moved to the target directory.
  • If you specify a single file as source, and the destination target is an existing directory then the file is moved to the specified directory
  • To rename a file you need to specify a single file as source, and single file as destination target.

For example, to rename the file file-example1.txt as file-example12.txt you would run:

How to rename multiple files with mv Command

The mv command can rename only one file at a time but it can be used in conjunction with other commands such as find or inside bash for or while loops to rename multiple files.

The following example shows how to use the Bash for loop to rename all .html files in the current directory by changing the .txt extension to .old.

Let’s analyze the code line by line:

  • The first line creates a for loop and iterates through a list of all files edging with .txt.
  • The second line applies to each item of the list and moves the file to a new one replacing .txt with .old. The part ${file%.html} is using the shell parameter expansion to remove the .txt part from the filename
  • done indicates the end of the loop segment.

We can also use the mv command in combination with find to achieve the same as above.

The find command is passing all files ending with .txt in the current directory to the mv command one by one using the –exec switch. The string {} is the name of the file currently being processed.

As you can see from the examples above, renaming multiple files using the mv command is not an easy task as it requires a good knowledge of Bash scripting.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *