Rename a File in Red Hat Linux with the mv Command
In this article
To rename a file in Red Hat Enterprise Linux (RHEL), use the mv command in a terminal. It changes the file’s name by moving it to a new path; when the source and destination are in the same directory, that effectively renames it. You need access to the file and permission to modify its directory. Take care not to overwrite another file with the destination name.
What You'll Need
- A terminal session on the Red Hat Linux system, either locally or over SSH.
- The file’s current name and its directory, plus the new name you want to use.
- Write permission for the directory containing the file. You generally do not need administrator privileges for files you own.
Step-by-Step
- Open a terminal. If you are connected remotely, sign in to the correct Red Hat system over SSH. Confirm that your shell is running on the system where the file is located.
- Locate the file. If you know its directory, list the contents with
ls -l /path/to/directory. For a file in your current directory, usels -l. Linux names are case-sensitive:Report.txtandreport.txtare different names. - Choose how to refer to the file. You can change to its directory using
cd /path/to/directory, then use just the filename, or provide the full path in the command. Check where you are withpwdbefore using a relative path. - Check whether the new name is already in use. Run
ls -l -- new-name, replacingnew-namewith your intended name. If a file or directory already has that name, do not proceed until you decide whether to choose another name or handle the existing item. On a case-sensitive filesystem, a name differing only in capitalization may be distinct, though some mounted filesystems behave differently. - Rename the file with
mv. For example, in the file’s directory, run:mv -- old-name.txt new-name.txt
The first argument is the existing name; the second is the desired name. The--marks the end of command options, which is helpful if a filename begins with a hyphen. For paths, use a command such asmv -- /home/alex/notes.txt /home/alex/notes-old.txt. - Use quotes for names containing spaces or shell characters. For example:
mv -- "quarterly report.txt" "quarterly report-final.txt"
Quotes keep the shell from treating spaces as separators. They also prevent many special characters in a name from being interpreted by the shell. - Verify the result. Run
ls -l -- new-name.txt, using your actual new name. If you are working in another directory, check the full destination path. The file’s contents are not changed by a same-directory rename, but confirm the name before using it in scripts or applications.
To move a file to another directory and rename it in one operation, provide the new path as the second argument: mv -- old-name.txt /home/alex/archive/renamed.txt. This moves the file as well as changing its name. If you intend only to rename it, keep the destination in the same directory.
Common Mistakes
- Reversing the arguments: The order is source first, destination second:
mv old-name new-name. Reversing them may move a different item or fail. - Using the wrong directory: A relative filename is interpreted from the current directory, not necessarily the directory shown in your file manager. Check with
pwdor use full paths. - Overwriting an existing destination: By default,
mvcan replace an existing file in some situations. Inspect the destination first. You can usemv -i -- old-name new-nameto request confirmation before an overwrite; still read the prompt carefully. - Forgetting spaces or capitalization: Match the existing spelling exactly and quote filenames with spaces. A typo can make
mvreport that the source does not exist. - Using
sudounnecessarily: Renaming a file you own normally does not require it. Administrator privileges can affect ownership or expose system files to accidental changes. Use elevated access only when authorized and needed. - Assuming
mvis a rename-only command: It also moves files. A destination that names a directory can place the source inside that directory rather than give it the intended new name.
Tips
- For important files, record the original name or make a backup before changing it. This is especially useful when configuration files or scripts are referenced by other programs.
- Use tab completion: type the beginning of a filename and press Tab. This can reduce spelling errors; if several names match, press Tab again to see suggestions.
- For a cautious one-off rename, use
mv -i -- old-name new-name. The interactive option asks before replacing a destination, but it does not replace the need to check paths. - For a dry check, use
printf '%s\n' -- old-name new-nameto inspect the strings you plan to use, then runmvonly after confirming them. Do not paste example names unchanged if your actual file has a different path. - Renaming a file can break references to its old name, including shortcuts, scripts, scheduled jobs, and application settings. Update those references if needed.
Troubleshooting
cannot stat or “No such file or directory”: Check the spelling, capitalization, current directory, and path. Use pwd and ls -la to inspect the directory, including hidden files. If the name contains spaces, quote it.
“Permission denied”: You may lack permission to rename the file or write to its parent directory. Check permissions with ls -ld /path/to/directory and ls -l -- old-name. Ask the system administrator or file owner for appropriate access; do not change permissions or use sudo without authorization.
“File exists” or an overwrite prompt: Stop and verify the destination. Choose an unused name, or, if replacement is intentional and you have confirmed the target, follow the prompt. Do not assume the existing item is expendable.
The file appears to have moved instead of being renamed: Check the destination directory. If the second argument was an existing directory, mv may have placed the file inside it. Move it back or rename it using the full intended destination path.
The rename works in the terminal but an application still shows the old name: Refresh the application or directory view. Also check whether the application uses a cached listing or a separate copy of the file.
Frequently Asked Questions
What command renames a file in Red Hat Linux?
Use mv, with the current name first and the new name second: mv -- old.txt new.txt.
Can I rename a file without changing its contents?
Yes. A same-directory mv changes the file’s name, not its contents. Other programs that refer to its old path may need updating.
How do I rename a file that contains spaces?
Put each filename in quotes, such as mv -- "old file.txt" "new file.txt".
How can I rename a file in another directory?
Use its full path for both arguments, for example mv -- /home/alex/old.txt /home/alex/new.txt. This changes the name in that directory.
Does renaming a file require root access?
Usually not if you own the file and have write permission for its parent directory. System-owned files may require an administrator, and changing them can affect the system.
What if a file with the new name already exists?
Choose another name or carefully confirm that replacing the existing file is intended. Use mv -i to get an overwrite prompt, but inspect the destination before answering.