Linux, Web Hosting, and Everything Else in Between
Linux, Web Hosting, and Everything Else in Between

How to Rename a File in Linux

rename file in linux

In this short tutorial, we’ll show you how to rename a file in Linux. Most commonly these commands are used on cloud servers and work on most Unix-based systems, including CentOS and Ubuntu.

This is our fist tutorial in a series of quick and short Linux tutorials.

Rename files using the mv command

The most basic (and relatively easiest) way of renaming commands in Linux is by using the mv command. Here’s the syntax:

mv [options] oldfilename newfilename

And here’s a real-life example that you’ll probably need to use after you install WordPress:

mv wp-config-example.php wp-config.php

The commands above only work if the file is in the directory you’re currently in. If they are in a different directory, you can use:

sudo mv /path/to/file.old /path/to/file.new

When you’re renaming files, chances are that you may rename the file to an already existing filename in the same directory. In that case, you can use the “-i” option, which will ask you if you want to overwrite the file.

As an example, if you already have an “index.php” file in the directory, and you want to rename “new.php” to “index.php”, you can use:

mv -i new.php index.php

And confirm when it asks you if you want to overwrite “index.php”.



Batch files renaming using the rename command

If you want to rename more files at once (in batches) you can use the “rename” command. However, this is for advanced users only. You’ll need to use regular expressions.

Here’s a basic example of the rename command:

rename 's/\.htm$/.html/' *

This command will rename all .htm files to .html in your working directory. You can use the same command to rename .jpeg files to .jpg.

A more common example is renaming files’ extensions from all-uppercase to all-lowercase

rename 's/\.JPG$/.jpg/' *

And that’s it. Now you should know how to rename files on your Linux server.

Leave a comment

Your email address will not be published. Required fields are marked *