Search⌘ K

Copying Files

Explore how to use the cp command to copy files between directories, rename files during copying, and use options to prevent data loss. Understand how to work efficiently with wildcards for bulk copying and manage your files safely within the command-line interface.

The cp command

To copy a file, we use the cp command, and specify the source and destination paths.

Let’s try this out by creating a README.md file in the files/code/elm directory. We then copy that file to the other code directories:

mkdir -p files/{movies,music,photos,docs/{diagrams,markdown},code/{go,js,elm}}
mkdir -p files/code/elm 
echo "# Project README" > files/code/elm/README.md
cat files/code/elm/README.md

We can practice the code in the terminal below.

Terminal 1
Terminal
Loading...

To copy this file to the files/code/go directory, we use the cp command and specify the source path, files/code/elm/README.md, and then the destination path, files/code/go/README.md:

$ cp files/code/elm/README.md
...