Premium lesson

Copying Files & Directories

Shell·2 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
icon light bulb key

Unlock the CLI & Scripting with Bash module

Learn how to gain advanced control over the OS and automate complex routine tasks prone to manual errors with the CLI and Bash scripting.

You get immediate access to:

  • 34 focused lessons across the CLI and Bash
  • 4 real-world projects with commented solutions
  • Ongoing updates to this bundle
  • Lifetime access to this bundle
Unlock this module