Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Linux's method of copying files using the cp command

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

How does linux copy files using the cp command? This problem may be often seen in our daily study or work. I hope you can gain a lot from this question. The following is the reference content that the editor brings to you, let's take a look at it!

The cp copy command is very useful in linux. It can copy a file (directory) under a specified path to another file or directory.

Linux cp copy command

The Linux cp command is mainly used to copy files or directories.

Basic syntax:

Cp [options] Source file destination file (folder)

Parameter [options] description:

-a: this option is usually used when copying a directory, preserving links, file properties, and copying everything in the directory. Its function is equal to the combination of dpR parameters.

-d: keep the link when copying. The links mentioned here are equivalent to shortcuts in the Windows system.

-f: overwrite existing target files without prompting.

-I: contrary to the-f option, prompt the user to confirm whether or not to overwrite the target file before overwriting, and the target file will be overwritten when answering "y".

-p: copy the modification time and access rights to the new file in addition to copying the contents of the file.

-r: if the source file given is a directory file, all subdirectories and files under that directory will be copied.

-l: do not copy the file, just generate the linked file.

Example of linux cp command

1. Copy a file to the target file (folder)

Format:

Cp source file destination file (folder)

Example: copy the 1.txt file to the testDir folder and the 2.text of the testDir folder

Description:

If the file is copied to the folder, the file name remains the same, but if it is copied into the file, the file name changes. If the target file already exists or contains a file with the same name in the destination folder, the target file or the file with the same name in the destination folder will be overwritten after the copy.

2. Copy multiple files to the target file (folder) at the same time

Format (two):

Cp Source File 1 Source File 2 destination folder

Multiple files listed can be copied to the destination folder at once, and each file is directly separated by spaces.

Cp files * destination folder

Using the wildcard character *, you can copy all files that meet the criteria to the target folder; for example: * .txt is to copy all files ending with ".txt" to the target folder.

Example: copy 1.txt, 2.txt, 3.txt files to the testDir folder

3. Copy a folder to the target file

Format:

Cp-r source folder destination folder

Note: the "- r" parameter must be added to the replication folder, otherwise the "cp: omitting directory" error will occur.

Thank you for reading! After reading the above, do you have a general idea of how linux uses the cp command to copy files? I hope the content of the article will be helpful to all of you. If you want to know more about the relevant articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report