Copying and Moving Files in Linux
How it works
cp SOURCE DEST copies a file; mv SOURCE DEST moves or renames it. If DEST is an existing directory, the original basename is normally used inside it. To copy a directory tree with GNU cp, use cp -R SOURCE DEST; unlike cp -a, it does not request preservation of all metadata. Both commands can replace an existing destination. GNU -i requests confirmation before overwrite; -n skips existing targets, but skip exit-status behavior varies by version. Check the result rather than relying only on a success status. A move within one filesystem often uses a rename; a cross-filesystem move may copy then remove the source and is not necessarily atomic.
Prepare a safe practice environment
Use GNU/Linux and Bash as a normal user. From the extracted package root run bash lab-fixtures/files/setup.sh. It prints a fresh private directory under /tmp. Copy that path and enter it with cd (quote the path if needed); check pwd. Each code example below starts in a new lab with the same fixture: notes/hello.txt, notes/daily report.txt, notes/-draft, notes/.hidden, and empty directories archive and empty. Set export LC_ALL=C for the shown ordering and English diagnostics. Never run the modifying examples on your own files.
Copy without losing the original
cp -i notes/hello.txt archive/hello.txtcat notes/hello.txtcat archive/hello.txtBoth names contain hello; the destination was absent in the fresh lab.
Rename a copy
cp -i notes/hello.txt archive/hello.txtmv -i archive/hello.txt archive/saved.txtls -1 archiveOnly saved.txt remains in archive. The original in notes is untouched.
After the copy, the two names identify separate regular files. The move changes the name of the copy inside archive, not the original in notes. When checking your own result, inspect both the source and the expected destination; a listing of archive alone cannot establish that the source is unchanged.
Copy a directory tree
The destination is a new directory with all four fixture entries, including .hidden.
Common mistakes and safe habits
Inspect destination paths first. An existing destination directory and a new destination filename have different meanings. Do not use force flags to fix unexpected overwrite prompts. A copy is not automatically a verified backup: compare contents and consider metadata and restoration.
Try it yourself
Copy notes/daily report.txt into archive under the name daily-copy.txt; rename the copy to final.txt and verify that the original still exists with its content.
Use a fresh lab for the practice. When finished, return to the lab root if you are in a subdirectory, then leave it with cd ... Remove only the identified temporary lab folder using your file manager if desired. Never remove an unrelated path.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Copying and moving files — Knowledge Check
Apply filesystem concepts in a fresh disposable Linux lab.
Which command preserves the original while creating a second file?
What happens with mv notes/hello.txt archive/ when archive exists?
What does GNU cp -R allow?
Which option asks before overwriting an existing destination?
Is every cross-filesystem mv an atomic rename?