Linux popular command cp
is used to copy files and folders. In this tutorial we will look different usage types of cp
command.
Syntax
Syntax of cp
command is like below.
cp [OPTION]... [-T] SOURCE DEST cp [OPTION]... SOURCE... DIRECTORY cp [OPTION]... -t DIRECTORY SOURCE...
Copy
We will just copy our source file to the destination file.
$ ls cat test $ cp cat cat2 $ ls cat cat2 test
We have copied our source file cat into new file cat2
$ cp cat cat2 test/ $ ls test/ cat cat2 ismail
We have copied cat and cat2 files into the test directory
Copy File and Give New Name
While copying the source file name is used as destination filename. If we need to rename newly created/copied file. We have to explicitly specify new name like below.
In this example we will copy file named test
and new file name will be test5
$ cp test test5
Verbose Mode See Details
While copying bulk and a lot of files we may want to see details or save the details into a log file. It can be achieved with -v option like below
$ cp -v cat cat2 bulk ‘cat’ -> ‘bulk/cat’ ‘cat2’ -> ‘bulk/cat2’
Preserve File Attributes
While copying if we do not want to change modification data, time, access list and associated information about file we can use -p flag
$ ls -l total 0 drwxr-xr-x 2 root root 27 Kas 4 01:59 bulk -rw-r--r-- 1 root root 0 Kas 4 01:55 cat -rw-r--r-- 1 root root 0 Kas 4 01:55 cat2 drwxr-xr-x 3 root root 40 Kas 4 01:45 test $ cp -p cat2 cat3 $ ls -l total 0 drwxr-xr-x 2 root root 27 Kas 4 01:59 bulk -rw-r--r-- 1 root root 0 Kas 4 01:55 cat -rw-r--r-- 1 root root 0 Kas 4 01:55 cat2 -rw-r--r-- 1 root root 0 Kas 4 01:55 cat3 drwxr-xr-x 3 root root 40 Kas 4 01:45 test
Copying Multiple Files
Sometimes we need to copy all files in a given path to the other path. In these situations we can use glob *
which is provided by Linux bash. This will copy all contents to the given new path. In this example we will copy all files in this current working directory to the new path named new/
. We can also specify full path like /home/poftut
.
$ cp * new/
Recursive Copy
Normally cp
command copies only files in the current directory. If we have files and folders to copy cp command do not copies sub files and directories. We can enable recursive copy with -r option. This will copy all sub files and folders in given path.
$ cp -r -v bulk builk3 ‘bulk’ -> ‘builk3’ ‘bulk/cat’ -> ‘builk3/cat’ ‘bulk/cat2’ -> ‘builk3/cat2’
Avoid Overwriting
By default cp
command will overwrite same files on the destination path. If we are working in a critical folder we can avoid overwriting with -i . It will ask question is destination has same file or folder.
$ cp -i cat bulk/ cp: overwrite ‘bulk/cat’? y
Copy Symbolic Links
Symbolic links are used to bind differents paths. Normally cp
command do not process the symbolic link contents and do not copy. We can enable copying symbolic link contents with -s
option.
$ cp -s /mnt/d /opt/bak
Create Hard Links
cp
command can be used create hardlinks. Hard Link is link where source and destination files shares same data. Any change one of them will affect all source and destination file. We will provide -l
which is used create hardlinks instead of copying them.
$ cp -l /mnt/d /opt/bak
Create Soft Links
We can also use cp
to create Soft Links. Soft links provides links to the source file and only source file change will affect all soft links.
$ cp -s /mnt/d /opt/bak
Copy Only Newer and Unexisting Files
This feature can be used to copy only files those are newer in the source and unexisting files. This is very useful for backup or sync purposes. We will use -u
option for this feature.
$ cp -u /mnt/d /opt/bak
Prevent Overwrite or No Clobber
We can strictly prevent overwriting files in the destination. We will use n
option in order to no clobber.
$ cp -n /mnt/d /opt/bak
Preserve All File Attributes
Files and folders have some file system and operating system related attributes. While copying some of them are changed according to the destination path. We can preserve these attributes with --preserve=all
option like below.
$ cp --preserve=all /mnt/d /opt/bak
Linux Cp or Copy Command Infografic

1 thought on “Linux Cp or Copy Command”