cp- Unix, Linux Command


Advertisements

Previous Page
Next Page  
 

NAME

cp-The cp command is used to make copies of files and directories.

SYNOPSIS

cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...

DESCRIPTION

cp- The cp command is a command to create copy of files and directories. The command has three principal modes of operation, expressed by the types of arguments presented to the program for copying a file to another file, one or more files to a directory, or for copying entire directories to another directory. The utility further accepts various command line option flags to detail the operations performed.

Options

Tag Description
cp -a archive files
cp -f force copy by removing the destination file if needed
cp -i interactive - ask before overwrite
cp -l link files instead of copy
cp -L follow symbolic links
cp -n no file overwrite
cp -R recursive copy (including hidden files)
cp -u update - copy when source is newer than dest
cp -v verbose - print informative messages

EXAMPLES

EXAMPLE-1: 

To copy single file main.c to destination directory bak:

$ cp main.c bak

Note:If the destination file newfile already exists, it will be overwritten without a confirmation prompt.
This is the default behavior for all cp operations.
If you want to be prompted before overwriting a file, use the -i (interactive) option.

EXAMPLE-2:

To copy 2 files main.c and def.h to destination absolute path directory /home/usr/rapid/ :

$ cp main.c def.h /home/usr/rapid/

EXAMPLE-3: 

To copy all .c files in current directory to subdirectory bak

$ cp *.c bak

EXAMPLE-4:

To copy directory src to absolute path directory /home/usr/rapid/

$ cp src /home/usr/rapid/

EXAMPLE-5:

To copy all files and directories in dev recursively to subdirectory bak

$ cp -R dev bak

EXAMPLE-6:

To create hard links to files instead of copying them:

Sometimes for any reasons, you may want to create “shortcut” or links to the files instead of copying them.

To do this, we can use -l option.

$  cp -l file1.txt   /home/nubo/test

output:

~$ cd test

$ ls -lrt
total 8
-rwxrw-r-- 1 nubo nubo 0 Dec 29 19:03 <F11>
-rwxrw-r-- 1 nubo nubo 248 Dec 29 19:07 Test-AR.a
-rwxrw-r-- 1 nubo nubo 0 Dec 29 19:18 example-4
-rwxrw-r-- 1 nubo nubo 0 Dec 29 19:18 example-3
-rwxrw-r-- 1 nubo nubo 0 Dec 29 19:18 example-2
-rwxrw-r-- 1 nubo nubo 0 Dec 29 19:18 example-1
-rw-rw-r-- 2 nubo nubo 15 Jan 8 14:26 file1.txt
nubo@nubo:~/test$

 


EXAMPLE-7:

To preserve attributes of file or directory while copying

Using -p option, you can preserve the properties of a file or directory as shown below:

$ ls -l sample.txt
-rw-r--r-- 2 bala geek   1038 Jan  9 18:40 sample.txt

$ cp -p sample.txt test/ 

$ ls -l test/sample.txt
-rw-r--r-- 2 bala geek   1038 Jan  9 18:40 test/sample.txt

It is also possible to preserve only the required properties like mode, ownership, timestamps, etc.,

 


Previous Page
Next Page