×
rename files in linux

There are two ways to rename the files and directories in Linux-based operating systems. You can be done either using a GUI file manager or using a command-line interface. Generally, we use the mv command to rename the files and directories. But, the mv command won’t support multiple files and directories at once. mv command renames only one file at a time. In this case, you can use mv command with other commands to rename the multiple files at a time.

In this tutorial, we will explain how to rename files and directories in Linux-based operating systems.

Rename files with mv Command

A simple way to rename the files and directories is with mv command. It can move the file and directory from one name to another name.

The basic syntax of mv command is shown below:

mv [option] file1.txt file2.txt

Where file1.txt is the name of the source file and file2.txt is the name of the destination file.

Or

mv [option] directory1 directory2

Where directory1 is the name of the source directory and directory2 is the name of the destination directory.

Let’s create a file named file1.txt and reneme it to file2.txt with mv command:

First, create a file named file1.txt:

touch file1.txt

Next, rename the file1.txt to file2.txt with mv command:

mv file1.txt file2.txt

Note : If you are login with normal user then mv command requires write permission for the folder containing the files

You can also use -v option to display the verbose output after running mv command.

For example, rename the file2.txt to file1.txt as shown below:

mv -v file2.txt file1.txt

You should see the following output:


‘file2.txt’ -> ‘file1.txt’

You can use the -i option with mv command that will prompt before overwriting a file. This is very useful when the destination file is already exists.

Let’s rename the file1.txt to file2.txt with mv command:

mv -i file1.txt file2.txt

You will be prompt before overwriting file as shown below:


mv: overwrite ‘file2.txt’? y

Type y and hit enter to overwrite the file.

If you specify a file as source and directory as destination them mv command will move the source file to the destination directory.

For example, move the file1.txt to the directory /opt/ as hown below:

mv file1.txt /opt/

You can also use the following command to list all the options available with mv command:

mv --help

You should see the following screen:

mv-help

Rename multiple files with mv command

mv command won’t rename multiple files at a time. You will need to use other commands like find, for loop and while loop with mv command to achieve this.

For example, rename all the files with .txt extension in the current directory to the .php extension with find command as shown below:

First, create multiple files with .txt extension as shown below:

touch file1.txt file2.txt file3.txt file4.txt file5.txt

Next, rename all the files from .txt to .php extension with the following command:

find . -depth -name "*.txt" -exec sh -c 'f="{}"; mv -- "$f" "${f%.txt}.php"' \;

In the above example, find command finds all the files with .txt extension in the current directory and use the mv command to rename the file with .php extension one by one using -exec switch.

You can also use “for loop” to rename all the files with .txt extension to the file with .php extension.

For example. run the following command to rename all the files from .txt to .php extension as shown below:

for file in *.txt; do mv -- "$file" "${file%.txt}.php" done

Above command will create a for loop for all the files with .txt extension, rename all the files one by one with .php extension then complete the loop with done command.

Rename files with rename Command

The rename command is very advanced than move command. You can use rename command to rename single and multiple files according to the regular expression perlexpr. The rename command comes preinstalled in most Unix-like operating systems. If it is not available by default, run the following command to install it on Ubuntu/Debian systems:

apt-get install rename -y

On Centos/RHEL/Fedora, run the following command:

yum install prename -y

The basic syntax of rename command is shown below:

rename [options] perlexpr [filenames]

Run the following command to display all the information of rename command:

man rename

You should see the following screen:

Now, let’s start with some example. First, create some files with the following command:

touch file1.txt file2.txt file3.txt file4.txt file5.txt

Next, rename all the files with .txt extension to .html:

rename 's/.txt/.html/' *.txt

You can use -v option with rename command to display names of files to be renamed along with their new names:

rename -v 's/.txt/.html/' *.txt

You should see the following screen:

You can use -f option with rename command to allow existing files to be over-written.

rename -f 's/.txt/.html/' *.txt

To convert all the files from lowercase to uppercase with the following command:

rename 'y/a-z/A-Z/' *

To convert all the files from uppercase lowercase with the following command:

rename 'y/A-Z/a-z/' *

Use -s with rename command to rename the files ignoring the symbolic links:

rename -s 's/.txt/.html/' *.txt

Use -n option with rename command shows the files that will be changed without touching them:

rename -n 's/.txt/.html/' *.txt

The above command wouldn’t actually rename the files but just print them in the console window.

Conclusion

In the above article, we learned in various ways you can rename files either single file or multiple files. I hope you have now enough knowledge of rename and mv command to rename files.