Technical skill

Linux command: mv

Subject: .../LINUX

Rename a file or put it in another folder

The mv command gives a file a new name or puts it in another folder. On Unix, folders are called directories. A path names where a file is, and mv takes a source path and a destination path. If the destination already names a directory, the source goes inside it with its current name. If the destination names a new path, mv uses that exact name. With several source paths, the last argument must be a directory. This is why mv report.log archive/ can have a different result from mv report.log archive/report.log when archive already exists.

For example, this gives a configuration file a backup-style name in the same folder:

mv -- ./service.conf ./service.conf.old

The -- tells GNU Coreutils to stop reading options, so a source path that begins with a hyphen is treated as a path. When a path is stored in a shell variable, quote it to keep spaces and wildcard characters in one argument:

mv -i -- "$source_file" "$backup_file"

Here -i asks before replacing an existing destination. Without that option, mv may replace a writable destination without asking. GNU Coreutils also has -T, which treats the destination as the exact new path instead of as a directory that should receive the source. That is useful when the target name must be unambiguous.

What happens to the file

When source and destination are on the same filesystem, a move is normally a rename of the directory entry; it does not copy all the file's contents. Across filesystems, the system cannot perform that rename. GNU mv copies the object and removes the source after the copy succeeds. A cross-filesystem move takes longer and is not atomic, so a failure during a multi-file move can leave some items in the destination while others remain at the source.

Permissions on the containing folders control whether their entries can be changed. A file's read permission usually does not decide whether its name can be moved. ACLs, the sticky bit on shared folders (which restricts who can delete entries), mount options, and security rules can affect the result. A symbolic link is a special file that points to another file or directory; moving the link moves the link itself. Avoid a trailing slash on a source that might be a symbolic link to a directory; systems differ on that case, and GNU documents surprising behavior.

Check the paths before relying on the result

Inspect both locations with ls -ld -- "$source_file" "$target_dir" and decide whether the destination should be a new name or a folder. Use an explicit destination path for a rename, or put an existing directory last when moving several sources. For a manual move, mv -i can help prevent an unnoticed replacement. A script should handle errors and choose an overwrite policy without depending on an interactive prompt. After a cross-filesystem move, verify the destination before removing any source data left behind. A successful move removes the original path, so it is not a backup.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Skyeng QA Automation Hard Skillset v09.21 preview
image descriptionSkyeng QA Automation Hard Skillset v09.21
by Aleksandra Kulikova
16 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful