Purchase Linux CDs / DVDs / Flash Drives at OSDisc.com

Welcome to Our Community

While Linux.org has been around for a while, we recently changed management and had to purge most of the content (including users). If you signed up before April 23rd, 2017 please sign up again. Thanks!

  1. More ways to get the info! - we shoot all of our new original content out as well as random messages on Twitter and our newsletter!. Twitter | Newsletter
    Dismiss Notice

The cp command

Discussion in 'Linux Beginner Tutorials' started by Rob, Jul 9, 2013.

  1. Rob

    Rob Administrator
    Staff Member

    Oct 27, 2011
    Likes Received:
    To show you how to copy files with Linux we talked about 'cp' in the lesson on aliases. 'cp' is for copying files from one place to another, or for making a duplicate of one file under a different name.

    Let's go back to Tony's 'stuff' file For example, if you saved Tony's e-mail attachment to your main /home directory, /home/[your name], you may want to create a directory to keep Tony's files You could make the directory for Tony tonyd (Tony's last name is Dweebweiler)

    Type this:

    mkdir tonyd
    Then you can do:
    cp stuff tonyd
    Remember use your TAB key to save time.

    Now you're going to have TWO files named 'stuff' because you copied that file to the directory 'tonyd/' - you didn't move it there. You'll have the original 'stuff' in your home directory and then the copy in /home/[your name]/tonyd/.

    You'll be able to tell the difference between the two files because the copy of 'stuff' in the directory 'tonyd' will show a different time. Use the command ls -l stuff on both files to see this.

    If you had used the command cp -p instead of just cp you would end up with two identical files in two different places. If you don't want that, there's a better way of doing it so that 'stuff' is only in the directory 'tonyd'. That's themv command. We'll talk about that shortly.

    More uses of the 'cp' command

    To show you how to copy directories and create duplicates of files. Now let's talk about two more basic uses of the cp command and some short cuts.

    You can also copy entire directories to another place. As I mentioned in a previous lesson, you may want to work as two different users for two different jobs. You may be working as 'fred' and your directory 'tonyd' is in the directory /home/bob, where you work as 'bob'

    (Log in to hide this advertisement)

    As 'fred', you can use the command:

    cp -r /home/bob/tonyd/ /home/fred/
    If you're in your home directory you can use this command

    cp -r /home/bob/tonyd/ ./
    To copy the directory 'tonyd' to your home directory.

    You may also use the command

    cp -r /home/bob/tonyd/ ~
    With the tilde wherever you happen to be and that will automatically copy the directory 'tonyd' to your other home directory.

    The other use of 'cp' we talked about was to get a copy of a file with a different name. For example, Tony's file 'stuff' is loaded with jokes. You may want to add some more jokes and then pass it along to another person You could do this:

    cp stuff stuff2
    or choose a name that's meaningful for you other than 'stuff2'

    Now you have another file that you can add jokes to while you preserve the original file. You can open it in 'pico' and start writing: \"Why did the chicken cross the road...\"

    Always remember to use that TAB key and the up and down arrows to save yourself some time.
  2. Greghove

    Greghove New Member

    Dec 22, 2017
    Likes Received:
    I'm sorry but i can't find the Alias lesson. Could you please give me a link to it?
  3. JasKinasis

    JasKinasis Well-Known Member

    Apr 25, 2017
    Likes Received:
    I don't think there is one in the tutorials section ATM - it may have been one of the old posts that was lost when the site was resurrected by Rob.

    But I started a thread about the alias command:

    In my initial post, the 1st part explains what aliases are and how to use them. In the 2nd part of it, I list a few aliases which I use often.

    Also there is Jarrets post from his Linux+ series:

    I hope that helps!
    #3 JasKinasis, Jan 11, 2018
    Last edited: Jan 11, 2018
    LangLiu, Greghove and atanere like this.
  4. Greghove

    Greghove New Member

    Dec 22, 2017
    Likes Received:
    Thank you very much good sir!
  5. Rishav Kumar

    Rishav Kumar New Member

    Mar 4, 2019
    Likes Received:
    difference between
    cp -r /home/bob/tonyd/ ~ and cp /home/bob/tonyd/ ~
    I can't understand...
  6. wizardfromoz

    wizardfromoz Super Moderator
    Staff Member Gold Supporter

    Apr 30, 2017
    Likes Received:
    G'day @Rishav Kumar and welcome to linux.org :)

    If you go to Terminal and type and enter

    man cp
    it will give you the options for the cp command.

    If you have established that /home/bob/tonyd/ has NO subfolders, and NO files, you could use

    cp /home/bob/tonyd/ /home/Rishav/
    cp /home/bob/tonyd/ ~
    If, however, you have established that there are subfolders and/or files within /home/bob/tonyd/ , then you would use

    cp -r /home/bob/tonyd /home/Rishav
    cp -r /home/bob/tonyd ~
    The -r options means "recursively", that is, it copies everything.

    Hope this helps

    Chris Turner
    Rob likes this.

Share This Page