Day 2: cp usage of common Linux commands

Posted by garblar on Thu, 23 Dec 2021 14:33:12 +0100

1. Command introduction

cp focuses on copying, mainly copying one or more source files or folders to the specified destination files or folders (it can be understood as operating on the window system, copying files or folders and pasting them to the specified location according to their own needs)

2. English meaning memory method

cp = copy

3. Syntax format

  • cp options- T source file destination file
  • cp options Source file catalogue
  • cp options- t directory source file

4. Description of options

  • -r: Copy a folder and all contents under it to another folder
  • -i: Contrary to the - f option, a prompt is given before overwriting the target file to ask the user to confirm whether to overwrite. When you answer y, the target file will be overwritten
  • -f: Overwrite the existing target file without prompting
  • -a: Copy as is and keep all (including time, access rights, file owner, etc.)
  • -p: Preserve the properties of the source file or directory
  • -l: Do not copy files, just generate linked files
  • -v: Displays the actions performed by the command in detail

5. Precautions

  • Source file: by default, the cp command cannot copy folders. If you want to copy folders, you must use the - R option;
  • Target file: when the source file is multiple files, the target file is required to be the specified directory.

6. Example description

6.1 copy one folder and all contents under it to another folder

cp -r /folder1 /folder2

[root@iZbp1d8rn0652ia3bzzmioZ local]# cd test1/
[root@iZbp1d8rn0652ia3bzzmioZ test1]# ls -l
 Total consumption 0
-rw-r--r-- 1 root root 0 11 March 23:00 111111.txt
-rw-r--r-- 1 root root 0 11 March 23:00 1111.txt
-rw-r--r-- 1 root root 0 11 June 23-22:59 111.txt
[root@iZ test1]# cd ../test2/
[root@iZ test2]# ls
[root@iZ test2]# cp -r /usr/local/test1/ /usr/local/test2/
[root@iZ test2]# ls
test1
[root@iZ test2]# cd test1/
[root@iZ test1]# ls
111111.txt  1111.txt  111.txt
[root@iZ test1]#

6.2 copy all contents in one folder to another folder

cp -r /folder1/* /folder2

drwxr-xr-x   2 root root   55 11 March 23:00 test1
drwxr-xr-x   2 root root    6 11 March 23:07 test2
[root@iZ local]# cd test1/
[root@iZ test1]# ls
111111.txt  1111.txt  111.txt
[root@iZ test1]# cd ../test2/
[root@iZ test2]# ls
[root@iZ test2]# cp /usr/local/test1/* /usr/local/test2
[root@iZ test2]# ls
111111.txt  1111.txt  111.txt
[root@iZ test2]#

6.3 when a copy file already exists under the target file, you will be asked whether to perform the operation when overwriting

cp -i /folder1/xxx.txt /folder2

[root@iZ test2]# ls
111111.txt  1111.txt  111.txt
[root@iZ test2]# cd ../test1/
[root@iZ test1]# ls
111111.txt  1111.txt  111.txt
[root@iZ test1]# cp -i 111.txt /usr/local/test2
cp: Overwrite'/usr/local/test2/111.txt'? y
[root@iZ test1]#

6.4 copy as is and keep all

cp -a 1111.txt /usr/local/test2/

[root@iZ test1]# ls -l
 Total consumption 0
-rw-r--r-- 1 root root 0 11 March 23:00 111111.txt
-rw-r--r-- 1 root root 0 11 March 23:00 1111.txt
-rw-r--r-- 1 root root 0 11 June 23-22:59 111.txt
[root@iZ test1]# cp 111.txt /usr/local/test2/
[root@iZ test1]# cd /usr/local/test2/
[root@iZ test2]# ls -l
 Total consumption 0
-rw-r--r-- 1 root root 0 11 March 23:28 111.txt
[root@iZ test1]# ls -l
 Total consumption 0
-rw-r--r-- 1 root root 0 11 March 23:00 111111.txt
-rw-r--r-- 1 root root 0 11 March 23:00 1111.txt
-rw-r--r-- 1 root root 0 11 June 23-22:59 111.txt
[root@iZ test1]# cp -a 1111.txt /usr/local/test2/
[root@iZ test1]# cd /usr/local/test2/
[root@iZ test2]# ls -l
 Total consumption 0
-rw-r--r-- 1 root root 0 11 March 23:00 1111.txt
-rw-r--r-- 1 root root 0 11 March 23:28 111.txt

Well, that's all for today's study! Welcome to the comment area to participate in communication and discussion, and make better learning and progress!

Series recommendation