cp Command in Linux with Examples
cp Command ( COPY )
cp command is used to copy the files and directories is one of the common tasks in every operating system. In Linux we use the cp command, which is used to create the exact copy of the files and directories.
Syntax
cp < options> < source> < Destination>
To copy a file without any option
To copy a file from one location to another within the same user without using any other options.
Example
[root@linuxhelp test]# cp file1.txt /home/user1/Music/
[root@linuxhelp ~]# ls -l /home/user1/Music/
-rw-r--r-- 1 root root 0 Jan 22 14:57 file1.txt
drwxr-xr-x 2 root root 4096 Jan 22 14:57 test
Here, the file1.txt file is simply copied from the directory called ' test' to outside of that directory, without deleting the source file.
Example
[root@linuxhelp test]# cp file1.txt file2.txt file3.txt /home/user1/Music/
[root@linuxhelp test]# ls -l /home/user1/Music/
total 4
To copy multiple file
To copy multiple files at the same time from one directory to another use cp command.
-rw-r--r-- 1 root root 0 Jan 22 15:19 file1.txt -rw-r--r-- 1 root root 0 Jan 22 15:19 file2.txt -rw-r--r-- 1 root root 0 Jan 22 15:19 file3.txt drwxr-xr-x 2 root root 4096 Jan 22 15:18 test
Here, the three files ' file1.txt file2.txt file3.txt' is copied from ' test' directory to outside of that directory.
To copy a directory
To copy a directory is little different. A specific option is to be used.
Example
[root@linuxhelp test]# ls -l drwxr-xr-x 2 root root 4096 Jan 22 15:25 test-new [root@linuxhelp Music]# cp -r test1 /home/user1/Pictures/ [root@linuxhelp Music]# ls -l /home/user1/Pictures/ drwxr-xr-x 2 root root 4096 Jan 22 15:26 test1
Here, the ' test-new' directory is copied from ' test' directory to outside of that directory.
Options:
r or R - recursive. It should be used for copying directory either it may be empty or not.
To archive files
Archiving the files is that copying the exact contents of the directory to another location including the symbolic links.
Example
[root@linuxhelp test1]# ls -l -rw-r--r-- 1 root root 0 Jan 22 15:37 file.txt lrw-r--r-- 1 root root 33 Jan 22 15:39 file1.txt &rarr /home/user1/Music/file1.txt [root@linuxhelp Music]# cp -a test1/ /home/user1/Pictures/ [root@linuxhelp Music]# ls -l /home/user1/Pictures/ lrw-r--r-- 1 root root 33 Jan 22 15:39 file1.txt &rarr /home/user1/Music/file1.txt
Here, the ' test1' directory is archived into ' Pictures' including the files and the symbolic links.
Options:
a &ndash archive, this option is used to archive the contents into the another directory.
To show the track of what is done
The process of copying can be tracked using the specific option in cp command.
Example
[root@linuxhelp test]# cp -v *.txt /home/user1/Pictures/ `file1.txt' -> `/home/user1/Pictures/file1.txt' `file2.txt' -> `/home/user1/Pictures/file2.txt' `file.txt' -> `/home/user1/Pictures/file3.txt' [root@linuxhelp test]# ls -l /home/user1/Pictures/ total 0 -rw-r--r-- 1 root root 0 Jan 22 15:54 file1.txt -rw-r--r-- 1 root root 0 Jan 22 15:54 file2.txt -rw-r--r-- 1 root root 0 Jan 22 15:54 file3.txt
Here, the files copying from current directory to ' Pictures' directory is tracked.
Option:
v &ndash To track the current work.
To copy only if source file is new
To copy only the files which is updated or edited recently, a specific option is to be used.
Example
[root@linuxhelp test]# ls -l total 4 -rw-r--r-- 1 root root 0 Jan 22 15:53 file1.txt -rw-r--r-- 1 root root 0 Jan 22 15:53 file2.txt -rw-r--r-- 1 root root 0 Jan 22 16:31 file3.txt -rw-r--r-- 1 root root 0 Jan 22 16:31 file4.txt [root@linuxhelp test]# ls -l /home/user1/Pictures/ total 4 -rw-r--r-- 1 root root 0 Jan 22 15:53 file1.txt -rw-r--r-- 1 root root 0 Jan 22 15:53 file2.txt -rw-r--r-- 1 root root 0 Jan 22 16:31 file3.txt [root@linuxhelp test]# vim file.txt [root@linuxhelp test]# cp -vu *.txt /home/user1/Pictures/ `file.txt' -> `/home/user1/Pictures/file.txt' [root@linuxhelp test]# ls -l /home/user1/Pictures/ total 4 -rw-r--r-- 1 root root 0 Jan 22 15:53 file1.txt -rw-r--r-- 1 root root 0 Jan 22 15:53 file2.txt -rw-r--r-- 1 root root 0 Jan 22 16:31 file3.txt -rw-r--r-- 1 root root 12 Jan 22 16:31 file4.txt
Since the ' file.txt' file is edited recently, this file is alone copied.
Option
u &ndash to copy only recently edited file.
To use interactive mode
In this mode, if the destination directory contains the same contents in source or to delete the contents, it will ask the confirmation. User have to answer the question either by pressing ' y' (yes) or ' n' (no).
Example
[root@linuxhelp test]# cp -i *.txt /home/user1/Pictures/ cp: overwrite `/home/user1/Pictures/file1.txt' ? y cp: overwrite `/home/user1/Pictures/file2.txt' ? y cp: overwrite `/home/user1/Pictures/file3.txt' ? y cp: overwrite `/home/user1/Pictures/file4.txt' ? y [root@linuxhelp test]# ls -l total 3 -rw-r--r-- 1 root root 0 Jan 22 14:56 file1.txt -rw-r--r-- 1 root root 0 Jan 22 15:18 file2.txt -rw-r--r-- 1 root root 0 Jan 22 15:18 file3.txt -rw-r--r-- 1 root root 0 Jan 22 15:18 file4.txt
Here, while copying the same contents it enters the interactive mode to check for verification of overide.
Option:
i &ndash Interactive mode.
Force copying
-f option is used to force the copying process. If the destination file is not opened, it will try again.
Example
[root@linuxhelp test]# cp -f *.txt -v /home/user1/Public/
`file1.txt' -> `/home/user1/Public/file1.txt'
`file2.txt' -> `/home/user1/Public/file2.txt'
`file3.txt' -> `/home/user1/Public/file3.txt'
`file4.txt' -> `/home/user1/Public/file4.txt'
Here, the .txt files are copied forcefully to ' Public' directory.
To create backup date of each copied file
The file which is copied will create the backup of the same file in the specified location with tilde (~) symbol at the last.
Example
[root@linuxhelp test]# cp --backup=simple -v *.txt ../../Pictures/ `file1.txt' -> `../../Pictures/file1.txt' (backup: `../../Pictures/file1.txt~' ) `file2.txt' -> `../../Pictures/file2.txt' (backup: `../../Pictures/file2.txt~' ) `file3.txt' -> `../../Pictures/file3.txt' (backup: `../../Pictures/file3.txt~' ) [root@linuxhelp test]# ls -l /home/user1/Pictures/ total 3 -rw-r--r-- 1 root root 0 Jan 22 17:33 file1.txt~ -rw-r--r-- 1 root root 0 Jan 22 17:33 file2.txt~ -rw-r--r-- 1 root root 0 Jan 22 17:33 file3.txt~
Option:
- - backup option is given with some other values as follows:
none , off : never backups (even --backup is given)
numbered , t : to notify numbered backups
simple , never : always make simple backups
To remove destination before copy
cp command will find the same name in destination folder. cp command will remove before copying.
Example
[root@linuxhelp test]# cp --remove-destination -v *.txt home/user1/Pictures/
removed `../../Pictures/file1.txt'
`file1.txt' -> `../../Pictures/file1.txt'
removed `../../Pictures/file2.txt'
`file2.txt' -> `../../Pictures/file2.txt'
removed `../../Pictures/file3.txt'
`file3.txt' -> `../../Pictures/file3.txt'
Here, the *.txt files in destination ' Pictures' directory, it will delete in the destination first and then copies the new one to that directory.
To create hard link instead of copying them
cp command will copy the same file in the destination path it means the inode value is same as source and destination.
It only works on the files but not directory
Example
[root@linuxhelp test]# ls &ndash lvi *.txt 525040 -rw-r--r--. 1 root root 0 Feb 26 12:39 file1.txt 525242 -rw-r--r--. 1 root root 0 Feb 26 12:39 file2.txt 525243 -rw-r--r--. 1 root root 0 Feb 26 12:39 file3.txt [root@linuxhelp test]# cp &ndash l *.txt /home/user1/Pictures [root@linuxhelp test]# ls &ndash lvi /home/user1/Pictures 525040 -rw-r--r--. 1 root root 0 Feb 26 12:39 file1.txt 525242 -rw-r--r--. 1 root root 0 Feb 26 12:39 file2.txt 525243 -rw-r--r--. 1 root root 0 Feb 26 12:39 file3.txt [root@localhost Music]# ls &ndash lvi 524967 drwxr-xr-x. 2 root root 4096 Feb 26 12:25 test 524999 drwxr-xr-x. 2 root root 4096 Feb 26 12:31 test1 [root@linuxhelp Music]#cp -rl test /home/user1/Music/test1/ [root@linuxhelp Music]# ls -lvi /home/user1/Music/test1/ 525039 drwxr-xr-x. 2 root root 4096 Feb 26 12:32 test
To create a symbolic link to file instead of copying
Symbolic link can done current working directory only. Symbolic link means link file for the source file. We make symbolic link for /home/user1/Music/test/file1.txt this file in current working directory.
Example
[root@linuxhelp test1]# cp -s /home/user1/Music/test/file1.txt file12.txt
[root@linuxhelp test1]# ls -l
lrwxrwxrwx. 1 root root 32 Feb 26 12:48 file12.txt -> /home/user1/Music/test/file1.txt
Copy without following symbolic link in source
cp command will copy of link file. When cp command found a file with symbolic link. This command will make still destination as a symbolic link.
Example
[root@linuxhelp test1]# cp -P file12.txt /home/user1/Music/test1/file21.txt
[root@linuxhelp test1]# ls -l /home/user1/Music/test1/file21.txt
lrwxrwxrwx. 1 root root 32 Feb 26 14:08 /home/user1/Music/test1/file21.txt -> /home/user1/Music/test/file1.txt
Copying with following symbolic link in source
This command will copy the same file in source file it means not created the link file. It knows in file size copied file size is bigger then the source file.
Example
[root@linuxhelp test1]# cp -L file12.txt file21.txt
[root@linuxhelp test1]# ls -l
lrwxrwxrwx. 1 root root 32 Feb 26 13:00 file12.txt -> /home/user1/Music/test/file1.txt
-rw-r--r--. 1 root root 0 Feb 26 14:25 file21.txt
Now you can see the result in the copied file in normal file format.
Copy file to given full path
This command will help you how to copy with given full path.
Example
[root@linuxhelp test]# cp /home/user1/Music/test/file1.txt /home/user1/Music/test1/filec.txt
[root@linuxhelp test]# ls -l /home/user1/Music/test1/
lrwxrwxrwx. 1 root root 32 Feb 26 13:00 file12.txt -> /home/user1/Music/test/file1.txt
-rw-r--r--. 1 root root 0 Feb 26 14:25 file21.txt
-rw-r--r--. 1 root root 243 Feb 26 15:06 filec.txt
Comments ( 0 )
No comments available