Cp Command (Linux)

cp has three principal modes of operation. These modes are inferred from the type and count of arguments presented to the program upon invocation.

[1] When the program has two arguments of path names to files, the program copies the contents of the first file to the second file, creating the second file if necessary.

[2]When the program has one or more arguments of path names of files and following those an argument of a path to a directory, then the program copies each source file to the destination directory, creating any files not already existing.

Note: For this case I have found that the destination directory needs to exist. else there is an error 

$ cp _datafolder1/1040.csv _datafolder2/900.csv _mydata3
cp: target ‘_mydata3’ is not a directory

Works fine when directory already exists : 

agoswami@agoswami-msft2 /cygdrive/e/azureml/12Dec/TR
$ mkdir _mydata3

agoswami@agoswami-msft2 /cygdrive/e/azureml/12Dec/TR
$ cp _datafolder1/1040.csv _datafolder2/900.csv _mydata3

agoswami@agoswami-msft2 /cygdrive/e/azureml/12Dec/TR
$ ls -l _mydata3/
total 555156
-rwxrwx—+ 1 agoswami Domain Users 474014440 Dec 17 13:01 1040.csv
-rwxrwx—+ 1 agoswami Domain Users 94459824 Dec 17 13:01 900.csv

[3]When the program’s arguments are the path names to two directories, cp copies all files in the source directory to the destination directory, creating any files or directories needed. This mode of operation requires an additional option flag, typically r, to indicate the recursive copying of directories. If the destination directory already exists, the source is copied into the destination, while a new directory is created if the destination does not exist.



In terms of options, the useful ones are:

  • R or r (recursive) – copy directories recursively



[1] Creating a copy of a file in the current directory:

cp prog.c prog.bak

This copies prog.c to prog.bak. If the prog.bak file does not already exist, the cp command creates it. If it does exist, the cp command replaces its contents with the contents of the prog.c file.

[2] Copy two files in the current directory into another directory:

cp jones smith /home/nick/clients

This copies the smith file to the smith.jr file. Instead of creating the file with the current date and time stamp, the system gives the smith.jr file the same date and time as the smith file. The smith.jr file also inherits the smith file’s access control protection.

[3] Copy a directory, including all its files and subdirectories, to another directory:

cp -R /home/nick/clients /home/nick/customers


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s