- 1How to Create a File in Linux
- 2How to Rename a File in Linux
- 3How to Copy a File in Linux
- 4How to Move a File in Linux
- 5How to Delete a File in Linux
- 6How to Create a Directory in Linux
- 7How to Delete a Directory in Linux
- 8How to Copy Directories Recursively in Linux
- 9Delete Directories Recursively in Linux
- 10How to View Hidden Files in Linux
- 11How to Create a Hidden File in Linux
- 12How to Create a Hidden Directory in Linux
- 13How to Find Files in Linux Using find Command
- 14Find Files in Linux Using locate Command
- 15How to View File Contents Using cat in Linux
- 16How to View File Contents Using less in Linux
- 17View File Contents Using the more Command in Linux
- 18Compare Files Using diff Command in Linux
- 19Compare Files in Linux Using cmp Command
- 20Check File Type in Linux with file Command
- 21Create Symbolic Links with ln -s in Linux
- 22How to Archive Files Using tar Command in Linux
- 23Compress Files with gzip in Linux – Beginner Tutorial
- 24How to Compress Files Using bzip2 in Linux
- 25Compress Files in Linux Using zip Command
- 26Extract Compressed Files using tar in Linux
- 27Extract ZIP Files on Linux with unzip
- 28How to Extract .gz Files using gunzip in Linux
How to Rename a File in Linux
How to Rename a File in Linux
In this Linux tutorial, you'll learn how to rename a file using the terminal.
Alright, let’s get started!
The Command You Need: mv
In Linux, there's no separate rename
command for everyday use. Instead, we use the mv
command — short for "move" — to rename files. It's like telling Linux: “Hey, move this file from its old name to a new name... in the same place.”
Basic Syntax
mv old_filename new_filename
Let’s see it in action.
Step-by-Step Example
Suppose we have a file called notes.txt
and we want to rename it to project_notes.txt
.
ls
notes.txt
mv notes.txt project_notes.txt
ls
project_notes.txt
Awesome! You've just renamed a file.
Renaming Inside a Directory
If the file is inside another folder, like documents/
, you can still rename it using the path:
mv documents/oldname.txt documents/newname.txt
This keeps the file in the same folder but changes its name.
What If the File Doesn’t Exist?
If you try to rename a file that doesn't exist, you'll get an error like this:
mv hello.txt goodbye.txt
mv: cannot stat 'hello.txt': No such file or directory
So always make sure the file you're trying to rename actually exists. Use ls
to list files in the current directory if you're unsure.
Pro Tip: No Confirmation by Default
Linux won’t ask for confirmation if new_filename
already exists — it will simply overwrite it. To avoid that, use the -i
(interactive) option:
mv -i file1.txt file2.txt
mv: overwrite 'file2.txt'?
This gives you the chance to stop the action if needed.
Summary
- Use
mv old_name new_name
to rename files - You can rename files in any directory by including the path
- Use
-i
to prevent accidental overwrites
That’s it! You’ve now learned how to rename a file in Linux like a pro. Keep experimenting and try renaming a few files in your system.
Comments
Loading comments...