How to Copy a File in Linux Using cp

In a graphical operating system like Windows, duplicating a file involves right-clicking, selecting ‘Copy’, navigating to a new folder, and right-clicking ‘Paste’. In the Linux terminal, this entire multi-step process is executed instantly with a single command: cp (short for copy). The cp command is fundamental to Linux system administration, used constantly for creating backups of configuration files before editing them, or deploying scripts across multiple directories.

Understanding the Syntax

The cp command requires a very specific syntax (grammar) to understand your intent. It always follows this structure: cp [what to copy] [where to put the copy].

In technical terms: cp source destination.

Scenario 1: Copying a Single File to a New Folder

Imagine you have a file named report.txt in your current directory, and you want to place a copy of it inside a folder named backup.

  1. Open your terminal.
  2. Type the command: cp report.txt backup/
  3. Press Enter.

The terminal will return to a new line without any confirmation message (Linux commands are traditionally silent if successful). You now have two identical files: the original report.txt, and a perfect clone inside the backup/ folder.

Scenario 2: Copying and Renaming Simultaneously

You can use cp to duplicate a file in the exact same directory, provided you give the copy a different name. This is how system administrators back up configuration files before making risky changes.

  1. Type: cp config.yaml config.yaml.backup
  2. Press Enter.

You now have a safe backup sitting right next to the original file.

Scenario 3: Copying Entire Folders (The Recursive Flag)

A common beginner mistake is trying to copy a folder just like a file (e.g., cp folder1 folder2). If you do this, Linux will throw an error: cp: -r not specified; omitting directory 'folder1'.

Because folders contain files, and sometimes other folders inside of those files, the command needs explicit permission to dig down into the folder structure. You must use the -r (recursive) flag.

  1. Type: cp -r project_assets backup_drive/
  2. Press Enter.

The -r flag tells Linux to copy the folder, every file inside it, every sub-folder inside it, and every file inside those sub-folders, duplicating the entire directory tree perfectly into the new location.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.