Amazon Cyber Monday deals See deals →ad

Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

How to Rename a File in Linux with the mv Command Safely

Renaming a file in Linux is usually done with the mv command. Although its name means “move,” it can also give a file a new name when the old and new paths are in the same directory. This guide shows the basic command, how to handle filenames with spaces, and ways to avoid overwriting the wrong file. You will need access to a terminal and permission to change the file or its containing directory.

The examples work in common Linux distributions and most Unix-like shells. Commands are case-sensitive: enter filenames with the same uppercase and lowercase letters shown by your system. If you are unsure about a name, list the directory contents before changing anything.

What You’ll Need

  • A Linux terminal, such as a terminal application on your desktop or an SSH session.
  • The file’s current name and the name you want it to have. Include the extension if you want to keep it.
  • Permission to rename the file and, in many cases, write permission on its containing directory.
  • For important files, a backup or a plan to check the new name before using the file again.

Step-by-Step

  1. Open a terminal. On a desktop Linux system, open the Terminal application from the applications menu. If the file is on a remote server, connect to it first using your usual SSH method. Commands run on the machine and account where the terminal session is active, so check that you are working on the intended system.
  2. Find the file and confirm its exact name. Use pwd to show the current directory, then use ls to list its contents. For example, the listing might show draft.txt. If you do not see the file, use cd to enter the folder that contains it, or plan to specify its path in the rename command. Remember that Draft.txt and draft.txt are different names on a typical Linux filesystem.
  3. Choose the new name and decide whether to keep the extension. A file named draft.txt can become final.txt with the extension preserved. Linux does not require extensions, but applications often use them to identify file types. Renaming it to final removes the visible extension; it does not convert the file’s contents. Check the full destination name before running the command.
  4. Run mv with the old name and new name. In the same directory, enter mv draft.txt final.txt and press Enter. The first argument is the existing file; the second is its destination. When the destination is a new name in the same directory, this renames the file. A successful command normally produces no message, so verify the result with ls or ls -l final.txt.
  5. Protect against replacing an existing file. Before renaming, check whether the destination already exists with ls -l final.txt. You can also use mv -i draft.txt final.txt. The -i option asks before overwriting an existing destination, giving you a chance to answer n and cancel. Do not assume a plain mv will always ask: in many configurations it replaces an existing file without a prompt.
  6. Use the correct path if the file is elsewhere. You can provide a relative path, such as mv notes/draft.txt notes/final.txt, or an absolute path, such as mv /home/sam/notes/draft.txt /home/sam/notes/final.txt. For a rename, keep the destination in the same directory and change only its final name. If the destination names a different directory, mv moves the file there; it does not merely rename it in place.
  7. Quote names that contain spaces or special characters. For a file named trip notes.txt, use mv 'trip notes.txt' 'travel notes.txt'. The quotes tell the shell to treat each complete name as one argument. You can also escape spaces with a backslash, as in mv trip\ notes.txt travel\ notes.txt, but quoting is often easier to read. Apply quoting to both source and destination when needed.
  8. Check the result and the file itself. Run ls -l to confirm the new filename, then open or inspect the file if it is important. For example, file final.txt reports the file type, and head final.txt can show the first lines of a text file. These checks help confirm you renamed the intended item rather than a similarly named file.

Common Mistakes

Reversing the two arguments: The standard order is mv source destination. If you type the new name first, the command will usually report that the source does not exist. Pause to identify which name is current and which is the intended result before pressing Enter.

Forgetting spaces need quoting: Without quotes, the shell splits a name such as trip notes.txt into separate arguments. The command may fail or act on an unintended path. Quote the entire filename, including its extension, rather than only the part before the space.

Accidentally moving instead of renaming: If the second argument is an existing directory, mv places the file inside that directory. For a simple rename, use the new full filename as the destination, not just the name of a folder. If you do want to move and rename at once, provide a destination path ending in the desired filename.

Overwriting a file or changing the extension unintentionally: A typo in the destination can replace an existing file, and omitting .txt removes that extension from the name. Inspect the destination first, use mv -i when appropriate, and review the whole command before executing it.

Using a graphical file manager’s undo expectations: A terminal rename may not have a convenient undo button. Linux does not keep a universal rename history that restores the previous name automatically. If the name matters, record the original name or make a backup before changing it.

Tips

Use tab completion to reduce typing errors. Start entering a unique part of the filename and press Tab; the shell may complete it for you. If several names match, press Tab again to see options. This is especially useful for long names, but still inspect the completed command because completion does not decide which destination you intend.

For extra caution, preview the exact source and destination with printf '%s\n' 'draft.txt' 'final.txt', or run ls -l -- draft.txt final.txt before renaming. The double hyphen in commands that support it marks the end of options, which helps when a filename begins with a hyphen. For example, mv -- -draft.txt final.txt treats -draft.txt as a filename rather than an option. The -- convention is supported by GNU coreutils on most Linux distributions.

For repeated renaming, use a small shell script only after testing it on sample files. A basic mv command renames one source at a time. Shell wildcards can match several files, but a command such as mv *.txt archive.txt does not rename every match to a separate name; it treats the final argument as a destination and may move the matched files into a directory if that destination is one. For batch renaming, inspect your distribution’s available tools and preview their changes before applying them.

Names that begin with a dot, such as .config-old, are hidden in ordinary ls output. Use ls -a to display them. Also, moving or renaming a file to another filesystem can involve copying and deleting rather than a single directory-entry change, so do not interrupt a large operation without checking its status.

Troubleshooting

“No such file or directory”: The source name or one of its directories is misspelled, the current directory is not what you expect, or the file is hidden. Check pwd, list files with ls -la, and compare the spelling carefully. If the name contains spaces, quote it. If you are not in the file’s directory, use its correct relative or absolute path.

“Permission denied”: You may lack permission to rename the file, or you may not have write permission on its containing directory. Check ownership and permissions with ls -l. If it is your file, change to a directory where you have permission or ask the system administrator for access. Do not reflexively add sudo: elevated privileges can affect the wrong file, and using them is not a substitute for checking the path.

The destination already exists: Stop and decide whether that file should be kept. Inspect it with ls -l or open it before proceeding. Choose a different destination name if you need both files. If replacing it is intentional, make a backup first and use an interactive prompt such as mv -i so you can confirm the replacement.

The file seems to have disappeared: It may have been moved into a directory rather than renamed, or the new name may be hidden or misspelled. Search the likely folder with ls -la. If you know the old name, a command such as find . -name 'draft.txt' -print searches below the current directory. Once found, use its actual path to move it back or rename it again.

The command reports “same file” or does nothing visible: Check whether the old and new names resolve to the same path, including capitalization and directory components. A successful rename is often silent, so verify with ls -l. If a desktop application still shows the old name, refresh its view; the file may already have been renamed on disk.

Related

Frequently Asked Questions

Is there a Linux command called rename?
Some distributions provide a separate rename utility for batch changes, but its behavior and syntax vary. For renaming one file, mv oldname newname is the standard, widely available approach.

Does mv change the file’s contents?
No. When used to rename a file in the same directory, it changes the name or directory entry, not the file’s contents. The file’s permissions and content normally remain intact, although details such as timestamps can vary depending on the operation and filesystem.

Can I rename a file to a name that already exists?
Usually, yes, but a normal mv may replace the destination. Check first and use mv -i to request confirmation. If you need to preserve both files, choose a destination name that is not already in use.

How do I rename a file in another directory?
Give both the existing path and the new path, for example mv /home/sam/docs/draft.txt /home/sam/docs/final.txt. Keeping both paths in the same directory makes it a rename. Naming another directory as the destination moves the file there instead.

Can I rename a file beginning with a hyphen?
Yes. Use -- before the filename when supported, as in mv -- -old.txt new.txt. Alternatively, specify a path such as ./-old.txt. Both forms prevent the leading hyphen from being interpreted as an option.

How to Rename a File in Linux with the mv Command Safely
Scroll to top