A day linux commands: cp (8)

cp

cp command is used to copy one or more source files or directory to the specified file or directory object. Or the next directory already exists it can copy the source file into a single file name designated specific file. cp command also supports copying multiple files at the same time, when a copy multiple files, object files argument must be a directory that already exists, or an error

format

cp [options] [parameters]

Parameter options

parameter Remark
-a And the effect of this parameter specifying "-dpR" the same parameters;
-d When copying symbolic link, the target file or directory is a symbolic link is also established, and the connection point with the original source file or directory of files or directories;
-f Forced copy a file or directory, regardless of whether the target file or directory already exists;
-i First ask the user before overwriting existing files;
-l Establish hard connection to the source file, rather than copying files;
-p Properties keep the source file or directory;
-R/r Recursive processing, all files and subdirectories in the specified directory treated together;
-s Creates a symbolic link to the source file, rather than copying files;
-u When compared to only update the target file in the source file modification time after using this parameter names correspond to each other or when the destination file does not exist, just copy the file;
-S When you back up files with the specified suffix "SUFFIX" instead of the default file extension;
-b Before the target file will overwrite the existing file backup target;
-v Detailed display operation command execution.
  • Source File: Develop list of source files. By default, cp command can not copy a directory, if you want to copy a directory, you must use -Roption;
  • Target file: specify the target file. When the "source" for multiple file, requires "destination file" specified directory.

Examples

  • Copy a single file to the destination directory, the file does not exist in the target file

    命令: cp myFile ./targetDir/myFile

[root@VM_0_9_centos test]# mkdir targetDir && cp myFile ./targetDir/myFile
[root@VM_0_9_centos test]# tree
.
|-- myFile
`-- targetDir
    `-- myFile  #复制的文件
  • Copy a single file to the target directory and renamed

    命令: cp myFile ./targetDir/myFile2

[root@VM_0_9_centos test]# mkdir targetDir && cp myFile ./targetDir/myFile2
[root@VM_0_9_centos test]# tree
.
|-- myFile
`-- targetDir
    `-- myFile2  #复制的文件
  • When the target file exists, it will be asked whether to overwrite

    命令:cp myFile ./targetDir/myFile2

[root@VM_0_9_centos test]# cp myFile ./targetDir/myFile2 
cp: overwrite ?./targetDir/myFile2?. y
  • Copy the entire directory

    命令: cp -a sourceDir targetDir/ cp -r sourceDir targetDir

 [root@VM_0_9_centos test]# tree
.
|-- sourceDir
|   |-- myFile1
|   `-- myFile2
`-- targetDir

2 directories, 2 files
[root@VM_0_9_centos test]# cp -a sourceDir targetDir
[root@VM_0_9_centos test]# tree
.
|-- sourceDir
|   |-- myFile1
|   `-- myFile2
`-- targetDir
    `-- sourceDir
        |-- myFile1
        `-- myFile2

3 directories, 4 files
  • When the copied files, generate only Connection document, rather than true copy

    命令:cp -s test.txt test_link.txt

root@VM_0_9_centos test]# ll
total 8
drwxr-xr-x 2 root root 4096 Oct 27 23:05 sourceDir
drwxr-xr-x 3 root root 4096 Oct 27 23:38 targetDir
[root@VM_0_9_centos test]# touch test.txt
[root@VM_0_9_centos test]# cp -s test.txt test_link.txt
[root@VM_0_9_centos test]# ll
total 8
drwxr-xr-x 2 root root 4096 Oct 27 23:05 sourceDir
drwxr-xr-x 3 root root 4096 Oct 27 23:38 targetDir
lrwxrwxrwx 1 root root    8 Oct 27 23:44 test_link.txt -> test.txt
-rw-r--r-- 1 root root    0 Oct 27 23:44 test.txt
  • Cover copy files, ways to avoid the emergence of a confirmation prompt y

    命令:\cp -a sourceDir targetDir

[root@VM_0_9_centos test]# tree
.
|-- sourceDir
|   |-- myFile1
|   `-- myFile2
`-- targetDir
    `-- sourceDir
        |-- myFile1
        `-- myFile2
3 directories, 4 files

[root@VM_0_9_centos test]# ls -l targetDir/sourceDir/
total 0
-rw-r--r-- 1 root root 0 Oct 27 23:05 myFile1
-rw-r--r-- 1 root root 0 Oct 27 23:05 myFile2

[root@VM_0_9_centos test]# cp -a sourceDir targetDir
cp: overwrite ?.argetDir/sourceDir/myFile1?. y
cp: overwrite ?.argetDir/sourceDir/myFile2?. y
[root@VM_0_9_centos test]# \cp -a sourceDir targ    etDir
[root@VM_0_9_centos test]# tree
.
|-- sourceDir
|   |-- myFile1
|   `-- myFile2
`-- targetDir
    `-- sourceDir
        |-- myFile1
        `-- myFile2

3 directories, 4 files

reference

Guess you like

Origin www.cnblogs.com/DiDi516/p/11749940.html