How to Rename a File in Linux Terminal
You may be new to Linux. Thus even rival tasks like renaming files may seem complicated. But it’s not.
Renaming files in a command prompt is actually easy and more flexible.
Before we delve into the details, it’s important to understand that, unlike graphical user interfaces (GUIs), the Linux command-line interface (CLI) doesn’t have a ‘rename’ button. Instead, we will use a command named. mv
(move), which has more functionality than you might expect.
This post not only covers the basics, but you’ll learn more extended uses of the mv command, like renaming files in bulk.
Renaming a Single File
The most basic scenario involves renaming a single file. Let’s say you have a file named file1.txt
and you want to rename it to file2.txt
. Here’s how you’d do that:
mv file1.txt file2.txt
BashThis command reads as: “Move (or rename) file1.txt
to file2.txt
.” Remember to replace file1.txt
and file2.txt
with your actual file names.
Renaming Files in a Different Directory
Sometimes you want to rename a file located in a different directory. Let’s say you have a file in the directory /home/user/documents
named file1.txt
and you want to rename it to file2.txt
. Here’s how to do it:
mv /home/user/documents/file1.txt /home/user/documents/file2.txt
BashThis command says: “Move (or rename) the file1.txt
in the /home/user/documents
directory to file2.txt
in the same directory.”
Renaming and Moving Files
The mv
command not only renames files but also moves them around. Let’s assume you want to rename a file and move it to a different directory simultaneously. If you have a file named file1.txt
in the directory /home/user/documents
and you want to rename it to file2.txt
and move it to the directory /home/user/downloads
, you would do the following:
mv /home/user/documents/file1.txt /home/user/downloads/file2.txt
BashIn this case, the command reads: “Move (or rename) file1.txt
from the /home/user/documents
directory to file2.txt
in the /home/user/downloads
directory.”
Renaming Multiple Files
Renaming multiple files can seem daunting, but with a combination of shell scripting and the mv command, it’s manageable. Suppose you have several text files named file1.txt
, file2.txt
, and so on, and you want to rename them to newfile1.txt
, newfile2.txt
, etc. Here’s an example using a simple for loop:
for file in file*.txt; do
mv "$file" "${file/file/newfile}"
done
BashIn this script, the for
loop iterates over each file that matches the pattern file*.txt
. The mv
command then renames each file, replacing the ‘file’ part of the name with ‘newfile’.
Well, you can also do it in a single file
for file in file*.txt; do mv "$file" "${file/file/newfile}"; done
BashRenaming Files with Special Characters
Renaming files with spaces or special characters in their names requires some extra attention. You need to use quotes or escape characters to ensure that the command line treats the name as a single entity. Suppose you have a file named file 1.txt
and want to rename it to file 2.txt
. Here’s how:
mv 'file 1.txt' 'file 2.txt'
BashOr with escape characters:
mv file\ 1.txt file\ 2.txt
BashIn both these cases, the space is either escaped with a backslash (\
) or placed within quotes so the terminal knows to treat it as part of the filename rather than a separator.
Renaming Files on a Remote Computer
Renaming files on a remote Linux server is also quite straightforward, provided you have SSH access. The SSH protocol lets you execute commands on a remote machine just as you would on your local system.
Let’s say you have SSH access to a remote machine, and you need to rename a file named file1.txt
to file2.txt
on that machine.
you can rename files on a remote server without explicitly logging into it by running a command directly via SSH. You provide the command you want to run as an argument to the ssh
command.
ssh username@server_ip 'mv /path/to/oldfile /path/to/newfile'
BashThe ssh username@server_ip
part logs you into the remote server. Then 'mv /path/to/oldfile /path/to/newfile'
part, which is enclosed in quotes, specifies the command that you want to run on the remote server.
A Word of Caution
Be careful when renaming files. If you accidentally rename a file to an existing filename, the original file will be overwritten without any warning. To prevent this, you can use the -i
(interactive) option, which prompts you before overwriting:
mv -i file1.txt file2.txt
BashThis command will ask for your confirmation before replacing file2.txt
with file1.txt
.
Conclusion
Renaming files in the Linux terminal doesn’t have to be complicated. With the mv
command and a bit of understanding, you can rename single or multiple files, rename and move files, and handle filenames with special characters. Just remember to be cautious to avoid overwriting existing files. Happy coding!
Thanks for the read, friend. It seems you and I have lots of common interests. Say Hi to me on LinkedIn, Twitter, and Medium.
Not a Medium member yet? Please use this link to become a member because I earn a commission for referring at no extra cost for you.