Are you looking to rename files in Linux using the command line? If so, you’ve come to the right place.
The command line terminal in Linux is a vital tool for managing and controlling Linux systems, including Virtual Private Servers (VPS). It allows you to perform various tasks using simple Linux commands, such as renaming or deleting files and folders.
This might seem challenging for non-technical users, but we’ve created this guide to help you understand how to rename files in Linux using the command line.
So without further delay, let’s get started!
How to Rename Files in Linux Using the mv Command
The mv
(move) command in Linux is useful for two main things: moving files to different locations and renaming files.
How to Use the mv Command
1. Accessing the Command Line:
– To use the mv
command, you need to open the terminal.
– If you’re working on a server, you’ll need to connect via SSH. You can use tools like PuTTY (on Windows) or the built-in Terminal (on macOS or Linux).
– If you’re working on a local machine, simply open the Terminal.
2. Connecting to Your VPS:
– If you have a Virtual Private Server (VPS), you’ll need the server’s IP address and your login details.
– Use the command below to log in. Replace “your-user” with your username and “your-server” with the server’s IP address:
ssh your-user@your-server
Basic mv Command Syntax
The structure of the mv
command is:
1 |
mv [option] source destination |
– source: This is the current location of the file.
– destination: This is where you want the file to go (or the new file name).
– option: These are extra settings you can use. Some common options are:
– -f
: Force the command to overwrite files without asking.
– -i
: Ask for confirmation before overwriting.
– -v
: Show what’s happening as the command runs.
How to Rename a File
If you are in the same folder as the file you want to rename, you can use this simple command:
1 |
mv oldfilename newfilename |
For example, to rename file1.txt
to file2.txt
, type:
1 |
mv file1.txt file2.txt |
If you’re in a different folder, you must first move to the correct folder using the cd
command. For example:
1 2 3 |
cd /home/user/docs/files mv file1.txt file2.txt |
Renaming Multiple Files at Once
By default, the mv
command can only rename one file at a time. You’ll need to use a loop if you want to rename multiple files. Here’s how:
1. Create a new script file using the touch
command:
1 |
touch rename_files.sh |
2. Open it in a text editor like nano
and write a loop:
1 2 3 4 5 |
for f in *.txt; do mv -- "$f" "${f%.txt}.pdf" done |
This script will rename all .txt
files to .pdf
files in the current directory.
3. Run the script using:
1 |
bash rename_files.sh |
4. If you get a permission error, you can use this command to fix it:
1 |
sudo chmod +x rename_files.sh |
How to Rename Files on Linux Using the Rename Command
The rename
command is a handy tool that gives you more control when changing the names of files in Linux. Many Linux systems already have this command, but if you don’t have it, you can easily install it using the Terminal.
How to Install the Rename Command
To install the rename
command, you need to type a specific command in the Terminal, depending on your Linux version:
– If you are using Ubuntu, Debian, or Linux Mint, type:
1 |
sudo apt install rename |
– If you are using CentOS or RHEL, type:
1 |
sudo yum install rename |
– For Arch Linux, use:
1 |
yay perl-rename |
Once installed, you’re ready to use the rename
command!
Basic rename Command Syntax
Here’s how the command looks when you want to rename something:
1 |
rename 's/old-name/new-name/' files |
– old-name: The current name of the file.
– new-name: The new name you want to give the file.
– files: The file or files you want to rename.
Example of Renaming Files
Let’s say you have a file named file1.txt
and want to rename it to newfile1.txt
. You would type:
1 |
rename 's/file1/newfile1/' file1.txt |
If you want to rename multiple files at once, for example, changing all .txt
files to .php
, you can use this command:
1 |
rename 's/.txt/.php/' *.txt |
This will rename all .txt
files in the folder to .php
.
Renaming Files in a Different Location
If the file you want to rename is in a different folder, you need to specify its location in the command. For example:
1 |
rename 's/.txt/.php/' /path/to/your/file.txt |
Just replace /path/to/your/file.txt
with the actual path where your file is located.
Other Cool Things You Can Do with Rename
The rename
command can do more than just change file names. Here are some cool tricks:
– To change all filenames to uppercase, type:
1 |
rename 'y/a-z/A-Z/' * |
– To change all filenames to lowercase, type:
1 |
rename 'y/A-Z/a-z/' * |
– To replace spaces in filenames with underscores, type:
1 |
rename 'y/ /_/' * |
How to Uninstall the Rename Command
If you no longer need the rename
command, you can remove it using the Terminal. The command depends on your Linux version:
– For Ubuntu, Debian, or Linux Mint, type:
1 |
sudo apt remove rename |
– For CentOS or RHEL, type:
1 |
sudo yum remove rename |
Conclusion
Now that you’ve learned how to rename files in Linux using simple commands like rename and mv, the process is straightforward. All you need to do is connect to your remote server via SSH or Terminal, and follow the command lines provided above—it’s that simple!
The rename command in Linux is a powerful and flexible tool for renaming single or multiple files effortlessly.
Whether you’re renaming one file or performing bulk renaming tasks, renaming offers many options to make the process efficient.
It’s easy to install and use, even for beginners, and provides useful features like changing file extensions, adjusting uppercase/lowercase, and replacing characters in filenames.
By following the simple steps in this guide, you can confidently rename files on your Linux system, saving both time and effort.
Rahul Kumar is a web enthusiast, and content strategist specializing in WordPress & web hosting. With years of experience and a commitment to staying up-to-date with industry trends, he creates effective online strategies that drive traffic, boosts engagement, and increase conversions. Rahul’s attention to detail and ability to craft compelling content makes him a valuable asset to any brand looking to improve its online presence.