How to rename files in Linux (Multiple and Safe Ways)

March 6, 2022
lgvps

One of the most fundamental tasks on a Linux system is to rename files. Renaming files can be done via a GUI file manager or the command-line terminal.

It is simple to rename a single file, but it cannot be easy how to rename multiple files at once in Linux, especially for new users of Linux.

This topic will teach you how to use the rename and mv commands to rename files and directories in Linux.

How to Rename a file in Linux

There are a few different ways to rename files in Linux, but these all involve using a command. Here are three methods. You can drag the file to Linux in the first method, but this only works if the first file already exists. If it doesn’t, you can rename it anyway – it will change its location and name. The second method requires typing a new path with the file’s name.

There are two basic ways to rename a file in Linux. You can use the “MV” command, but you may not have access to this built-in command. Alternatively, you can use the “rename” command to rename multiple files and directories at once. A rename command used in conjunction with the mv command will achieve a more complex renaming.

Renaming multiple directories can be challenging, especially for new Linux users. For this, the mv command is your best bet. Using the mv command will rename only one file at a time, or you can use it in a loop. You can use the mv command with other commands and save it as a template for future reference. To run the script, simply enter the name of the directory you want to rename, followed by the mv-exec option.

In the case of Linux, you can rename a directory by using the “mv” command. This command renames files and directories. It also moves them from one location to another. The mv command can be used to rename more than one directory at a time. You can also use the mv command with the “find” command to change the directory’s name filesystem-wide.

Renaming files using the mv command (with Example)

Renaming files using the mv command (with Example)

the mv command is used to move files from one place to another. The mv command syntax is as follows.

mv [OPTIONS] source destination

You can have multiple files or directories as the source, while the destination can only be one file or directory.

  • If multiple files are specified as a source, the destination must be a directory. The source files will be moved to the target directory.
  • You can specify one file as a source and an existing directory as a destination. The file will then be moved to the specified directory.
  • You can rename files by specifying a single file as a source file and one file as a destination file target.

To rename the lgvps1 file to lgvps2, you could run:

mv lgvps1.txt lgvps2.txt

How to rename multiple files with the mv command

The mv Command can only rename one file at once, but it can also be used with other commands like find Or inside the Bash for Or While Loops to rename multiple files.

This example shows you how to use Bash for loop to rename all .html Change the directory to include files .html Extension to .php.

for f in *.html; do
    mv -- "$f" "${f%.html}.php"
done

Let’s take a look at the code line-by-line:

  • The first line creates a for loop that iterates through all edging files with .html.
  • The second line applies each item in the list and moves it to a new file replacing .html by .php. To remove the .html from the filename, the ${file%.html} uses the shell parameter extension.
  • Done signifies the end of the loop segment.

This is an example of mv combined with find to achieve similar results as above:

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

The find command passes all files that end in .html using the -exec option. The string {} is the name of the file currently being processed..

As you can see, renaming multiple files with the mv command requires knowledge of Bash scripting.

Renaming files using rename command

To rename multiple files, use the rename command. This command is more complex because it requires basic knowledge of regular expressions.

There are two versions of rename commands with different syntaxes. This tutorial will use the Perl version rename command. This version can be installed easily using the package manager in your distribution if you don’t already have it.

sudo apt install rename
  • Install and rename using CentOS or Fedora
sudo yum install prename
  •  Install and rename to Arch Linux
yay perl-rename ## or yaourt -S perl-rename

The syntax of the Rename command can be found here:

rename [OPTIONS] perlexpr files

The command will rename files to conform to the specified Perlexpr regular expression.

This example will modify all files that have the Extension .html and .php.

rename 's/.html/.php/' \*.html

To print the names of files that need to be renamed without renaming them, you can use the -n option.

rename -n 's/.html/.php/' \*.html

This is the output: 

rename(file-90.html, file-90.php)
rename(file-91.html, file-91.php)
rename(file-92.html, file-92.php)
rename(file-93.html, file-93.php)
rename(file-94.html, file-94.php)

The rename command does not overwrite existing files by default. To allow files to be overwritten, pass the -f option:

rename -f 's/.html/.php/' \*.html

Here are several ways how the Rename command can be used: 

  • Replace the spaces in filenames with underscores
rename 'y/ /\_/' \*
  • Convert filenames into lowercase
rename 'y/A-Z/a-z/' \*
  • Convert filenames into uppercase
rename 'y/a-z/A-Z/' \*

How to Rename a Directory in Terminal

There are two basic ways to rename a directory in the terminal. You can use the “find” command, but you may not have access to this built-in command. Alternatively, you can use the “rename” command to rename multiple directories and files at once. A rename command used in conjunction with the mv command will achieve a more complex renaming.

Renaming multiple directories can be challenging, especially for new Linux users. For this, the mv command is your best bet. Using the mv command will rename only one file at a time, or you can use it in a loop. You can use the mv command with other commands and save it as a template for future reference. To run the script, simply enter the name of the directory you want to rename, followed by the mv-exec option.
Here is an example of rename directory command :

mv <source_directory> <target_directory>

Here is another example to rename a temp directory located in /home/user/

mv /home/user/temp /home/user/dir

As we can see, the temp directory has been successfully renamed into dir

File and directory rename | Conclusion |

In this tutorial, we learned how to use mv and name commands to rename files and directories.

Other commands can also be used to rename files in Linux. Linux beginners who find the command line intimidating can use GUI batch tools like Metamorphose.

If there is anything else that requires more clarification, please don’t hesitate to leave a comment!

The right decision!
If you're ready to make an impact in your business, it's time to get started now! Discover our offers and make the right decision!
Get Started Now!
payment gateway method for lgvps company
payment gateway method for lgvps company
Stripe paypal method for lgvps
A BeinCart LLC Company
Copyright © 2022 BeinCart LLC | Designed by LGVPS Team