How to Rename a File or Folder using the Terminal on Linux Mint

Share
In this tutorial, we'll learn how to rename a file, folder (directory), or symlink with the terminal on Linux Mint using the mv command.

To rename a file, folder, or symlink, execute the following terminal command [how?]:

mv old-name.txt new-name.txt

The command above will rename a file called old-name.txt that exists in the current working directory to new-name.txt. See [How to Refer to Files and Folders using the Terminal on Linux Mint] for how to write this filename argument.

Danger: mv will REPLACE the target file if it exists. It will not show an error or ask for confirmation! More specifically, mv will replaces the hard link that exists at the target filepath, changing which inode it points to. Consequently, if you have multiple hard links to the same file, mv won't affect the other hard links. Notably, this is different from how the cp command works, which overwrites the inode instead of just replacing the hard link.

Questions and Answers

What Happens if The Target of mv Already Exists?

If you mv a file to the filepath of an existing file, the file will be overwritten (the hard link will be replaced).

What Happens if The Target of mv is a Symlink?

If the target of mv is a symlink, the symlink itself will be replaced.

mv is a command that operates on the hard links themselves. When the target of mv is a symlink, what that actually means is that there is a hard link (a filepath) to a symlink inode. From this symlink inode we can get which filepath the symlink is symlinking to. The mv command will replace this hard link with a hard link that points to the inode of the file being renamed, effectively deleting the symlink if its hard link count becomes zero.

Written by Noel Santos.

About the Author

I'm a self-taught Brazilian programmer graduated in IT from a FATEC. In a world of increasingly complex and essential computers, I decided to use my technical expertise in hardware, desktop applications, and web technologies to create an informative resource to make PC's easier to understand.

View Comments