How to keep the directory structure unchanged in linux folder replication


Designed for programming, automatic code writing robot, free to open. This article introduces the knowledge about how to keep the directory structure unchanged in linux folder replication. Many people will encounter such difficulties in the operation of actual cases. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

In linux, you can use 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, the directory structure can be copied after the folder is copied. The syntax is p --parent original folder target folder.

Operating environment of this tutorial: linux7.3 system and Dell G3 computer.

Linux folder copying can keep the directory structure unchanged. The --parent parameter of cp command can also copy the directory structure when copying files. At this time, the second parameter can only be a directory, not a file name.

Linux cp (copy file) command is mainly used to copy files or directories.


cp [options] source dest


cp [options] source... directory

Parameter description:

-a: This option is usually used when copying a directory. It preserves the link and file attributes, and copies everything under the directory. Its function is equal to the combination of dpR parameters.

-d: Keep the link when copying. The link mentioned here is equivalent to a shortcut in the Windows system.

-f: Overwrite the existing target file without prompting.

-i: Contrary to the -f option, a prompt is given before overwriting the target file, asking the user to confirm whether to overwrite it. When you answer Y, the target file will be overwritten.

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

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

-l: Do not copy files, just generate linked files.

Examples are as follows:

cp --parent ./mex/list.txt ./dir/

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

$ cp –r test/ newtest

Note: Users must use the parameter -r or -R when copying directories with this instruction.

That's all for how to achieve the same directory structure in linux folder replication. Thank you for reading. If you want to know more about the industry, you can pay attention to the Yisu Cloud website, and Xiaobian will output more high-quality practical articles for everyone!

Ask AI for details

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us