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

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

mv

Move files between directories and rename them — the same operation in Linux.

ls

List the contents of a directory, with options for hidden files, long format, sorting and more.

pwd

Show the absolute path of the directory you are currently in.

More how-to guides

Check disk space in Linux

See how much space is left, and find out what is using it.

Extract a .tar.gz archive

Unpack tar.gz, tar.bz2, tar.xz and zip archives from the command line.

Free a port that is already in use

Find which process holds a port and stop it cleanly.

Find and replace text in files

Replace text in one file or across a whole project, safely.