How to Rename a File in Linux
Rename files and directories with mv, including names containing spaces.
Renaming a file in Linux is a move operation within the same directory. The `mv` command changes the directory entry while leaving the file contents intact.
Step by step
1 Rename a file
$ mv old.txt new.txt Changes the filename in the current directory.
2 Rename a directory
$ mv old-project new-project The same syntax works for directories.
3 Handle spaces safely
$ mv "old report.txt" "final report.txt" Quote filenames containing spaces or other shell metacharacters.
4 Avoid accidental replacement
$ mv -i old.txt new.txt Interactive mode asks before replacing an existing destination.
Tips worth knowing
- Use tab completion to avoid typing complicated filenames incorrectly.
- Check `pwd` before using relative paths.
- For batch renaming, test a command on a small sample before applying it broadly.
Frequently asked questions
What command renames files in Linux?
Use `mv old-name new-name`.
Does mv change file contents?
No. A normal rename changes the pathname, not the file data.
How do I rename a file with spaces?
Quote the old and new names, for example `mv "old file.txt" "new file.txt"`.
The commands behind it
Move files between directories and rename them — the same operation in Linux.
lsList the contents of a directory, with options for hidden files, long format, sorting and more.
pwdShow the absolute path of the directory you are currently in.
More how-to guides
See how much space is left, and find out what is using it.
Extract a .tar.gz archiveUnpack tar.gz, tar.bz2, tar.xz and zip archives from the command line.
Free a port that is already in useFind which process holds a port and stop it cleanly.
Find and replace text in filesReplace text in one file or across a whole project, safely.