Cp Command in Linux/Unix With Examples | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

cp is a command in various Unix and Unix-like operating systems for copying files and directories.

cp stands for copy. It creates an exact image of a file on a disk with different file name. cp command require at least two filenames in its arguments.

~/codeFactory$ cp --help 
Usage: cp [OPTION]... [-T] SOURCE DEST
  or:  cp [OPTION]... SOURCE... DIRECTORY
  or:  cp [OPTION]... -t DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
  -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

By default, sparse SOURCE files are detected by a crude heuristic and the
corresponding DEST file is made sparse as well.  That is the behavior
selected by --sparse=auto.  Specify --sparse=always to create a sparse DEST
file whenever the SOURCE file contains a long enough sequence of zero bytes.
Use --sparse=never to inhibit creation of sparse files.

When --reflink[=always] is specified, perform a lightweight copy, where the
data blocks are copied only when modified.  If this is not possible the copy
fails, or if --reflink=auto is specified, fall back to a standard copy.
Use --reflink=never to ensure a standard copy is performed.

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:

  none, off       never make backups (even if --backup is given)
  numbered, t     make numbered backups
  existing, nil   numbered if numbered backups exist, simple otherwise
  simple, never   always make simple backups

As a special case, cp makes a backup of SOURCE when the force and backup
options are given and SOURCE and DEST are the same name for an existing,
regular file.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report cp translation bugs to <https://translationproject.org/team/>
Full documentation at: <https://www.gnu.org/software/coreutils/cp>
or available locally via: info '(coreutils) cp invocation'

cp command works on three principal modes of operation and these operations depend upon number and type of arguments passed in cp command :

1. Two file names : If the command contains two file names, then it copy the contents of 1st file to the 2nd file. If the 2nd file doesn’t exist, then first it creates one and content is copied to it. But if it existed then it is simply overwritten without any warning. So be careful when you choose destination file name.

~/codeFactory$ ll
total 9
drwxr-xr-x 6 user user  10 Aug 24 16:37  ./
drwxr-xr-x 6 user user  14 Sep  4 11:38  ../
d---rwx--- 2 user user   2 Aug 16 16:29  Download/
drwxr-xr-x 2 user user   2 Aug 16 16:29  Photos/
drwxr-xr-x 2 user user   2 Aug 16 16:53 'Photos and Videos'/
drwxr-xr-x 2 user user   2 Aug 16 16:29  Videos/
-rwx------ 1 user user 149 Aug 12 15:32  test.txt*
-rw-r--r-- 1 user user  27 Aug 24 16:37  test2.txt
-rw-r--r-- 1 user user  37 Aug 23 17:19  test3.txt
-rw-r--r-- 1 user user  27 Aug 24 16:26  test4.txt

~/codeFactory$ cp test.txt test1.txt

~/codeFactory$ ll
total 10
drwxr-xr-x 6 user user  11 Sep  4 11:46  ./
drwxr-xr-x 6 user user  14 Sep  4 11:38  ../
d---rwx--- 2 user user   2 Aug 16 16:29  Download/
drwxr-xr-x 2 user user   2 Aug 16 16:29  Photos/
drwxr-xr-x 2 user user   2 Aug 16 16:53 'Photos and Videos'/
drwxr-xr-x 2 user user   2 Aug 16 16:29  Videos/
-rwx------ 1 user user 149 Aug 12 15:32  test.txt*
-rwx------ 1 user user 149 Sep  4 11:46  test1.txt*
-rw-r--r-- 1 user user  27 Aug 24 16:37  test2.txt
-rw-r--r-- 1 user user  37 Aug 23 17:19  test3.txt
-rw-r--r-- 1 user user  27 Aug 24 16:26  test4.txt

2. One or more arguments : If the command has one or more arguments, specifying file names and following those arguments, an argument specifying directory name then this command copies each source file to the destination directory with the same name, created if not existed but if already existed then it will be overwritten, so be careful.

Empty folder
~/codeFactory$ ls Download/

~/codeFactory$ cp test.txt test1.txt Download/

~/codeFactory$ ls Download/
test.txt  test1.txt

3. Two directory names : If the command contains two directory names, cp copies all files of the source directory to the destination directory, creating any files or directories needed. This mode of operation requires an additional option, typically R, to indicate the recursive copying of directories.

~/codeFactory$ ls -R Download/
Download/:
Dir1  test.txt  test1.txt

Download/Dir1:
test.txt

Download1 not exist
~/codeFactory$ cp -R Download/ Download1/

~/codeFactory$ ls -R Download1/
Download1/:
Dir1  test.txt  test1.txt

Download1/Dir1:
test.txt

Using cp with options:

1. -i (interactive): i stands for Interactive copying. With this option system first warns the user before overwriting the destination file. cp prompts for a response, if you press y then it overwrites the file and with any other option leave it uncopied.

~/codeFactory$ cp -i test.txt test1.txt 
cp: overwrite 'test1.txt'? y

2. -b (backup): With this option cp command creates the backup of the destination file in the same folder with the different name and in different format.

~/codeFactory/Download$ ll
total 5
drwxr-xr-x 2 user user   3 Sep  4 11:57 Dir1/
-rwx------ 1 user user 149 Sep  4 11:51 test.txt*
-rwx------ 1 user user 149 Sep  4 11:51 test1.txt*

~/codeFactory/Download$ cp -b test.txt test1.txt

~/codeFactory/Download$ ll
total 6
drwxr-xr-x 2 user user   3 Sep  4 11:57 Dir1/
-rwx------ 1 user user 149 Sep  4 11:51 test.txt*
-rwx------ 1 user user 149 Sep  4 12:08 test1.txt*
-rwx------ 1 user user 149 Sep  4 11:51 test1.txt~*

3. -f (force): If the system is unable to open destination file for writing operation because the user doesn’t have writing permission for this file then by using -f option with cp command, destination file is deleted first and then copying of content is done from source to destination file.

~/codeFactory/Download$ ll
total 6
drwxr-xr-x 2 user user   3 Sep  4 11:57 Dir1/
-rwx------ 1 user user 149 Sep  4 11:51 test.txt*
-r-xr-xr-x 1 user user 149 Sep  4 12:08 test1.txt*
-rwx------ 1 user user 149 Sep  4 11:51 test1.txt~*

User, group and others doesn't have writing permission.

Without -f option, command not executed
~/codeFactory/Download$ cp test.txt test1.txt
cp: cannot create regular file 'test1.txt': Permission denied

~/codeFactory/Download$ cp -f test.txt test1.txt

4. -r or -R: Copying directory structure. With this option cp command shows its recursive behavior by copying the entire directory structure recursively.

~/codeFactory$ cp Download Download1
cp: -r not specified; omitting directory 'Download'

With -r, execute successfully
~/codeFactory$ cp -r Download Download1

5. -p (preserve): With -p option cp preserves the following characteristics of each source file in the corresponding destination file: the time of the last data modification and the time of the last access, the ownership (only if it has permissions to do this), and the file permission-bits.

Note: For the preservation of characteristics you must be the root user of the system, otherwise characteristics changes.

~/codeFactory$ ll test.txt 
-rwx------ 1 user user 149 Aug 12 15:32 test.txt*

~/codeFactory$ cp -p test.txt test1.txt

~/codeFactory$ ll test1.txt 
-rwx------ 1 user user 149 Aug 12 15:32 test1.txt*

Copying using * wildcard: The star wildcard represents anything i.e. all files and directories. Suppose we have many text document in a directory and wants to copy it another directory, it takes lots of time if we copy files 1 by 1 or command becomes too long if specify all these file names as the argument, but by using * wildcard it becomes simple.

Folder1 is empty
~/codeFactory$ ll Folder1/

~/codeFactory$ ll *.txt
-rwx------ 1 user user 149 Aug 12 15:32 test.txt*
-rwx------ 1 user user 149 Aug 12 15:32 test1.txt*
-rw-r--r-- 1 user user  27 Aug 24 16:37 test2.txt
-rw-r--r-- 1 user user  37 Aug 23 17:19 test3.txt
-rw-r--r-- 1 user user  27 Aug 24 16:26 test4.txt

~/codeFactory$ cp *.txt Folder1/

~/codeFactory$ ll Folder1/
total 7
-rwx------ 1 user user 149 Sep  4 12:44 test.txt*
-rwx------ 1 user user 149 Sep  4 12:44 test1.txt*
-rw-r--r-- 1 user user  27 Sep  4 12:44 test2.txt
-rw-r--r-- 1 user user  37 Sep  4 12:44 test3.txt
-rw-r--r-- 1 user user  27 Sep  4 12:44 test4.txt

Leave a comment