Back to Blog
Lesson 7 of the Linux: Linux Command Line for Developers course
LinuxJuly 25, 20264 min read

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.

linuxcommand-linefile-managementcpmvtutorial
A close-up shot of hands inserting a handwritten CD into an envelope, with a computer in the background.

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:

Bash
cp [source] [destination]

If you are working in your web server project and need to create a backup of your index file, you would run:

Bash
cp 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:

Bash
mv [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.

Bash
mv 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:

Bash
mv 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.

  1. Verify your location: Ensure you are in your project root.
  2. Move the template: Move temp_config into the config/ directory.
  3. 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

  1. Navigate to your project root directory.
  2. Create a new dummy file named draft.txt using the touch command.
  3. Move draft.txt into your logs/ directory.
  4. Copy the file from logs/draft.txt back to your project root, but name the copy final_draft.txt.
  5. Use the ls command to confirm both files exist in their respective locations.

Common Pitfalls

  • Overwriting files: By default, mv and cp will overwrite the destination file if it already exists without asking for confirmation. Use cp -i or mv -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 -r flag.
  • Trailing slashes: When using mv, if the destination directory exists, the file will be moved inside that directory. If the directory does not exist, mv will 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 cp for duplication and cp -r for entire directories.
  • Use mv for both renaming files and relocating them to new paths.
  • Always be mindful of overwriting existing files; use -i for safety.

Up next: We will learn how to modify the content of the files you’ve organized using basic text editors.

Similar Posts