Mastering cp and mv: Moving and Copying Data in Linux
Learn to copy and move files in Linux with the cp and mv commands. Master recursive flags and path manipulation to organize your server project workspace.

Previously in this course, we covered creating and removing files using touch and rm. Now that you can generate and clean up files, you need to manage their location and duplication within your project. This lesson introduces cp and mv, the two fundamental utilities for organizing your Linux workspace.
Copying Files with cp
The cp (copy) command creates an exact replica of a file or directory. Unlike moving, copying preserves the original source while generating a new instance at the destination.
The basic syntax is simple:
Bashcp [source] [destination]
If you are working in your web server project and need to create a backup of your index file, you would run:
Bashcp index.html index.html.bak
Recursive Copying
When you need to copy an entire directory, a standard cp command will fail because it does not know how to handle the nested contents. You must use the -r (recursive) flag. This tells the shell to walk through the source directory and replicate every file and subdirectory within it.
Bash# Copying the entire 'assets' folder to a backup directory cp -r assets/ assets_backup/
Moving and Renaming with mv
The mv (move) command is dual-purpose: it relocates files from one path to another, and it renames files by "moving" them to the same directory with a different name.
The syntax is identical to cp:
Bashmv [source] [destination]
Renaming Files
Because moving a file within the same folder is logically just a rename operation, there is no separate "rename" command in standard Linux.
Bashmv server_config.txt production_config.txt
Moving Files
To move a file into a different directory (for example, putting a log file into our logs/ directory created in project kickoff), use:
Bashmv access.log logs/
Worked Example: Managing the Web Server Project
Let’s apply these commands to your hardened web server project. We want to move a template configuration into the correct location and create a backup of it before we start editing.
- Verify your location: Ensure you are in your project root.
- Move the template: Move
temp_configinto theconfig/directory. - Create a backup: Create a copy of that config within the same folder.
Bash# 1. Move the template into the config folder mv temp_config config/web_server.conf # 2. Create a backup copy for safety cp config/web_server.conf config/web_server.conf.bak # 3. Verify the changes ls -l config/
Hands-on Exercise
- Navigate to your project root directory.
- Create a new dummy file named
draft.txtusing thetouchcommand. - Move
draft.txtinto yourlogs/directory. - Copy the file from
logs/draft.txtback to your project root, but name the copyfinal_draft.txt. - Use the ls command to confirm both files exist in their respective locations.
Common Pitfalls
- Overwriting files: By default,
mvandcpwill overwrite the destination file if it already exists without asking for confirmation. Usecp -iormv -i(interactive) if you want the shell to prompt you before overwriting. - Forgetting the recursive flag: If you get an error message saying "omitting directory," it is almost certainly because you tried to copy a folder without the
-rflag. - Trailing slashes: When using
mv, if the destination directory exists, the file will be moved inside that directory. If the directory does not exist,mvwill rename the file to that directory name. Always double-check your pathing using absolute and relative pathing techniques.
FAQ
Can I move multiple files at once?
Yes. You can pass multiple source files to mv or cp, provided the last argument is a directory. For example: mv file1.txt file2.txt logs/ will move both files into the logs/ folder.
What happens if I copy a directory to a destination that already exists?
If the destination directory exists, cp -r will place the source folder inside the destination folder, rather than merging the contents.
Recap
- Use
cpfor duplication andcp -rfor entire directories. - Use
mvfor both renaming files and relocating them to new paths. - Always be mindful of overwriting existing files; use
-ifor safety.
Up next: We will learn how to modify the content of the files you’ve organized using basic text editors.
Work with me

VPS Server Setup, Deployment & Hardening
Get your app live on a fast, secure server — properly configured, hardened, and deployment-ready. No more wrestling with the command line.

Custom Email & File Storage System on Cloudflare (Google Workspace Alternative)
Your own private email + file storage suite on your domain — unlimited mailboxes, no per-seat fees. A self-owned Google Workspace alternative for a flat ~$5/month.

