Home > Operation and Maintenance > Linux Operation and Maintenance > How to copy a Linux folder to keep the directory structure unchanged

How to copy a Linux folder to keep the directory structure unchanged

WBOY
Release: 2022-07-11 15:46:51
Original
3433 people have browsed it

In Linux, you can use the cp command to assign folders and the directory structure remains unchanged; the cp command is mainly used to copy files or directories. When the parameter of the command is set to "parent", you can copy After the folder, copy the directory structure, the syntax is "p --parent original folder target folder".

How to copy a Linux folder to keep the directory structure unchanged

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

How to keep the directory structure unchanged when copying Linux folders

Use the --parent parameter of the cp command to copy the directory structure when copying files. At this time, the second parameter only Can be a directory, not a file name.

Linux cp (English full spelling: copy file) command is mainly used to copy files or directories.

Syntax

cp [options] source dest
Copy after login

or

cp [options] source... directory
Copy after login

Parameter description:

  • -a: This option is usually used when copying a directory, it Preserve links, file attributes, and copy all contents of the directory. Its effect is equal to the dpR parameter combination.

  • -d: Keep the link when copying. The links mentioned here are equivalent to shortcuts in Windows systems.

  • -f: Overwrite an existing target file without giving a prompt.

  • -i: Contrary to the -f option, a prompt is given before overwriting the target file, asking the user to confirm whether to overwrite. The target file will be overwritten when answering y.

  • -p: In addition to copying the contents of the file, the modification time and access permissions are also copied to the new file.

  • -r: If the given source file is a directory file, all subdirectories and files in the directory will be copied.

  • -l: Do not copy files, just generate link files.

The example is as follows:

cp --parent ./mex/list.txt ./dir/
Copy after login

Use the command cp to copy all the files in the current directory test/ to the new directory newtest, enter the following command:

$ cp –r test/ newtest
Copy after login

Note: When users use this command to copy a directory, they must use the parameter -r or -R.

Recommended learning: Linux video tutorial

The above is the detailed content of How to copy a Linux folder to keep the directory structure unchanged. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template