1. 程式人生 > 其它 >[Linux]cp command in Linux with examples

[Linux]cp command in Linux with examples

cpstands forcopy. This command is used to copy files or group of files or directory. It creates an exact image of a file on a disk with different file name.cpcommand require at least two filenames in its arguments.

Syntax:

cp [OPTION] Source Destination
cp [OPTION] Source Directory
cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory

First and second syntax is used to copy Source file to Destination file or Directory.
Third syntax is used to copy multiple Sources(files) to Directory.

cpcommand 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 of1st fileto the2nd 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.
    cp Src_file Dest_file
    

    Suppose there is a directory namedgeeksforgeekshaving a text filea.txt.
    Example:

    $ ls
    a.txt
    
    $ cp a.txt b.txt
    
    $ ls
    a.txt  b.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 !!.
    cp Src_file1 Src_file2 Src_file3 Dest_directory
    

    Suppose there is a directory namedgeeksforgeekshaving a text filea.txt, b.txtand a directory namenewin which we are going to copy all files.
    Example:

    $ ls
    a.txt  b.txt  new
    
    Initially new is empty
    $ ls new
    
    $ cp a.txt b.txt new
    
    $ ls new
    a.txt  b.txt
    

    Note:For this case last argumentmustbe adirectoryname. For the above command to work,Dest_directorymust exist becausecpcommand won’t create it.

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

    In the above command,cpbehavior depend upon whetherDest_directoryis exist or not. If theDest_directorydoesn’t exist, cp creates it and copies content ofSrc_directoryrecursively as it is. But if Dest_directory exists then copy ofSrc_directorybecomes sub-directory underDest_directory.

Options:

There are many options ofcpcommand, here we will discuss some of the useful options:
Suppose a directory namedgeeksforgeekscontains two files having some content named asa.txtandb.txt. This scenario is useful in understanding the following options.

$ ls geeksforgeeks
a.txt  b.txt

$ cat a.txt
GFG

$ cat b.txt
GeeksforGeeks

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

$ cp -i a.txt b.txt
cp: overwrite 'b.txt'? y

$ cat b.txt
GFG

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

$ ls
a.txt  b.txt

$ cp -b a.txt b.txt

$ ls
a.txt  b.txt  b.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-foption withcpcommand, destination file is deleted first and then copying of content is done from source to destination file.

$ ls -l b.txt
-r-xr-xr-x+ 1 User User 3 Nov 24 08:45 b.txt

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

Without -f option, command not executed
$ cp a.txt b.txt
cp: cannot create regular file 'b.txt': Permission denied

With -f option, command executed successfully
$ cp -f a.txt b.txt

4. -r or -R:Copying directory structure. With this optioncpcommand shows its recursive behavior by copying the entire directory structure recursively.
Suppose we want to copygeeksforgeeksdirectory containing many files, directories intogfgdirectory(not exist).

$ ls geeksforgeeks/
a.txt  b.txt  b.txt~  Folder1  Folder2

Without -r option, error
$ cp geeksforgeeks gfg
cp: -r not specified; omitting directory 'geeksforgeeks'

With -r, execute successfully
$ cp -r geeksforgeeks gfg

$ ls gfg/
a.txt  b.txt  b.txt~  Folder1  Folder2

5. -p(preserve):With-poptioncppreserves 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 theroot userof the system, otherwise characteristics changes.

$ ls -l a.txt
-rwxr-xr-x+ 1 User User 3 Nov 24 08:13 a.txt

$ cp -p a.txt c.txt

$ ls -l c.txt
-rwxr-xr-x+ 1 User User 3 Nov 24 08:13 c.txt

As we can see above botha.txtandc.txt(created by copying) have same characteristics.

Examples:

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.

Initially Folder1 is empty
$ ls
a.txt  b.txt  c.txt  d.txt  e.txt  Folder1

$ cp *.txt Folder1

$ ls Folder1
a.txt  b.txt  c.txt  d.txt  e.txt

https://youtu.be/ngx4G10XY2s?list=PLqM7alHXFySFc4KtwEZTANgmyJm3NqS_L