• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero® Blog

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • AI Agents
    • OpenClaw
    • Claude Code
    • n8n
    • Hermes Agent
    • Open WebUI
    • Docker
  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
    • Email Hosting
    • HeroicGuard
    • GPU Hosting
    • Website Builder
  • VPS
    • Managed VPS
    • Unmanaged VPS
    • Flex VPS
  • Reseller
  • Gaming
  • Domains
  • Account
  • Blog Home
  • Categories
  • Authors

How to Rename a File in Linux

Bhagwad Park

Published on: September 13, 2023

Categories: Linux Command Line, VPS Hosting 0

Renaming a file in Linux is easy, once you know how. You can do it from either the command-line or using the GUI, though the latter is faster and uses fewer resources.

In this tutorial, I’ll show you how to rename not just one file, but multiple files at the same time. In addition, I’ll show you some alternative tools that you can use to rename Linux files to overcome some limitations of the default rename command, as well as to take advantage of some powerful features for advanced users. Let’s say you just bought one of our VPS packages and we are looking to get your hands dirty, this should be the perfect guide!

So let’s get started!

Table of Contents
  • How to Rename a File in Linux – Command Line
    • The Command for Renaming Directories is the Same
  • Common Flags for the “mv” Command while Renaming Files
    • -i or –interactive
    • -n or –no-clobber
    • -v or –verbose
  • Renaming Multiple Files in Linux
  • Using Alternative Command Line Tools to Rename Linux Files
    • Using “find” in Conjunction with “mv”
    • Using “rename” Instead of Find
  • Conclusion

How to Rename a File in Linux – Command Line

The basic syntax to rename a file in Linux is simple:

mv old_filename new_filename

In the screenshot below, I change the name of “testfile” to “newtestfile”:

Rename a File in Linux using the mv Command
Rename a File in Linux using the mv Command

And that’s it. And in case you’re wondering, yes – “mv” stands for “move” and this command is also used to move a file in Linux from one location to another with a different name. However, this doesn’t mean that the system is destroying the file and recreating it again with a new name. When the “mv” command is used to rename a file, the system is smart enough to realize that the source and destinations are the same, and it doesn’t waste resources. It simply changes the metadata to reflect the new name.

In fact, this behavior isn’t limited to just moving files within the same directory. Linux changes the metadata of the file to point to the new location even when using the “mv” command to move the file to a new location.

The Command for Renaming Directories is the Same

If you want to rename a director instead of a file on Linux, the command is the same. This is because, internally, Linux treats files and directories the same. A directory is simply a file containing the names of other files. So you can use the “mv” command in the same way to rename an entire folder.

Common Flags for the “mv” Command while Renaming Files

The “mv” command in Linux has a number of flags. While using “mv” to rename files, not all these flags are useful. However, here are the ones that are:

-i or –interactive

If you’re renaming a file, and an existing file already exists with the new name, then the “-i” flag will ask you for confirmation whenever there’s a conflict between the new filename and an existing one. If you press “y” during the prompt, Linux will overwrite the existing file.

Here’s an example:

mv -i old_name new_name

-n or –no-clobber

The “-n” flag ensures that Linux never overwrites existing files if they exist with the new filename. This is useful to ensure that you don’t accidentally mess up and delete an existing file. Here’s what the command looks like:

mv -n old_name new_name

-v or –verbose

The verbose command gives you detailed information about what’s happening, which files were renamed, and what the new filename is. You can use it like this:

mv -v old_name new_name

Here’s the output of the above command in verbose mode:

Using the mv Command in Verbose Mode
Using the mv Command in Verbose Mode

As you can see, “mv” is aware that you’re only trying to rename a file and it tells you as much. So if you want to ensure that your file renaming operation is being carried out according to your instructions, the verbose mode can help.

Renaming Multiple Files in Linux

You can use the “mv” command to rename multiple files at the same time using a loop. For example, let’s say you want to iterate through all the text files and append the word “new_” to each file with the “.txt” extension.

The following command will accomplish this:

for file in *.txt; do mv "$file" "new_$file"; done

The above command is a “for do” loop that identifies all files with the extension “*.txt”. It then uses the $file variable to craft successive “mv” commands that append the string “new_” to each file. Running the above command, I get this:

Renaming Multiple Files in Linux
Renaming Multiple Files in Linux

As you can see, if you have a pattern for the files that you want to target, you can rename all of them at the same time without needing to do it individually. This kind of power is typical of the Linux command line. If you wanted to achieve the same result using a GUI, it wouldn’t have been possible.

Using Alternative Command Line Tools to Rename Linux Files

While the “mv” command is quite powerful and, as seen above, we can use it in conjunction with “for” loops to target files that match a certain pattern, the operation is still limited in terms of what it can target. For example, it can’t target file size. If we need to quickly rename files above a certain size limit, then a simple for loop wouldn’t do the trick.

Using “find” in Conjunction with “mv”

The “find” command is able to target files with a much larger set of attributes. For example, let’s say I want to identify all files above a certain size limit – 1 MB for example – and rename them. The following code will do just that:

find . -type f -name "*.txt" -size +1M -exec mv "{}" "${file%.txt}_large.txt" \;

The above code will identify all files with a size greater than one MB, thanks to the “-size” flag, and then use the “exec” command to invoke “mv” and append the text “large” to the first portion of the file name.

The “find” command can also target other attributes that we can’t otherwise easily target using a regular “for” loop. For example, we can isolate the following types of files and folders:

  1. Files that have been modified recently
  2. Directories instead of files
  3. Files that have a specific set of permissions
  4. Files that have a specific type of ownership

As you can see, the range of flags you can use with “find”, makes it much more powerful compared to a vanilla “for” loop.

Using “rename” Instead of Find

If you want to use regular expressions to find and rename files, you can still use “find”, but the commands grow progressively more complicated. If you want to consistently use regular expressions in your renaming operations, I suggest you use the “rename” utility instead. There are several, but you can install the Perl rename utility on Ubuntu using the following command:

sudo apt update
sudo apt install rename

After installing rename, you can use regular expressions to match specific parts of a file and rename it. For example, let’s say you have some files like this:

  • file_01.txt
  • file_02.txt
  • file_03.txt

And you want to rename them to:

  • file1.txt
  • file2.txt
  • file3.txt

In other words, we want to get rid of the “_0” in the file names. To do this with the “rename” utility, we use:

rename 's/file_0/file/' file_*.txt

This simple command matches “file_0” and replaces it with “file” for all files exhibiting this pattern. Doing the same thing using “find” would be a lot more complicated!

Conclusion

Renaming files in Linux using the command line is easy. The process can be as simple as a single “mv” command, or it can get progressively more sophisticated based on your needs. If you need to use regular expressions, then the “rename” command is your best bet since it improves the clarity of the command, and you’re less likely to make mistakes.

Bhagwad Park Profile Picture
Bhagwad Park

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!

Related Posts

Nmap Scan Types: How to Scan a Server’s Network

The "nmap" command allows you to scan a remote server and determine its network configuration. Here are the various nmap scan types.

How to Use zsh Syntax Highlighting

In an earlier post, we discussed the basics of zsh, what it is, and how to get started. Today we’ll look at syntax highlighting in zsh, its meaning, and how to implement it. I’ll also explain why it doesn’t work well with bash, even though we can get it to work in a “hacky” way. […]

Bash Conditional Expressions: [[ vs [ vs test

In bash scripts, we use conditional statements frequently. Whether it’s in an “if fi” logic block or within a “while” or “do” loop, we evaluate statements all the time to determine whether they’re true or false, and perform various actions based on the result. True to form, there are multiple ways to do this, and […]

Jq Command: All you Need to Know

This jq command tutorial will walk you through the usage of "jq" - a command that works with JSON files like "awk" works with text.

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!


Most Popular Posts

NameHero’s Recommended WordPress Plugin and Theme Setup

WordPress Hosting vs. Web Hosting – What’s The Difference?

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • OpenClaw Hosting
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero® proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • Managed VPS Hosting
  • Unmanaged VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • GPU Hosting
  • Email Hosting
  • HeroicGuard
  • Domains
  • Website Builder
  • AI Agent Hosting
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2026 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA