[Shell command collection file management] Linux copy command cp command usage guide


Shell command column: Full analysis of Linux Shell commands


describe


The cp command is a commonly used file copy command in Linux, used to copy one or more files or directories to a specified target location. Its basic syntax is as follows:

cp [选项] 源文件 目标文件

Among them, 源文件represents the path of the file or directory to be copied, and 目标文件represents the path of the copied file or directory.

Common options include:

  • -r: Recursively copy an entire directory and its subdirectories.
  • -p: Preserve the properties of the source file, including file permissions, owner, group, timestamp, etc.
  • -f: Force copying, that is, overwrite the existing target file.
  • -i: Interactive copy, if the target file already exists, you will be prompted whether to overwrite it.
  • -v: Show detailed copy process.

The following are the main functions and usage of the cp command:

  1. Copy files: Copy one or more files to the specified destination location. For example, cp file1.txt file2.txtcopy file1.txt to file2.txt.

  2. Copy directory: Use -rthe option to recursively copy an entire directory and its subdirectories. For example, cp -r dir1 dir2copy the dir1 directory and all its subdirectories and files to the dir2 directory.

  3. Preserve properties: Use -pthe option to preserve the properties of the source file, including file permissions, owner, group, timestamp, and more. For example, cp -p file1.txt file2.txtcopy file1.txt to file2.txt and keep its properties.

  4. Force copy: Use -fthe option to force copy, that is, overwrite the existing target file. For example, cp -f file1.txt file2.txtcopy file1.txt to file2.txt. If file2.txt already exists, it will be overwritten.

  5. Interactive copy: Use -ithe option to perform interactive copy. If the target file already exists, you will be prompted whether to overwrite it. For example, cp -i file1.txt file2.txtcopy file1.txt to file2.txt. If file2.txt already exists, you will be prompted whether to overwrite it.

  6. Show details: Use -vthe option to show details of the copy process. For example, cp -v file1.txt file2.txtcopy file1.txt to file2.txt and display the copy details.

In addition to the above common options, the cp command also supports some other options. You can man cpview the complete help document through the command.


Syntax format

cp [选项] 源文件 目标文件
cp [选项] 源文件... 目标目录

Parameter Description

  • -rOr --recursive: copy the entire directory recursively.
  • -pOr --preserve: Preserve the properties of the source file, including file permissions, owner, group, timestamp, etc.
  • -fOr --force: Force copying, overwriting the existing target file.
  • -iOr --interactive: interactive copy, prompt whether to overwrite the existing target file.
  • -vOr --verbose: Show detailed copy process.
  • -uOr --update: Copy only newer files in the source file to the target directory.
  • -lOr --link: Create a hard link instead of copying the file.
  • -sOr --symbolic-link: Create a symbolic link instead of copying the file.

error condition

  • If the source file does not exist, an error message is displayed and the copy process is terminated.
  • If the destination file already exists and the -for -iparameter is not used, an error message is displayed and the copy process is terminated.
  • If there are insufficient rights for the copy operation, an error message is displayed and the copy process is terminated.

Precautions

There are some things to note when using the Linux shell’s cp command:

  1. When copying a single file, the target file can be an existing file or a new file name. If the destination file already exists and no -for -iparameter is used, the copy operation will be terminated and the destination file will not be overwritten.

  2. When copying multiple files, the destination file must be a directory. If the target file is an existing directory, the source file will be copied to that directory. If the target file does not exist, the directory is created and then the source file is copied into it.

  3. Use -rthe or --recursiveparameter to recursively copy files in an entire directory and its subdirectories. If this parameter is not used, the cp command will report an error when the source file is a directory.

  4. Use -pthe or --preserveparameter to preserve the properties of the source file, including file permissions, owner, group, timestamp, etc. If this parameter is not used, the copied file will use the default attributes.

  5. Use -fthe or --forceparameter to force copying even if the destination file already exists. Be careful when using this parameter as it will overwrite an existing target file.

  6. Use the -ior --interactiveparameter to perform interactive copying, that is, you will be prompted whether to overwrite the existing target file during the copying process.

  7. Use -vthe or --verboseparameter to display the detailed copy process, including copied file names and destination paths.

  8. Use -uthe or --updateparameter to copy only newer files in the source file to the target directory. If the source and destination files have the same timestamp, no copy operation will occur.

  9. Use the -lor --linkparameter to create a hard link instead of copying the file. A hard link refers to multiple files sharing the same data. If one file is modified, other files will also be affected.

  10. Use the -sor --symbolic-linkparameter to create a symbolic link instead of copying the file. A symbolic link is a shortcut from one file to another file or directory.

  11. During the copying process, if you do not have sufficient permissions to perform the copying operation, an error message will be displayed and the copying process will be terminated.

In short, when using the cp command, you need to pay attention to the existence of the target file, the type of the target file when using recursive copy, the need to retain attributes, the choice of forced copy and interactive copy, the need to display detailed information, and permission issues wait.


underlying implementation

In Linux systems, the cp command is implemented through system calls. System call is a mechanism for interaction between user programs and the operating system kernel. Through system calls, user programs can request the operating system kernel to perform specific operations.

Specifically, when a user enters the cp command in the shell, the shell parses the command and calls the exec system call to execute the cp command. In the exec system call, the executable file of the cp command is loaded into memory and the command line parameters are passed to the executable file.

The executable file of the cp command will parse the command line parameters and perform corresponding operations based on the parameters. In the underlying implementation, the cp command uses the open system call to open the source file, the read system call to read the content of the source file, and then the write system call to write the content to the target file. If a directory needs to be copied, the cp command traverses the files in the directory using the opendir and readdir system calls and calls itself recursively to copy files in subdirectories.

When copying files, the cp command can also use the fcntl system call to set file attributes such as file permissions, owner, group, etc. This preserves the properties of the source file.

It should be noted that the underlying implementation of the cp command does not directly copy the contents of the file, but implements the copy operation by reading the contents of the source file and then writing to the target file. This ensures data integrity during copying and preserves the properties of the source files.

In short, the bottom layer of the cp command is implemented through system calls, and the copy operation is completed by reading the content of the source file and then writing it to the target file.


Example

Example 1

Copy files to the specified directory:

Order:

cp file1.txt /home/user/documents/

Explanation:
Copy the file1.txt file to the /home/user/documents/ directory.

Example 2

Copy an entire directory recursively:

Order:

cp -r dir1 /home/user/documents/

Explanation:
Copy the dir1 directory and all its subdirectories and files to the /home/user/documents/ directory.

Example three

Preserve source file attributes:

Order:

cp -p file1.txt file2.txt

Explanation:
Copy file1.txt to file2.txt, and retain the attributes of file1.txt, including file permissions, owner, group, timestamp, etc.

Example 4

Force copying, overwriting existing target files:

Order:

cp -f file1.txt file2.txt

Explanation:
Copy file1.txt to file2.txt. If file2.txt already exists, it will be overwritten.

Example five

Interactive copy, prompts whether to overwrite the existing target file:

Order:

cp -i file1.txt file2.txt

Explanation:
Copy file1.txt to file2.txt. If file2.txt already exists, you will be prompted whether to overwrite it.

Example 6

Show detailed copy process:

Order:

cp -v file1.txt file2.txt

Explanation:
Copy file1.txt to file2.txt and display the copy details.

Example 7

Copy multiple files to the target directory:

Order:

cp file1.txt file2.txt /home/user/documents/

Explanation:
Copy file1.txt and file2.txt files to the /home/user/documents/ directory.



Conclusion

During our exploration, we have gained an in-depth understanding of the power and wide application of Shell commands. However, learning these techniques is just the beginning. The real power comes from how you integrate them into your daily routine to increase efficiency and productivity.

Psychology tells us that learning is a continuous and active process. So, I encourage you to not only read and understand these commands, but also practice them. Try creating your own commands and gradually master shell programming so that it becomes part of your daily routine.

Also, remember that sharing is a very important part of the learning process. If you found this blog helpful, please feel free to like and leave a comment. Sharing the problems or interesting experiences you encountered when using Shell commands can help more people learn from them.
In addition, I also welcome you to bookmark this blog and come back to check it anytime. Because review and repeated practice are also the keys to consolidating knowledge and improving skills.

最后,请记住:每个人都可以通过持续学习和实践成为Shell编程专家。我期待看到你在这个旅途中取得更大进步!


阅读我的CSDN主页,解锁更多精彩内容:泡沫的CSDN主页

在这里插入图片描述

Guess you like

Origin blog.csdn.net/qq_21438461/article/details/131360933