Article Summary
Renaming a file in Linux means using command-line tools like `mv` instead of a simple right-click. This article covers the `mv` and `cp` commands, wildcards for batch renaming, directory renaming, flags like `-i` and `-v`, and the installable `rename` command. You'll gain the confidence to rename a file in Linux quickly and safely.
The quickest way to rename a file in Linux is with the mv (move) command. Simply type mv currentname newname in your terminal, but that’s not all. We can use different parameters to help us and to protect ourselves. For bulk renaming operations involving multiple files, the rename command offers powerful pattern matching capabilities.
In this guide, you’ll learn how to rename single files and directories, batch rename multiple files, use helpful command flags to prevent mistakes, and explore both command-line and GUI options for file renaming in Linux.
How to rename a file in Linux with the mv command
The mv (move) command is the standard tool for renaming files in Linux. While its name suggests moving files between locations, it’s also the primary method for renaming files when you keep them in the same directory.
Basic mv command syntax
Basic mv command syntax: mv [current-filename] [new-filename]
The mv command takes two arguments: the source file (current name) and the destination (new name). Here’s a practical example:
$ mv august.png september.png
This renames “august.png” to “september.png” in a single step. If you specify a different directory in the destination, the file will be both moved and renamed.
Important note about file extensions: The terminal environment typically doesn’t protect you from changing file extensions the way most graphical file managers do. The extension is part of the filename, a label for the user, and can be changed at any time.
Be careful when typing your new filename: changing .png to .jpg won’t convert the image format, it will just change the label. If a file opens in the wrong program after renaming, check that you typed the extension correctly.
Renaming files with the interactive flag
One risk with the mv command is accidentally overwriting an existing file. If your destination filename already exists, Linux will overwrite it without warning.
To prevent this, use the -i (interactive) flag:
$ mv -i august.png september.png
If september.png already exists, you’ll see a prompt asking whether to overwrite it. Type “y” to confirm or “n” to cancel.
Alternatively, use the -n (no-clobber) flag to prevent overwrites entirely without any prompt:
$ mv -n august.png september.png
With -n, if the destination file exists, the command simply does nothing.
Using verbose mode to track changes
When you want to see exactly what the mv command is doing, add the -v (verbose) flag:
$ mv -v august.png september.png
This outputs a confirmation of the operation, showing you the source and destination filenames.
You can combine flags for maximum safety:
$ mv -iv august.png september.png
This shows you what’s happening and prompts you before any overwrites.
Get 70 hours of hands-on Linux training with this Udemy course: Mastering Linux: The Comprehensive Guide
How to rename a directory in Linux
Renaming a directory works exactly the same as renaming a file. In Linux, a directory is essentially treated as a special type of file, so the mv command handles both identically.
To rename a directory called “2026” to “2027”:
$ mv 2026 2027
That’s all there is to it. The same flags (-i, -n, -v) work for directories just as they do for files.
How to rename multiple files in Linux
The mv command works well for single files, but it has limitations when you need to rename many files at once. For bulk renaming with pattern matching, you’ll want to use the rename command.
Installing the rename command
The rename command may not be installed by default on your Linux distribution. Here’s how to install it:
- Ubuntu or Debian: $ sudo apt install rename
- Fedora or CentOS: $ sudo dnf install rename
- Arch Linux: $ sudo pacman -S perl-rename.
To check if rename is already installed, type which rename in your terminal. If it returns a path, you’re ready to go.
Using rename command with patterns
On many Linux systems, rename supports Perl regular expressions to transform filenames.
Here are common examples:
Change all .txt files to .md:
$ rename 's/.txt/.md/' *.txt
Add a prefix to all files:
$ rename 's/^/backup_/' *.png
Replace spaces with underscores:
$ rename 's/ /_/g' *
The basic syntax is rename ‘s/pattern/replacement/’ files. The s/ indicates a substitution, followed by what to find, what to replace it with, and which files to process.
Use the -n flag to preview changes without actually renaming:
$ rename -n 's/.txt/.md/' *.txt
This shows you what would happen, letting you verify before committing.
Renaming and moving files simultaneously
Since mv handles both renaming and moving, you can do both in one command. To rename august.png to september.png and move it into the 2026 directory:
$ mv august.png 2026/september.png
The directory path and filename are simply combined in the destination argument.
Learn Linux in 2 days with this Udemy course: Linux for Busy Professionals
Renaming files using a GUI file manager
If you prefer not to use the terminal, most Linux desktop environments include file managers with built-in rename functionality:
- GNOME Files (Nautilus): Right-click the file and select “Rename,” or select the file and press F2.
- Dolphin (KDE): Right-click and choose “Rename,” or press F2.
- Thunar (XFCE): Right-click for “Rename,” or use F2.
The process is similar to renaming files in Windows or macOS. Select your file, trigger the rename function, type the new name, and press Enter.
While GUI methods work well for occasional renames, the command line is usually faster for bulk operations or when working on remote servers without a graphical interface.
If you’re looking to build your Linux skills, Linux courses on Udemy can help you get comfortable with both approaches. You might also want to explore our guide toFinding the Best Linux Distro for your needs.
mv command vs rename command comparison
| Feature | Mv command | Rename command |
| Best for | Single files or simple renames | Bulk renaming with patterns |
| Pre-installed | Yes | Sometimes (may need installation) |
| Syntax complexity | Simple | Requires regex knowledge |
| Pattern matching | No | Yes |