How to Rename Files in Linux: Master the Command Line

Renaming files in Linux is a fundamental skill that every Linux user should have. Whether you are organizing your files, automating scripts, or managing servers, this is important knowledge to have.

In this tutorial, we will explore the mv command and other techniques to rename files using the Linux command line.

Rename a Single File in Linux with the mv Command

The command to rename a single file in Linux is the mv command. To rename a file using the mv command you specify mv followed by the original file name followed by the new file name.

Below you can see the basic syntax of the mv command:

mv <original_filename> <new_filename>

Let’s see how this command works by first creating a file called report.txt in the current directory using the Linux touch command:

$ touch report.txt

Now, imagine you want to rename the file to report_december.txt to make the file name more specific.

First of all, use the ls command to show the content of the current directory:

$ ls
report.txt

Then use the mv command to rename the file:

$ mv report.txt report_december.txt

Using the ls command again you can confirm that the file has been renamed successfully.

$ ls                               
report_december.txt

Note: Be cautious of overwriting existing files. Always verify file names before executing the mv command that by default will not ask you to confirm if an existing file should be overwritten.

Using the -i option with mv can prevent you from overwriting an existing file unless you confirm to go ahead. Below you can see an example of this scenario.

In the current directory, there are two files: report.txt and report_december.txt.

$ ls
report.txt		report_december.txt

The following mv command will ask you to confirm before overwriting the target file because you are passing the -i option to it.

$ mv -i report.txt report_december.txt
overwrite report_december.txt? (y/n [n]) n
not overwritten

This is good to know to prevent the loss of files you might need.

Rename a Single File in Linux Using Relative and Absolute Path

In the previous example, you have renamed the file from the directory where the report.txt file is.

Note: in Linux, the directory you are in is called current working directory.

Now, what changes in the syntax of the mv command if you are not in the same directory where the file you want to rename is?

You can use the absolute path of the file as part of the mv command. So if the report is in /opt/reports/ the command to rename the file changes from:

mv report.txt report_december.txt

To the following:

mv /opt/reports/report.txt /opt/reports/report_december.txt

The name of the new file can also include a completely different directory from the one where the original file is.

In that case, the mv command not only renames the file but also moves the file to the new target directory. Here is an example:

mv /opt/reports/report.txt /opt/updated_reports/report_december.txt

Here we are assuming that the directory /opt/updated_reports/ already exists.

How to Rename Multiple Files in Linux

You can rename multiple files in Linux using a for loop that executes the mv command multiple times, once for each file to be renamed.

Let’s start with the following directory which contains four files with extension .txt.

$ ls
report_april.txt	report_december.txt	report_january.txt	report_march.txt

To change the extension of all the .txt files in the current directory to .out you can use the following shell for loop.

$ for filename in *.txt; do
    mv "$filename" "${filename%.txt}.out"
done

Here is what the directory content looks like after executing it.

$ ls
report_april.out	report_december.out	report_january.out	report_march.out

You have successfully renamed all the files in the current directory!

Now, let’s explain every line of the for loop you have just executed:

for filename in *.txt; do

  • for filename in: This initiates a for loop, which is used to iterate over a list of files.
  • *.txt: This is a pattern that matches all files in the current directory that have the .txt extension. The loop executes its body once for each file that matches this pattern.
  • do: This keyword signals the beginning of the loop’s body, which executes the mv command.

mv "$filename" "${filename%.txt}.out"

  • mv: This is the move command used to rename the files in the current directory.
  • "$filename": This is the current .txt file being processed in the loop. The quotes around $filename are important to handle file names that contain spaces or other special characters.
  • "${filename%.txt}.out": This removes the .txt extension from the file name (stored in the variable filename) and appends .out instead. The % in ${filename%.txt} is a pattern removal operator, which removes the shortest match of .txt from the end of the value in filename.
  • The line as a whole renames each .txt file to have a .out extension instead.

done

  • This keyword indicates the end of the body of the for loop.

The syntax of this for loop might look complex if it’s the first time you are seeing it. In reality, it’s straightforward once you get used to it.

Conclusion

After going through this tutorial, you know:

  • How to use the Linux mv command to rename a single file.
  • How to use relative path and absolute path when renaming a file.
  • How to rename multiple files using a for loop and the mv command.

Leave a Comment