How to Rename a File in Linux

Renaming files is a regular process in any operating system, including Linux. Whether you wish to give a file a more descriptive name, correct a typo, or arrange your files, Linux has several options for renaming files quickly and efficiently.

In this article, we will look at several strategies for rename a file in linux, like renaming single file, renaming multiple files, changing file extensions and more ranging from basic commands to complex capabilities.

Prerequisites

  • A system that runs a Linux distribution
  • A user with sudo privileges
  • Access to the command line/terminal window
  • Possession of a text editor such as Vim or Nano

Syntax for Renaming a file

The ‘mv‘ command (short for “move”) is a handy utility that not only moves files but can also rename them. The syntax for renaming a file is as follows:

mv [options] [source] [destination]

Behavior of the mv Command

Here is a table for the behavior of the mv command:

mv command in Linux

Rename Single File with ‘mv’ Command

To rename a single file in Linux, use the mv command with the following syntax:

mv [options] [current file name] [new file name]

To see if the file name has been successfully changed after using the mv command, use the ls command to list the files in the directory. Here’s a modified command sequence:

mv example1.txt example2.txt

Here the mv command changes the filename from example1.txt to example2.txt.

If the command is successful, no output will be displayed. You can use the ls -l command to list the files in the directory to confirm the name change:

cloudbooklet@cloudbooklet:~$ ls -l total 20 -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Desktop -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Document -rw-rw-r- 1 cloudbooklet cloudbooklet 0 Jun 10 05:20 example1.txt -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Music -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Pictures cloudbooklet@cloudbooklet:~$ mv example1.txt example2.txt cloudbooklet@cloudbooklet:~$ ls -l total 20 -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Desktop -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Document -rw-rw-r- 1 cloudbooklet cloudbooklet 0 Jun 10 05:20 example2.txt -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Music -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Pictures cloudbooklet@cloudbooklet:~$

See also  7 Best Free AI Chatbots for Chatting

After running this command, the output should show the modified file name example2.txt, indicating that the file was successfully renamed.

Rename Multiple Files with ‘mv’ Command

You can also use the mv command combined with other commands such as find command to rename several files at once. The find command allows you to choose numerous files based on certain criteria, and then rename them with the mv command using the -exec option.

Here’s an example command that uses find and mv to rename files:

find . -depth -name “[current file name element]” -exec sh -c ‘f=”{}”; mv – “$f” “${f%[current file name element]}[new file name element]”‘ ;

Let’s analyze the command:

  • find: Begin a search in the current directory and any subdirectories.
  • -depth: Enables depth-first search, which means that files are evaluated before parent folders.
  • -name “[current_file_name_element]”: Specifies a pattern or criterion for matching the files to be renamed.
  • -exec sh -c ‘…’;: Executes the following shell command on each file found.

Rename File Extensions

To convert the extension of numerous files, use the find command in conjunction with the mv command as we did above.

Here’s an example command to accomplish that:

find . -depth -name “*.txt” -exec sh -c ‘f=”{}”; mv – “$f” “${f%.txt}.pdf”‘ ;

This command will convert all files with the .txt extension in the current directory and its subdirectories to .pdf.

cloudbooklet@cloudbooklet:~$ ls -l total 20 -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Desktop -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Document -rw-rw-r- 1 cloudbooklet cloudbooklet 0 Jun 10 05:20 example1.txt -rw-rw-r- 1 cloudbooklet cloudbooklet 0 Jun 10 05:20 example2.txt -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Music -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Pictures cloudbooklet@cloudbooklet:~$ find . -depth -name “*.txt” -exec sh -c ‘f=”{}”; mv – “$f” “${f%.txt}.pdf”‘ ; cloudbooklet@cloudbooklet:~$ ls -l total 20 -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Desktop -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Document -rw-rw-r- 1 cloudbooklet cloudbooklet 0 Jun 10 05:20 example1.pdf -rw-rw-r- 1 cloudbooklet cloudbooklet 0 Jun 10 05:20 example2.pdf -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Music -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Pictures cloudbooklet@cloudbooklet:~$

Rename Multiple Files using for

We can also rename multiple files using the mv command inside a for loop in a bash script:

Here we will create a new bash file using nano editor.

nano rename_file.sh

Add the following lines to the file.

for f in *.txt; do mv – “$f” “${f%.txt}.pdf” done

The script begins with #!/bin/bash to identify Bash as the interpreter. The for function loops through files that match the pattern example*.txt. Then the mv command that is inside the for is used to rename each file by replacing the .txt extension with .pdf on each execution.

See also  How to Italicize in Character AI in 3 Simple Steps

Save the changes (Ctrl+X, then Enter in Nano) and exit the editor.

Run the following command to make the bash file executable:

sh rename_files.sh cloudbooklet@cloudbooklet:~$ ls -l total 20 -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Desktop -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Document -rw-rw-r- 1 cloudbooklet cloudbooklet 0 Jun 10 05:20 example1.txt -rw-rw-r- 1 cloudbooklet cloudbooklet 0 Jun 10 05:20 example2.txt -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Music -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Pictures -rw-rw-r- 1 cloudbooklet cloudbooklet 0 Jun 10 05:20 rename_files.sh cloudbooklet@cloudbooklet:~$ sh rename_files.sh cloudbooklet@cloudbooklet:~$ ls -l total 20 -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Desktop -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Document -rw-rw-r- 1 cloudbooklet cloudbooklet 0 Jun 10 05:20 example1.pdf -rw-rw-r- 1 cloudbooklet cloudbooklet 0 Jun 10 05:20 example2.pdf -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Music -rwxrwxr-x 1 cloudbooklet cloudbooklet 56 Jun 10 05:20 Pictures -rw-rw-r- 1 cloudbooklet cloudbooklet 0 Jun 10 05:20 rename_files.sh cloudbooklet@cloudbooklet:~$

Rename command Installation

The rename package is a powerful tool for renaming numerous files or directories at once. It has more capabilities and flexibility than the mv command, but it does require some fundamental Perl expression understanding.

Install Rename

Install the rename package if it isn’t already installed on your Linux distribution. The installation procedure may differ based on your package management.

Here are the commands for a few popular distributions:

For Ubuntu and Debian-based systems:

sudo apt install rename

For CentOS and Fedora systems:

sudo yum install rename

For Arch Linux systems:

sudo pacman -S rename

Syntax for Rename Command

Once installed, you can use the rename command to rename files or folders. The basic syntax is as follows:

rename [options] ‘s/[filename element]/[replacement]/’ [filename]

Renaming files and directories is done with the rename command. The syntax you supplied is for the s option, which allows you to replace the first occurrence of a given string in a filename with another string.

  • The options parameter is optional and can be used to modify the command’s execution. The -v option, for example, displays verbose output, and the -i option prompts before overwriting an existing file.
  • The filename element argument specifies the component of the filename to be replaced. A literal string or a wildcard character can be used. The wildcard *.txt, for example, would match all files with the .txt extension.
  • The replacement argument is the string with which the filename element should be replaced.
  • The file to rename is specified by the filename parameter.
See also  3 Easy Ways to Enable Two-Factor Authentication 2FA in Threads

Examples for Rename Command

Example 1: File extension

The filename element parameter is the component of the filename that you want to replace. In this case, you should substitute the .pdf extension for the .txt extension. As a result, the filename element argument is .txt, and the replacement argument is.pdf.

For example, the following command would convert the file extension of all files ending in .txt to .pdf:

rename -v ‘s/.txt/.pdf/’ *.txt cloudbooklet@cloudbooklet:~$ rename -v ‘s/.txt/.pdf/’ *.txt example.txt renamed as example.pdf cloudbooklet@cloudbooklet:~$

This command would replace the first occurrence of the string .txt in the filename of all files ending in .txt with the extension .pdf.

Now file extensions would be .pdf.

Example 2: Replacing a Filename

The syntax is the same as for changing the file extension, but the filename element argument is the component of the filename that you want to replace.

rename -v ‘s/example/test/’ *.txt cloudbooklet@cloudbooklet:~$ rename -v ‘s/example/test/’ *.txt example.txt renamed as test.txt cloudbooklet@cloudbooklet:~$

This command would replace the string test with the first occurrence of the string example in the filename of all files with the .txt extension. The filenames that would result would be test.txt.

Example 3: Delete a Filename

The rename command can be used to delete a part of a filename by omitting the replacement part of the expression.

The syntax is the same as for changing a portion of a filename, except the replacement parameter is not included.

rename -v ‘s/ple//’ *.txt cloudbooklet@cloudbooklet:~$ rename -v ‘s/ple//’ *.txt example.txt renamed as exam.txt cloudbooklet@cloudbooklet:~$

This command would remove the first occurrence of the string ple in the filename of all files ending in .txt.

Now the filename would be exam.txt

Example 4: Convert Lowercase & Uppercase Characters

Use the following command to change lowercase characters in filenames to uppercase characters:

rename -v ‘y/a-z/A-Z/’ *.txt

Use the following command to change uppercase characters in filenames to lowercase characters:

rename -v ‘y/A-Z/a-z/’ *.TXT # lowercase to uppercase cloudbooklet@cloudbooklet:~$ rename -v ‘y/a-z/A-Z/’ *.txt example.txt renamed as EXAMPLE.TXT # uppercase to lowercase cloudbooklet@cloudbooklet:~$ rename -v ‘y/A-Z/a-z/’ *.TXT EXAMPLE.TXT renamed as example.txt cloudbooklet@cloudbooklet:~$

This command would replace all lowercase characters in all filenames ending in .txt with uppercase characters and replace all uppercase characters in all filenames ending in .txt with lowercase characters.

Also read: You might also find useful our guide on How to Create and Use Bash Functions in Linux

Conclusion

In conclusion, renaming files in Linux is a simple task that can be completed with programs such as mv or rename. Understanding the syntax and arguments of these commands enables users to efficiently rename files and successfully manage their data effectively. Please feel free to share your thoughts and feedback in the comment section below.