Get the App
SLTechnology News&Howtos  ›  Network Security  › 

The use of cp

Shulou Source: shulou.com Published: 2022-06-01 02:27:01 10月04日 Update

Cp copies files and directories

Cp SRC DEST

SRC is a file:

If the target does not exist:

Create a new DEST and populate the contents of SRC into the DEST

If the target exists:

If DEST is a file: overwrite the contents of SRC to DEST

For security reasons, the-I option is recommended for the cp command

If DEST is a directory:

Create a new file with the same name as the original file under DEST, and populate the contents of SRC into the new file

SRC...: multiple files

DEST must exist and be a directory. Other situations will cause errors.

SRC is the directory: use option at this time:-r

If DEST does not exist:

Then create the specified directory and copy all the files in the SRC directory to DEST

If DEST exists:

If DEST is a file: error reported

If DEST is a directory:

-I: interactive

-r,-R: recursively copy the directory and all its internal contents

-a: archiving, equivalent to-dR--preserv=all

-d:--no-dereference-- preserv=links does not copy the original file, only the link name

-- preserv [= ATTR_LIST] various attributes

Mode: permission

Ownership: belong to the main group

Timestamp: timestamp

Links: link

Xattr:

Context: environment

All: all of them

-p: equivalent-- preserv=mode,ownership,timestamp

-v:-- verbose to view the replication process. Do not add-v for background execution.

-f:-- force coercion

Practice

1. Use the alias command to back up all files under the / etc/ directory to a separate new directory under / testdir/ every day. The new directory format is required to be backupYYYY-mm-dd, and the backup process is visible.

2. First create the / testdir/rootdir directory, and then copy all the files under / root to this directory, and request to retain the original permissions.

Cp-r-preserv=mode / root/* / testdir/rootdir

This command cannot be executed because / root/ in / root/* represents files or directories below root, and * represents all files and directories, because / testdir/rootdir is a file or directory that has not yet been created, and all files cannot be copied to directories or files that do not exist, and existing files cannot be copied successfully, only existing directories can be copied successfully.

Tags: Files directories contents commands success representatives backups permissions goals procedures links and will security reason interactive aliases only background multiple attributes Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Xiaomi Shulou Information Docker OPPO Reno