Linux:: [Basic Commands:: File and Directory Operations: (7)]:: cp: Copy the specified file or directory and copy the specified file to the specified path (and rename)

Foreword: This article is the content of the basic Linux operation chapter!
The environment I use is based on Tencent Cloud Server: CentOS 7.6 64bit.


Study set:


Directory index:
1. Basic syntax and functions
2. Description of common options
3. Documents Basic copy operation
- - 3.1 Copy the specified file to the current directory
- - 3.2 Copy the specified file to the specified directory
- - 3.3 Copy the specified file to the upper-level directory
4. Basic directory copy operations
- - 4.1 Test direct copy of the directory
- - 4.2 Optional option [-r] to realize directory copy
- - 4.3 Copy directories and files
5. Recommended related articles or series


1. Basic syntax and functions

grammar:

  • cp [options] source file or directory target file or directory (already exists!)
  • The target file can be given a new name!

Function:

  • Copy a file or directory

illustrate:

  • The cp command is used to copy files or directories. If two or more files or directories are specified at the same time, and the final destination is an existing directory, it will copy all previously specified files or directories to this directory. If multiple files or directories are specified at the same time, and the final destination is not an existing directory, an error message will appear.

2. Description of common options

  • -f or --force forcibly copies a file or directory, regardless of whether the destination file or directory already exists
  • -i or --interactive Ask the user before overwriting the file
  • -r recursively processes files and subdirectories in the specified directory together. If the form of the source file or directory does not belong to a directory or symbolic link, it will be treated as an ordinary file.
  • -R or --recursive recursive processing, processing files and subdirectories in the specified directory together

3. Common options and tests

3.1 Copy specified files to the current directory
[Mortal@VM-12-16-centos ~]$ ls
StudyingOrder_Linux  test1  test2  test3  test_mkdir
[Mortal@VM-12-16-centos ~]$ mkdir test_cp
[Mortal@VM-12-16-centos ~]$ ls
StudyingOrder_Linux  test1  test2  test3  test_cp  test_mkdir
[Mortal@VM-12-16-centos ~]$ cd test_cp

/* 同时创建两个目录一个源文件所在目录,一个拷贝目的地所在目录 */
[Mortal@VM-12-16-centos test_cp]$ mkdir origin_dir destination_dir
[Mortal@VM-12-16-centos test_cp]$ ls
destination_dir  origin_dir

/* 创建源文件并用重定向方式写入信息(以后会介绍) */
/* 创建源文件 */
[Mortal@VM-12-16-centos test_cp]$ touch origin_dir/test_origin.txt
/* 用重定向方式写入信息 */
[Mortal@VM-12-16-centos test_cp]$ echo "hello" > origin_dir/test_origin.txt 

/* 执行拷贝操作:不指定拷贝目录即表示拷贝到当前所在目录 */
[Mortal@VM-12-16-centos test_cp]$ cp origin_dir/test_origin.txt copy.txt

[Mortal@VM-12-16-centos test_cp]$ ls
copy.txt  destination_dir  origin_dir

/* 查看拷贝文件的内容! */
[Mortal@VM-12-16-centos test_cp]$ cat copy.txt 
hello

3.2 Copy the specified file to the specified directory
/*
	将 origin_dir 目录下的 test_origin.txt 文件
    拷贝到:destination_dir 目录下!
*/
/* 操作指令: */
[Mortal@VM-12-16-centos test_cp]$ cp origin_dir/test_origin.txt destination_dir/copy.txt

/* 查看拷贝文件内容 */
[Mortal@VM-12-16-centos test_cp]$ cat destination_dir/copy.txt 
hello

3.3 Copy the specified file to the upper-level directory
/* 查看当前所在路径 */
[Mortal@VM-12-16-centos test_cp]$ pwd
/home/Mortal/test_cp

/* 执行操作: */
[Mortal@VM-12-16-centos test_cp]$ cp origin_dir/test_origin.txt ../copy.txt

/* 返回上级并查看目录: */
[Mortal@VM-12-16-centos test_cp]$ cd ..
[Mortal@VM-12-16-centos ~]$ pwd
/home/Mortal

/* 查看是否存在拷贝文件 */
[Mortal@VM-12-16-centos ~]$ ls
copy.txt(这)  StudyingOrder_Linux  test1  test2  test3  test_cp  test_mkdir

4. Modify file creation time

4.1 Basic operation test

Conclusion:You cannot copy the directory directly!

[Mortal@VM-12-16-centos test_cp]$ ls
copy.txt  destination_dir  origin_dir

[Mortal@VM-12-16-centos test_cp]$ cp origin_dir origin_dir_backup
cp: omitting directory ‘origin_dir’		

/* 无法直接操作非空目录 */

/* 测试空目录拷贝 */
[Mortal@VM-12-16-centos test_cp]$ mkdir test
[Mortal@VM-12-16-centos test_cp]$ ls
copy.txt  destination_dir  origin_dir  test
[Mortal@VM-12-16-centos test_cp]$ cp test test_backup
cp: omitting directory ‘test’

4.2 Optional [-r] to implement directory copy

Conclusion: Use -r to copy the directory, and files in non-empty directories will also be copied!


Question: Will single directory/multi-level directories in non-empty directories be copied?

/* 执行拷贝:空目录! */
[Mortal@VM-12-16-centos test_cp]$ cp -r test test_backup
[Mortal@VM-12-16-centos test_cp]$ ls
copy.txt  destination_dir  origin_dir  test  test_backup

/* 执行拷贝:非空目录! */
[Mortal@VM-12-16-centos test_cp]$ cp -r origin_dir origin_dir_backup
[Mortal@VM-12-16-centos test_cp]$ ls
copy.txt  destination_dir  origin_dir  origin_dir_backup  test  test_backup

/* 查看目录下子文件是否拷贝 */
[Mortal@VM-12-16-centos test_cp]$ ls origin_dir_backup/
test_origin.txt
[Mortal@VM-12-16-centos test_cp]$ ls origin_dir
test_origin.txt

4.3 Copy directories and files

Answer the question: Will single directories/multi-level directories in non-empty directories be copied?

Answer: Use: cp -r... to directly copy all the contents in the specified directory!

[Mortal@VM-12-16-centos test_cp]$ rm -r test_backup

[Mortal@VM-12-16-centos test_cp]$ mkdir -p test/a/aa/aaa
[Mortal@VM-12-16-centos test_cp]$ touch test/t1.txt
[Mortal@VM-12-16-centos test_cp]$ touch test/a/aa/aaa/t2.txt
[Mortal@VM-12-16-centos test_cp]$ tree test
test
|-- a
|   `-- aa
|       `-- aaa
|           `-- t2.txt
`-- t1.txt

3 directories, 2 files
[Mortal@VM-12-16-centos test_cp]$ cp -r test test_backup
[Mortal@VM-12-16-centos test_cp]$ ls
copy.txt  destination_dir  origin_dir  origin_dir_backup  test  test_backup
[Mortal@VM-12-16-centos test_cp]$ tree test_backup/
test_backup/
|-- a
|   `-- aa
|       `-- aaa
|           `-- t2.txt
`-- t1.txt

3 directories, 2 files

5. Recommendations for related articles or series

1. Linux learning directory collection ;


2. Linux:: [Basic Instructions:: File and Directory Operations: (4)]:: mkdir:: Create Directory: Specify the path to create a single directory and create a multi-level directory at once< a i=1>;
3. Linux:: [Basic commands:: File and directory operations: (5)]:: touch: Create ordinary files, modify file creation time and other basic operations ;
4. Linux:: [Basic commands:: File and directory operations: (6)]:: rmidr / rm: delete empty directories, delete Non-empty directory and file deletion instructions;


Guess you like

Origin blog.csdn.net/weixin_53202576/article/details/130958423