Copying Files & Directories

7 min read·Jan 1, 2025

To copy a single file in a specified directory, you can use the cp command — which stands for copy — as follows:

$ cp source target

Where:

  • source is the path to the file you want to copy.
  • target is the path to the directory you want to copy it into.

Note: If the last component of the target parameter is not an existing directory, the file will be copied under the specified name instead of its original name.

Example

This command will copy the index.js file located in the current working directory into the api directory under its original name:

$ cp index.js ./api

This command will copy the index.js file located in the current working directory into the same directory under the name index_copy.js:

$ cp index.js index_copy.js

This command will copy the index.html file located in the ~/websites/blog directory into the ~/websites/shop directory under the name home.html:

$ cp ~/websites/blog/index.html ~/websites/shop/home.html

Copying multiple files at once

To copy multiple files at once in a specified directory, you can use the cp command as follows:

$ cp source ... target

Where:

  • source ... are the paths to the files you want to copy.
  • target is the path to the directory you want to copy them into.

Note: When using this syntax, all the specified files will copied under their original names.

Example

This command will copy all the files located in the ~/api directory with a .json file extension into the ~/api-v2 directory:

$ cp ~/api/*.json ~/api-v2

Copying a single directory

To copy an entire directory, including the files and subdirectories it contains, you can use the cp command with the -R flag (short for recursive) as follows:

$ cp -R source target

Where:

  • source is the path to the directory you want to copy.
  • target is the path to the directory you want to copy it into.

Note: If the target directory doesn't exist, the cp command will automatically create it for you.

Example

This command will copy the ~/website/logs directory and its content into the ~/backups directory under the name logs_2024_04:

$ cp -R ~/website/logs ~/backups/logs_2024_04

Copying multiple directories at once

To copy multiple directories at once in a specified directory, you can use the cp command with the -R flag as follows:

$ cp -R source ... target

Where:

  • source is the path(s) to the directory(ies) you want to copy.
  • target is the path to the directory you want to copy them into.

Note: When using this syntax, you will have to first create the target directory as the cp command will otherwise output the following error:

cp: target is not a directory

Example

This command will copy the logs_2024_03 and logs_2024_04 directories and their content into the logs directory:

$ cp -R logs_2024_03 logs_2024_04 logs

Safely copying entries

By default, the cp command will not check if a file with the same name already exists in the target directory and will automatically overwrite it.

Copying entries in interactive mode

To be prompted for confirmation before potentially overwriting an existing file with the same name in the target directory, you can use the cp command with the -i flag (short for interactive):

$ cp -i source [...] target

Once executed, you can either type y (short for yes) to proceed or n (short for no) to skip the copy of this entry, then press the ENTER key to confirm your choice.

Example

This command will prompt for confirmation before attempting to overwrite the server.js file present in the current working directory:

$ cp -i index.js server.js
overwrite server.js? (y/n [n]) n
not overwritten

Note: By default, the n answer is pre-selected to prevent any file loss in case the ENTER key is accidentally pressed.

Skipping the copy of overlapping files

To silently skip the copy of files that may overlap in the target directory, you can use the cp command with the -n flag as follows:

$ cp -n source [...] target

Alternatively, if you want the cp command to explicitly indicate which files where skipped, you can also use the -v flag (short for verbose) as follows:

$ cp -n -v source [...] target
server.js not overwritten

Example

This command will skip the copy the index.js file into the current working directory under the name server.js and explicitly indicate it:

$ cp -n -v index.js server.js
server.js not overwritten

Summary

Here's a summary of what you've learned in this lesson:

  • The cp command is used to copy files and directories.
  • The cp source target command is used to copy single source entry into a target directory.
  • The cp source ... target command is used to copy multiple source entries into a target directory.
  • The cp -i command is used to copy entries in interactive mode.
  • The cp -n command is used to silently skip the copy of overlapping entries in the target directory.

Enjoying the courses?

I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:

Support BackendBrewery
Copying Files & Directories in Bash | Backend Brewery