Conquer The Command Line: The cp Command

Jim Homme
3 min readDec 17, 2020

--

How to use the Linux cp command plus essential options

In this installment of “Conquer The Command Line” we talk about how to use the Linux cp command.

When ever you want to copy files or directories, use the cp command.

Memory Aid: Think “copy”

With the cp command, when you copy a file or directory, Linux keeps the original. The command looks like this. cp <original> <new>, where is the, well, original file or directory and is the copy.

You can copy files to the same directory, or you can copy them to a different directory. To do that, you include the path. You can also copy directories to other directories. Here are some examples.

Let’s say you have a file called letter.txt and you want to copy it to a file called letter-template.txt in the same directory. Type cp letter.txt letter-template.txt. To prove that it works, you could use ls to list the directory and see that letter-template.txt now exists.

If you have the file called letter.txt and you want to copy it to a directory called letters, you would type cp letter.txt letters.

Using a path, if you have a directory called docs inside the current directory and you want to copy the file called letters.txt to it, you would type cp letter.txt docs/letters.

Essential Options

-i makes cp ask if you really want to overwrite a file or directory. You might be in danger of destroying something if you want to copy it to a different directory and it happens to have the same name, so this is a good option to use if you are unsure if a file or directory has the same name as the original.

-v makes cp display each file as it copies it.

-l makes cp copy the files pointed to by links, rather than links. The jargon for using links to point to files is "dereferencing."

-r makes cp copy directories recursively, which means all the files and directories in the directory, all the files and directories in its subdirectories, and on and on, until they are all in the new directory.

-rv makes cp copy the contents of a directory recursively and display each file and directory as it copies it.

Related Commands

To print the working directory, the one you are currently pointing to, use the pwd command. To change to directories, use the cd command. To list the contents of a directory, use the ls command. To make directories, use the mkdir command. To remove directories, use the rmdir command. To move or rename files and directories, use the mv command.

Remember to use the man command to find all the options for any command on your system. And remember that for a less technical explanation of commands to check out tldr pages.

What Now?

Hopefully this post has helped you take one more step in conquering the command line. I welcome any feedback.

This post was originally written at https://www.jimhomme.com

--

--

Jim Homme

Musician, father of three, husband. Information Technology veteran. Bookworm. Chess player. Accessibility consultant. Plain language advocate