Working with files
Linux commands: mv
Learn how the Linux mv command moves a file to a new path, how it doubles as the way you rename files, and how to move several files into a folder at once.
8 minute lesson
mv changes a path’s name or location:
touch pear
mv pear new_pear
Within one filesystem this is often a metadata change, so moving a large file can be nearly instant. Across filesystems, the implementation must copy the data and remove the source, which takes longer and introduces more failure points.
When the final argument is a directory, move one or more sources into it:
touch pear
touch apple
mkdir fruits
mv pear apple fruits/
The destination is the main risk. Plain mv can replace an existing file. For interactive work, mv -i asks before overwriting. mv -n avoids overwriting where supported. In scripts, check beforehand and handle the race or use application-level atomic file operations when correctness depends on it.
Quote paths with spaces and use -- before names that may begin with a dash:
mv -- "draft report.md" "final report.md"
After a move, verify both sides:
test ! -e old-name && test -e new-name
Moving an open file does not necessarily stop a running Unix process from using it; the process may still hold the underlying file descriptor. That is why log rotation and deployments need deliberate coordination.
Try it: move a file within a directory, then across two mounted filesystems if available. Compare timing and explain the different failure modes.
Lesson completed