[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedJune 26, 2023

How to Rename Files and Directories.

How To Rename Files And Directories.

In this tutorial, we will show you how to use the mv and rename commands to rename files and directories.

 

Renaming Files with the mv Command

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

mv [OPTIONS] source destination

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 a source and a single file as a destination target.

For example, to rename the file file1.txt as file2.txt you would run:

mv file1.txt file2.txt

 

Renaming multiple files with the 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 .html extension to .php.

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

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 .html.

  • The second line applies to each item of the list and moves the file to a new one replacing .html with .php. The part ${file%.html} is using the shell parameter expansion to remove the .html part from the filename.

  • done indicates the end of the loop segment.

Here is an example using mv in combination with find to achieve the same as above:

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

The find command is passing all files ending with .html in the current directory to mv one by one using the -exec option. 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.

 

Renaming Files with the rename Command

The rename command is used to rename multiple files. This command is more advanced than mv as it requires some basic knowledge of regular expressions.

There are two versions of the rename command with different syntax. In this tutorial, we will be using the Perl version of the rename command. If you don’t have this version installed on your system, you can easily install it using the package manager of your distribution.

  • Install rename on Ubuntu and Debian

    sudo apt install rename
  • Install rename on CentOS and Fedora

    sudo yum install prename
  • Install rename on Arch Linux

    yay perl-rename ## or yaourt -S perl-rename

The syntax for the rename command is as follows:

rename [OPTIONS] perlexpr files

The rename command will rename the files according to the specified perlexpr regular expression.

The following example will change all files with the extension .html to .php:

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

You can use the -n option to print names of files to be renamed, without renaming them.

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

The output will look something like this:

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)

By default, the rename command doesn’t overwrite existing files. Pass the -f option to allow existing files to be over-written:

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

Below are a few more common examples of how to use the rename command:

  • Replace spaces in filenames with underscores

    rename 'y/ /\_/' \*
  • Convert filenames to lowercase

    rename 'y/A-Z/a-z/' \*
  • Convert filenames to uppercase

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

 

Conclusion

We’ve shown you how to use the mv and rename commands to rename files.

Keep reading
How to Set Up SSH Keys on Debian 10
September 6, 2023

How to Set Up SSH Keys on Debian 10

How To Set Up SSH Keys On Debian 10 This article describes how to generate SSH keys on Debian 10 systems. We will also show you how to set up an SSH key-based authentication and connect to remote Linux servers without entering a password. Creating SSH keys on Debian The chances are that you…

Read article
How to understand difference between LAN and WAN.
September 6, 2023

How to understand difference between LAN and WAN.

How To Understand Difference Between LAN And WAN. In the world of computer networking, two fundamental types of networks play a pivotal role: Local Area Networks (LANs) and Wide Area Networks (WANs). LANs are the networks you encounter on a daily basis in your home or office, while WANs are…

Read article
Networking Layer Structure: OSI vs. TCP/IP
September 6, 2023

Networking Layer Structure: OSI vs. TCP/IP

Networking Layer Structure: OSI Model Vs. TCP/IP Model Communication between networked devices is a fundamental aspect of today’s interconnected world. To understand and manage this complex process, engineers and network specialists use reference models that divide functions and protocols into…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00