Copying files & directories: cp

Objectives
  • Demonstrate how to make copies of files and directories.
  • Demonstrate how to copy files and directories with whitespaces in their name.
Prerequisites
  • None.

Overview

Use cp to copy a file or directory (to another location).

For basic operations, cp command takes two arguments: source file/directory and destination file/directory:


cp SOURCE DESTINATION
 

  • If the destination is a directory, the file will be copied there, and will keep its filename.
  • If the destination is a filename, the file will be copied and the copy will be renamed.

As with all commands, be sure to use the --help option to explore all the features this command offers.

Copying files

Copying a file into another directory

sarah@laptop ~/Downloads$ cp calculations.xls ../documents
sarah@laptop ~/Downloads$

→ The above command copies the file calculations.xls to the ~/documents directory

→ The calculations.xls name is not changed.

Copying a file into your current directory

sarah@laptop ~/documents/reports$ cp ~/Downloads/report5.pdf .
sarah@laptop ~/documents/reports$

→The above command copies the file ~/Downloads/report5.pdf to your current directory.

→ Remember: a single dot represents your current directory (~/documents/reports)

Copying a file into another directory and renaming it

sarah@laptop ~/Downloads$ cp calculations.xls ../documents/TaxCalc2023.xls
sarah@laptop ~/Downloads$

→ The above command copies the file calculations.xls to the ~/documents directory, and renames it to TaxCalc2023.xls.

Duplicating a file in the same directory

sarah@laptop ~/Downloads$ cp calculations.xls calc.xls
sarah@laptop ~/Downloads$

→ The above command copies the file calculations.xls and saves it in the same directory (the current directory) under the name calc.xls.

Copying directories

You can also use the cp command to copy directories:


cp -r SOURCE DESTINATON
 

Note:

When you copy a directory, you also copy all of its contents: subdirectories (and their subdirectories, and so on) and files. In other words, cp performs a recursive copy.

To copy directories, you must provide the -r option, which stands for recursive, otherwise the command will refuse to copy them.

Copying a directory into another directory

sarah@laptop ~/documents/reports$ cp -r 2023 ~/archive/reports
sarah@laptop ~/documents/reports$

→ The above command copies the ~/documents/reports/2023 directory (and all of its contents) into the ~/archive/reports directory.

Copying a directory into your current directory

sarah@laptop ~/images$ cp -r ~/Downloads/screenshots .
sarah@laptop ~/images$

→ The above command copies the ~/Downloads/screenshots directory (and all of its contents) into your current directory (~/images).

Duplicating a directory in the same directory

sarah@laptop ~/documents/$ cp -r reports reports_backup
sarah@laptop ~/documents$

→ The above command copies the ~/documents/reports directory (and all of its contents) and saves it in the same directory (the current directory) under the name reports_backup.

Directories with spaces in their name

There are two ways to work with directories that have spaces in their name:

Use single or double quotes

sarah@laptop:~/Documents$ cp "draft reports" ~/backups

Use backslash to escape each whitespace character

sarah@laptop:~/Documents$ cp draft\ reports ~/backups

Both methods are equivalent; feel free to use the one you are most comfortable with.

Summary

  • Use cp to copy files.
  • Use cp -r to copy directories and their contents (recursively).
  • If the destination is a directory, the file will be copied there, and will keep its filename.
  • If the destination is a filename, the file will be copied and the copy will be renamed.
  • If the directory name contains whitespaces, wrap the name in quotes or escape each whitespace character with a backslash (\)