cp command in Linux with Examples


Advertisements

Name

cp - copy files and directories

Synopsis

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

Description

cp command copies files (or, optionally, directories). The copy is completely independent of the original. You can either copy one file to another, or copy arbitrarily many files to a destination directory.

In the first format, when two file names are given, cp command copies SOURCE file to DEST file.

In the second format copy multiple SOURCE(s) to a DIRECTORY.

In the third format, if the ‘--target-directory’ (‘-t’) option is given, ‘cp’ copies each SOURCE file to the specified directory, using the SOURCEs’ names.

Options

cp command accepts the following options.

-a, --archive
   same as -dR --preserve=all

--attributes-only
   don't copy the file data, just the attributes

--backup[=CONTROL]
   make a backup of each existing destination file

-b   like --backup but does not accept an argument

--copy-contents
   copy contents of special files when recursive

-d   same as --no-dereference --preserve=links

-f, --force
   if  an  existing  destination file cannot be opened, remove it and try again (this option is ignored when the -n option is also used)

-i, --interactive
   prompt before overwrite (overrides a previous -n option)

-H   follow command-line symbolic links in SOURCE

-l, --link
   hard link files instead of copying

-L, --dereference
   always follow symbolic links in SOURCE

-n, --no-clobber
   do not overwrite an existing file (overrides a previous -i option)

-P, --no-dereference
   never follow symbolic links in SOURCE

-p   same as --preserve=mode,ownership,timestamps

--preserve[=ATTR_LIST]
   preserve the specified attributes (default: mode,ownership,timestamps), if  possible additional attributes: context, links, xattr, all

--no-preserve=ATTR_LIST
   don't preserve the specified attributes

--parents
   use full source file name under DIRECTORY

-R, -r, --recursive
   copy directories recursively

--reflink[=WHEN]
   control clone/CoW copies. See below

--remove-destination
   remove  each existing destination file before attempting to open it (contrast with --force)

--sparse=WHEN
   control creation of sparse files. See below

--strip-trailing-slashes
   remove any trailing slashes from each SOURCE argument

-s, --symbolic-link
   make symbolic links instead of copying

-S, --suffix=SUFFIX
   override the usual backup suffix

-t, --target-directory=DIRECTORY
   copy all SOURCE arguments into DIRECTORY

-T, --no-target-directory
   treat DEST as a normal file
    
-u, --update
   copy only when the SOURCE file is newer than the destination file or when the destination file is missing

-v, --verbose
   explain what is being done

-x, --one-file-system
   stay on this file system

-Z   set SELinux security context of destination file to default type

--context[=CTX]
   like  -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX

--help display this help and exit

--version
   output version information and exit

Examples

1. Copy file in current directory itself.

$ cp viewers_list.txt users_list.txt

2. Copy a file in ‘backup’ directory

$ cp viewers_list.txt backup/

3. Copy in ‘backup’ directory with different name

$ cp viewers_list.txt backup/viewers_list.bak

4. Use -i option of cp commandfor interactive mode to prompt before overwriting an existing file.

$ cp -i viewers_list.txt users_list.txt
cp: overwrite 'users_list.txt'? y
$ cp --interactive viewers_list.txt users_list.txt
cp: overwrite 'users_list.txt'? y

5. Copy multiple files to a specified directory, in this case 'news'

$ cp current_news.txt headlines.txt cover_story.txt news/

6. Copy multiple files using wild card. It copies all files with extension .txt to 'newsportal' directory.

$ cp *.txt newsportal/

7. To create a backup of the destination file, if that already exists, use -b or --backup option.

$ cp -b viewers_list.txt users_list.txt
$ ls -lt
total 96
-rw-rw-r-- 1 expert expert    69 Jun  4 15:52 users_list.txt
-rw-rw-r-- 1 expert expert    36 Jun  4 15:52 users_list.txt~
-rw-rw-r-- 1 expert expert    69 Jun  4 15:51 viewers_list.txt
...
$ cp --backup=numbered viewers_list.txt users_list.txt
$ ls -lt
total 100
-rw-rw-r-- 1 expert expert    69 Jun  4 15:56 users_list.txt
-rw-rw-r-- 1 expert expert    69 Jun  4 15:52 users_list.txt.~1~
-rw-rw-r-- 1 expert expert    36 Jun  4 15:52 users_list.txt~
-rw-rw-r-- 1 expert expert    69 Jun  4 15:51 viewers_list.txt
...    

8. To create backup with specific suffix, use -S or --sufix= option.

$ cp -S .bak viewers_list.txt users_list.txt
$ cp -v -b --suffix=.bak mount.htm mount_old
'mount.htm' -> 'mount_old' (backup: 'mount_old.bak')

9. To copy files or directories in recursive manner, use -r or -R option.

$ cp -R newsportal archive
$ cp -r newsportal archive
$ cp --recursive newsportal archive

10. To copy only when source file is newer than the destination file we can use -u option.

$ cp -u headlines.txt current\ news/
$ cp --update headlines.txt current\ news/

11. When we do not want to accidently overwrite an existing file, we can use -n option.

$ cp -n headlines.txt currentnews.txt
$ cp --no-clobber headlines.txt currentnews/

12. We can use -s option to create a symbolic link of the file.

$ cp -s current_news.txt cn
$ cp --symbolic-link current_news.txt cn

13. We can use -l optin to create hard link of the file.

$ cp -l current_news.txt shortcut/cn
$ cp --link current_news.txt shortcut/cn

14. We can use -p option to preserve the source file attributes (mode, ownership and timestamps) while copying a file.

$ ls -l viewers_list.txt 
-rw-rw-r-- 1 expert expert 69 Jun  4 15:51 viewers_list.txt
$ cp -p viewers_list.txt backup/
$ ls -l backup/viewers_list.txt 
-rw-rw-r-- 1 expert expert 69 Jun  4 15:51 backup/viewers_list.txt
$ cp --preserve=all viewers_list.txt backup/

15. Create archive files and directories during copy

$ cp -a newsportal/ archive
$ cp --archive newsportal/ archive

16. We can use copy attributes only without data of a file

$ cp --attributes-only viewers_list.txt shortcut/
Advertisements