cp makes a new file
The command cp reads the source file and writes its bytes into a newly created file. The result is a separate inode with its own data blocks. Changing the copy never changes the original, and deleting the original does not affect the copy. The time taken depends on the size of the data, because every byte must be read and written. The new file receives the permissions of the source minus the umask, and the owner of the process, and it gets new timestamps unless you ask cp to preserve them.
Preserving attributes matters for backups. The option -p keeps the mode, the owner where permitted, and the timestamps. The option -a is the archive mode, which is recursive and also preserves symbolic links, so the copy is a faithful image of the tree. Use -a when you want a copy that behaves like the original.
cd /workspace
mkdir -p src
echo "data" > src/file.txt
cp src/file.txt copy.txt
cp -r src backup
ls backupThe first copy creates a new regular file. The recursive copy creates the directory backup and the file inside it. The listing shows file.txt.
mv renames when it can
The command mv behaves differently depending on where the destination is. Inside a single filesystem, it calls the rename system call. That call changes one directory entry from the old name to the new one, and it does not touch the data. A rename of a file that is one gigabyte long takes the same time as a rename of an empty file. The operation is also atomic: another process sees either the old name or the new name, never a half-written state.
Across filesystems the inode cannot be moved to another device by renaming, so mv copies the data, preserves what it can, and then removes the source. If the copy fails partway, the source is still intact, but the destination may be incomplete. That is why a cross-filesystem move of a large directory can be slow and can leave debris after an interruption.
Renaming, moving into a directory, and overwriting
The same command handles three cases. If the destination is an existing directory, mv moves the source inside it. If the destination does not exist, mv renames the source to that name. If the destination is an existing file, mv replaces it, and the old contents are lost without warning unless you ask for interaction. The option -i prompts before overwriting, and -n refuses to overwrite at all. The option -t names the target directory first, which is useful when you move several files into one place.
cd /workspace
mv copy.txt renamed.txt
mkdir -p archive
mv renamed.txt archive/
ls archiveThe first command is a rename inside one directory. The second moves the file into a directory, which is still a rename because the directory is on the same filesystem.
cp and mv with directories and links
Copying a directory requires the recursive option, because cp refuses to copy a directory without it. Moving a directory is always cheap inside one filesystem, because only the entry changes. A symbolic link is moved as a link, not followed. These rules are easy to remember if you keep the model in mind: cp creates new data, and mv renames existing entries.
Watch the trailing slash and the target name. The command cp -r src dest creates dest as a copy of src if dest does not exist, but it copies src into dest if dest already exists. Check whether the destination exists before you run a recursive copy in a script.
Preventing surprises
Use cp -i and mv -i in interactive sessions if you want a prompt, or use -n in scripts to refuse overwrites. Use -v to see each action. For batch work, a dry run is safer than a guess: list the sources with ls or find first, confirm the destination, and only then run the copy or move.
Copying with links and updates
The option -u copies a file only when the source is newer than the destination, or when the destination is missing. It is a simple way to refresh a mirror without rewriting files that have not changed. Combined with -a, it gives a cheap incremental copy for a directory tree, although a dedicated tool such as rsync handles deletions and partial transfers better. The option -l creates hard links instead of copies, and -s creates symbolic links. These options turn cp into a way to build views of data without duplicating it.
A hard-link copy is only safe when nobody edits the files in place, because every name shares one inode. A symbolic-link copy depends on the source staying where it is. Choose the form that matches how the copy will be used, and say so in a comment when you write the script.
Moving between filesystems in practice
You can see whether a move will be a rename by comparing the device numbers of the source and destination directories. The command stat -c %d prints the device number. Equal numbers mean one filesystem, and a rename is possible. Different numbers mean mv will copy. This check takes a second and tells you what the cost will be before you start a large move.
When a copy across filesystems is interrupted, the destination may hold part of the data. A safe script copies to a temporary name on the destination filesystem, verifies the size or a checksum, and then renames the file into place. The final rename is the only step that makes the new name visible, which is the same atomic pattern the last unit of this module explains.
Practice in the lab
The lab copies a file, copies a directory, renames a file, and moves it into another directory. Each step leaves evidence in /workspace, and the checks look at the names and the contents rather than at the inode numbers.