cp Command in Linux

Back

The cp ( copy ) command allows you to take the contents of one file and place a copy with the same or different name in the directory of your choice. 

For example :

cp file1 file2  
                 This command takes the content of file1 and saves the contents in file2. One of the dangers of cp is that it can easily overwrite files in different directories, without prompting you to make sure that's what you really wanted to do.

The cp command with -r switch supports recursive changes. 

For example :

$ cp  -ar  /usr/share/doc/.  /doc
                This command will copies all sub-sirectories of the noted directories along with associated files.


$ cp file1 file2
                copy file1 to a file called file2

$ cp file1 /tmp
               copy file1 to the /tmp directory

$ cp file1 ~smith
               copy file1 to the home directory of "smith"

$ cp * /tmp
               copy everything in the directory to the /tmp directory