A detailed introduction to Linux file copying

Mondo Technology Updated on 2024-02-21

1. Why do you need to learn the cp copy file command first in the Linux introductory chapter?

Because Linux is different from Windows, Windows is a graphical interface, and it is easy to copy and paste. Linux Server Edition does not have a graphical interface for many things, and even if it does, it is often impossible to copy due to permission issues, and it is more likely to be copied using shell command line mode.

2. Detailed explanation of the basic usage and parameters of cp commands

cp source file destination path.

Among them,Source FileIndicates the file or files to be copied (separated by spaces); Destination Path Directory NameIndicates the location to which the file was copied.

Commonly used options include:

a: The effect of this parameter is specified at the same time"-dpr"The parameters are the same;

d: When copying a symbolic connection, the target file or directory is also established as a symbolic connection, and points to the original file or directory connected to the source file or directory;

f: forcibly copying a file or directory, regardless of whether the target file or directory already exists;

i: Ask the user before overwriting the existing file;

l: Hard-connect the source file instead of copying it;

p: preserves the properties of the source file or directory;

r r: recursive processing, which processes all files in the specified directory together with the subdirectories;

s: Establish a symbolic connection to the source file instead of copying the file;

u: If you use this parameter, you will copy the file only when the change time of the source file is newer than the target file, or if the object file with the corresponding name does not exist.

s: When backing up files, replace the default suffix of the file with the specified suffix "suffix";

b: Back up the target file before overwriting the existing file target;

v: Displays the operations performed by the command in detail.

3. Instance demo

Example 1: Placing a single filemy.inifrom/home/user/directory copied to/etc/init.dTable of Contents:

cp /home/user/my.ini /etc/init.d

cur_path=`dirname $0`

Quickly copy j**a, tomcat, mysql, etc. to the specified directory, and force the installation to be overwritten.

4. Detailed explanation of important parameters -rf

In practice, remember the most important parameter rf, r for recursion, f for forced override, but during the upgrade execution, a prompt will still pop up, if there are many files, you need to process yes one by one.

In the following way, a new directory will be created in MySQL, so you need to pay attention.

cp -rf /home/huf/mysql /usr/local/mysql

Use ls -l usr local to see what's underneath the folder.

5. The role and significance of slashes in CP

cp -rf $cur_path/cyweb /usr/local

The meaning of adding a slash in front of cp is in many Linux systemsbashrc file.

alias cp='cp -i', which leads to the default of ordinary cp will keep popping up prompts, to confirm, which is not conducive to making shell batch processing**.

vim ~/.bashrc

In the specific learning process, you can type the cp command several times in the shell to master the meaning of the parameters, which is the most basic command of all command words in linux, and it feels very easy to learn linux if you master it.

Related Pages