The cp command

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

  1. Rob

    Rob Administrator Staff Member

    Messages:
    575
    Likes Received:
    214
    Trophy Points:
    43
    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:

    Code:
    mkdir tonyd
    Then you can do:
    Code:
    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'

    As 'fred', you can use the command:

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

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

    You may also use the command

    Code:
    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:

    Code:
    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. plotozoidz

    plotozoidz New Member

    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    cp -i is another useful option.

    -i Stands for 'interactive'. which prompts you for y (yes) or n (no) before overwrite of a file. This helps protect you from overwriting files that you did not intend to.

    Lets say you have two files: file1.txt and file2.txt already in existence.
    And use the following:

    Code:
    cp -i file1.txt file2.txt
    The terminal will prompt you something like:
    cp: overwrite `file2.txt'?
    which you respond by typing y key or n key.

    Now lets say we had only one file: file1.txt in existence. file2.txt did not exist.
    and again we use:

    Code:
    cp -i file1.txt file2.txt
    This time file1.txt would copy to file2.txt
    You would not get a prompt in this case, as the prompt only occurs with a potential overwrite.

    This holds true whether we copying files from one place to another place, or for making a duplicate of one file under a different name.

    -------------------------------------------------------

    Its is also worth noting that if you:

    Code:
    cp -r directory2 directory1
    and directory1 already existed, directory2 will placed inside directory1, as opposed to overwriting it as in the case of cp of files.
    Last edited: Mar 24, 2014
  3. Kampton Joe

    Kampton Joe New Member

    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    what does \; and {} in the following command - Also is space necessary b/w cp and {}? Thanks!!!

    find ./ -name file_1 -exec cp {} /dir1 \;
  4. WharfRat

    WharfRat New Member

    Messages:
    28
    Likes Received:
    10
    Trophy Points:
    3
    The {} is replaced by the current file name being processed that's escaped by the \. The ; is the end of the line -exec command ; as described in man find

    To see what's happening
    Code:
    find /var/log -type f -iname 'syslog*'  -exec echo "-->{}<--" \;
    The -exec is used to take action on any found files like chown, chmod cp or anything else you would need to do with them.

Share This Page