[Linux] See "cp command", "rm command", "mv command" for the first time

1. cp command

Copy a file or directory: Rebuild a copy of the file or directory (source) that needs to be copied, andSave as a new file or directory.

cp命令格式:

cp [options]...source file or directory...destination file or directory

常用选项:

-f、-i、-p、-r

options illustrate
-f When overwriting the target file or directory with the same name, no reminder will be issued, and the copy will be forced directly.
-i Prompt the user to confirm when overwriting the target file or directory with the same name.
-p When copying, keep the attributes of the source file, such as permissions, owners, and time stamps, unchanged.
-r This option must be used when copying directories, which means recursively copy all files and subdirectories.
[root@clr /opt/gzy]# ls #查看当前目录下的test1.txt
abc  ky28  test1.txt
[root@clr /opt/gzy]# echo 123456 > test1.txt  #将123456写到文件test1.txt中
[root@clr /opt/gzy]# cat test1.txt  #查看test1.txt文件
123456
[root@clr /opt/gzy]# ll
总用量 4
drwxr-xr-x. 3 root root 51 327 18:41 abc
drwxr-xr-x. 2 root root  6 327 18:13 ky28
-rw-r--r--. 1 root root  7 327 19:19 test1.txt
[root@clr /opt/gzy]# cp test1.txt ly.txt  #复制test1.txt,并重命名为ly.txt
[root@clr /opt/gzy]# ll
总用量 8
drwxr-xr-x. 3 root root 51 327 18:41 abc
drwxr-xr-x. 2 root root  6 327 18:13 ky28
-rw-r--r--. 1 root root  7 327 19:21 ly.txt
-rw-r--r--. 1 root root  7 327 19:19 test1.txt
[root@clr /opt/gzy]# cat ly.txt  #查看文件ly.txt
123456
[root@clr /opt/gzy]# rm -f test1.txt  #删除源文件test1.txt
[root@clr /opt/gzy]# ll
总用量 4
drwxr-xr-x. 3 root root 51 327 18:41 abc
drwxr-xr-x. 2 root root  6 327 18:13 ky28
-rw-r--r--. 1 root root  7 327 19:21 ly.txt
[root@clr /opt/gzy]# cat ly.txt #查看ly.txt文件
123456
[root@clr /opt/gzy]# cp ly.txt ~/lyg11.txt #将ly.txt文件复制到root用户的家目录中,并重新命名为lyg11.txt
[root@clr /opt/gzy]# cd  #切换到root目录
[root@clr ~]# ll
总用量 16
-rw-------. 1 root root 1832 323 00:54 anaconda-ks.cfg
drwxr-xr-x. 6 root root 4096 327 16:37 gzy
-rw-r--r--. 1 root root 1880 323 01:05 initial-setup-ks.cfg
-rw-r--r--. 1 root root    7 327 19:23 lyg11.txt
drwxr-xr-x. 2 root root    6 323 01:07 公共
drwxr-xr-x. 2 root root    6 323 01:07 模板
drwxr-xr-x. 2 root root    6 323 01:07 视频
drwxr-xr-x. 2 root root    6 323 01:07 图片
drwxr-xr-x. 2 root root    6 323 01:07 文档
drwxr-xr-x. 2 root root    6 323 01:07 下载
drwxr-xr-x. 2 root root    6 323 01:07 音乐
drwxr-xr-x. 2 root root    6 323 01:07 桌面
[root@clr ~]# cat lyg11.txt  #查看lyg11.txt文件中的内容
123456
[root@clr ~]# cd /opt/gzy/ #切换到/opt/gzy/目录
[root@clr /opt/gzy]# cp -i ly.txt ~/lyg11.txt #复制同名文件时,提醒
cp:是否覆盖"/root/lyg11.txt"? n  #不覆盖
[root@clr /opt/gzy]# cat ~/lyg11.txt  #用户家目录中原有lyg11.txt文件中的内容为123456
123456
[root@clr /opt/gzy]# cat ly.txt #查看ly.txt文件中内容
123456
[root@clr /opt/gzy]# vi ly.txt #修改ly.txt文件
[root@clr /opt/gzy]# cp -i ly.txt ~/lyg11.txt  #将ly.txt文件复制到root用户家目录中,并重命名为lyg11.txt
cp:是否覆盖"/root/lyg11.txt"? y  #同名覆盖
[root@clr /opt/gzy]# cd  #切换到root用户家目录
[root@clr ~]# cat lyg11.txt  #查看lyg11.txt文件内容,是ly.txt文件修改后的内容
abcghhjkkkk`123456
[root@clr ~]# cd /opt/gzy/
[root@clr /opt/gzy]# cat ly.txt #查看文件ly.txt文件中的内容
abcghhjkkkk`123456
[root@clr ~]# type cp
cp 是 `cp -i' 的别名
[root@clr ~]# cd /opt/gzy/
[root@clr /opt/gzy]# cp -f ly.txt ~/123.txt #将ly.txt复制到root用户的家目录下,并重命名为123.txt
[root@clr ~]# touch 123.txt  #在root用户家目录下创建文件123.txt
[root@clr ~]# cd /opt/gzy/
[root@clr /opt/gzy]# cp -f ly.txt ~/123.txt
cp:是否覆盖"/root/123.txt/ly.txt"? ^C  #cp -f的功能并未发生作用,没有同名复制提醒
[root@clr /opt/gzy]# vi ly.txt    #编辑文件ly.txt
[root@clr /opt/gzy]# \cp -f ly.txt ~/123.txt  #\反斜杠,起到取消cp -i的别名功能
[root@clr /opt/gzy]# /usr/bin/cp -f ly.txt ~/123.txt  #使用cp这个外部命令的完整路径,进行复制,也可以取消cp -i的别名功能

注意:
cp is the alias of cp -i, there are three ways to not use the alias function of cp -i: cancel the alias (alias cp); use the full path of the external command cp, enter /usr/bin/cp; \cp also Can cancel the alias function of cp;

———————————————————— cp -p command ——————————————————————

insert image description here
———————————————————— cp -r command ——————————————————————

insert image description here

[root@clr /opt]# ll #查看/opt目录
总用量 0
drwxr-xr-x. 2 root root 22 327 18:14 clr
drwxr-xr-x. 2 root root  6 327 09:40 file1,txt
drwxr-xr-x. 2 root root  6 327 09:40 file2,txt
drwxr-xr-x. 4 root root 43 327 22:43 gzy
drwxr-xr-x. 2 root root  6 1031 2018 rh
[root@clr /opt]# touch {1,2,3}.txt   #创建1.txt和2.txt以及3.txt这三个文件
[root@clr /opt]# ll
总用量 0
-rw-r--r--. 1 root root  0 327 23:09 1.txt
-rw-r--r--. 1 root root  0 327 23:09 2.txt
-rw-r--r--. 1 root root  0 327 23:09 3.txt
drwxr-xr-x. 2 root root 22 327 18:14 clr
drwxr-xr-x. 2 root root  6 327 09:40 file1,txt
drwxr-xr-x. 2 root root  6 327 09:40 file2,txt
drwxr-xr-x. 4 root root 43 327 22:43 gzy
drwxr-xr-x. 2 root root  6 1031 2018 rh
[root@clr /opt]# cp -r clr/ {1..3}.txt -r file1,txt/ gzy/  #复制目录clr、文件1.txt、2.txt和3.txt,以及目录file1,txt到/opt/gzy/目录中
cp:是否覆盖"gzy/clr/ky28.txt"? y
cp:是否覆盖"gzy/1.txt"? y
cp:是否覆盖"gzy/2.txt"? y
cp:是否覆盖"gzy/3.txt"? y
[root@clr /opt]# ll
总用量 0
-rw-r--r--. 1 root root   0 327 23:09 1.txt
-rw-r--r--. 1 root root   0 327 23:09 2.txt
-rw-r--r--. 1 root root   0 327 23:09 3.txt
drwxr-xr-x. 2 root root  22 327 18:14 clr
drwxr-xr-x. 2 root root   6 327 09:40 file1,txt
drwxr-xr-x. 2 root root   6 327 09:40 file2,txt
drwxr-xr-x. 6 root root 110 327 23:12 gzy
drwxr-xr-x. 2 root root   6 1031 2018 rh
[root@clr /opt]# cd gzy  #切换到目录gzy中
[root@clr /opt/gzy]# ll
总用量 4
-rw-r--r--. 1 root root   0 327 23:12 1.txt
-rw-r--r--. 1 root root   0 327 23:12 2.txt
-rw-r--r--. 1 root root   0 327 23:12 3.txt
drwxr-xr-x. 3 root root  51 327 18:41 abc
drwxr-xr-x. 2 root root  22 327 23:12 clr
drwxr-xr-x. 2 root root   6 327 23:12 file1,txt
drwxr-xr-x. 2 root root   6 327 18:13 ky28
-rw-r--r--. 1 root root 991 327 22:43 ly.txt
[root@clr /opt/gzy]# cd ../  #回到/opt/gzy/目录的上一个目录
[root@clr /opt]# ls -R gzy/  #以递归方式显示gzy目录
gzy/:
1.txt  2.txt  3.txt  abc  clr  file1,txt  ky28  ly.txt

gzy/abc:
123  h-abc.txt  test1.txt

gzy/abc/123:
11

gzy/abc/123/11:
22

gzy/abc/123/11/22:
33

gzy/abc/123/11/22/33:

gzy/clr:
ky28.txt

gzy/file1,txt:

gzy/ky28:

2. rm command

delete file or directory

delete specified file or directory

rm命令格式:rm [options] file or directory to remove

常用选项:

-f、-i、-r

options illustrate
-f Do not remind when deleting files or directories, but force delete them directly.
-i Prompt the user for confirmation when deleting a file or directory. (y means to delete, n means not to delete)
-r This option must be used when deleting a directory, meaning to recursively delete the entire directory tree (should be used with caution).

注意:
Do not directly delete existing directories or configuration files in the system to avoid unexpected failures.

————————————————————— rm -f command ————————————————————

[root@clr /opt]# rm 1.txt
rm:是否删除普通空文件 "1.txt"?^C
[root@clr /opt]# type rm
rm 是 `rm -i' 的别名
[root@clr /opt]# rm -f 1.txt  #rm -f强制删除1.txt

——————————————————————— rm -rf command ————————————————————

[root@clr /opt]# rm -r gzy  #递归删除gzy这个目录
rm:是否进入目录"gzy"? y
rm:是否进入目录"gzy/abc"? ^C
[root@clr /opt]# rm -rf gzy  #强制删除gzy这个目录

In the production environment, you need to back up before deleting to prevent irreparable losses;

[root@clr /opt]# cp clr/ clr_bak -r  #复制目录clr并重命名为clr_bak
[root@clr /opt]# ll
总用量 0
-rw-r--r--. 1 root root  0 327 23:09 2.txt
-rw-r--r--. 1 root root  0 327 23:09 3.txt
drwxr-xr-x. 2 root root 22 327 18:14 clr
drwxr-xr-x. 2 root root 22 327 23:38 clr_bak
drwxr-xr-x. 2 root root  6 327 09:40 file1,txt
drwxr-xr-x. 2 root root  6 327 09:40 file2,txt
drwxr-xr-x. 2 root root  6 1031 2018 rh
[root@clr /opt]# cp 3.txt{,.bak}  #复制3.txt并重命名为3.txt.bak
[root@clr /opt]# ll
总用量 0
-rw-r--r--. 1 root root  0 327 23:09 2.txt
-rw-r--r--. 1 root root  0 327 23:09 3.txt
-rw-r--r--. 1 root root  0 327 23:39 3.txt.bak
drwxr-xr-x. 2 root root 22 327 18:14 clr
drwxr-xr-x. 2 root root 22 327 23:38 clr_bak
drwxr-xr-x. 2 root root  6 327 09:40 file1,txt
drwxr-xr-x. 2 root root  6 327 09:40 file2,txt
drwxr-xr-x. 2 root root  6 1031 2018 rh

3. mv command

move a file or directory

Shift the specified file or directory location.

If the destination location is the same as the source location, it is equivalent to performing a rename operation.

mv命令格式:mv [options]...source file or directory...destination file or directory

[root@clr /opt]# mv 2.txt ~  #将2.txt文件移动到root用户的家目录
[root@clr /opt]# cd    #切换到root用户的家目录
[root@clr ~]# ll
总用量 24
drwxr-xr-x. 2 root root   20 327 22:41 123.txt
-rw-r--r--. 1 root root    0 327 23:09 2.txt
-rw-r--r--. 1 root root    7 327 22:38 abc.txt
-rw-------. 1 root root 1832 323 00:54 anaconda-ks.cfg
drwxr-xr-x. 6 root root 4096 327 16:37 gzy
-rw-r--r--. 1 root root 1880 323 01:05 initial-setup-ks.cfg
-rw-r--r--. 1 root root   19 327 19:42 lyg11.txt
-rw-r--r--. 1 root root  991 327 22:43 ly.txt
drwxr-xr-x. 2 root root    6 323 01:07 公共
drwxr-xr-x. 2 root root    6 323 01:07 模板
drwxr-xr-x. 2 root root    6 323 01:07 视频
drwxr-xr-x. 2 root root    6 323 01:07 图片
drwxr-xr-x. 2 root root    6 323 01:07 文档
drwxr-xr-x. 2 root root    6 323 01:07 下载
drwxr-xr-x. 2 root root    6 323 01:07 音乐
drwxr-xr-x. 2 root root    6 323 01:07 桌面
[root@clr ~]# cd /opt/
[root@clr /opt]# ll
总用量 0
-rw-r--r--. 1 root root  0 327 23:09 3.txt
-rw-r--r--. 1 root root  0 327 23:39 3.txt.bak
drwxr-xr-x. 2 root root 22 327 18:14 clr
drwxr-xr-x. 2 root root 22 327 23:38 clr_bak
drwxr-xr-x. 2 root root  6 327 09:40 file1,txt
drwxr-xr-x. 2 root root  6 327 09:40 file2,txt
drwxr-xr-x. 2 root root  6 1031 2018 rh
[root@clr /opt]# mv rh/ clr/   #将目录rh移动到clr目录中
[root@clr /opt]# ll
总用量 0
-rw-r--r--. 1 root root  0 327 23:09 3.txt
-rw-r--r--. 1 root root  0 327 23:39 3.txt.bak
drwxr-xr-x. 3 root root 32 327 23:48 clr
drwxr-xr-x. 2 root root 22 327 23:38 clr_bak
drwxr-xr-x. 2 root root  6 327 09:40 file1,txt
drwxr-xr-x. 2 root root  6 327 09:40 file2,txt
[root@clr /opt]# ls clr/
ky28.txt  rh
[root@clr /opt]# mv 3.txt 33.txt  #将3.txt重命名为33.txt
[root@clr /opt]# ll
总用量 0
-rw-r--r--. 1 root root  0 327 23:09 33.txt
-rw-r--r--. 1 root root  0 327 23:39 3.txt.bak
drwxr-xr-x. 3 root root 32 327 23:48 clr
drwxr-xr-x. 2 root root 22 327 23:38 clr_bak
drwxr-xr-x. 2 root root  6 327 09:40 file1,txt
drwxr-xr-x. 2 root root  6 327 09:40 file2,txt

[root@clr /opt]# mv 33.txt /root/3344.txt  #将/oopt目录下的33.txt文件移动到/root/目录下,并重命名为3344.txt
[root@clr /opt]# cd
[root@clr ~]# ll
总用量 24
drwxr-xr-x. 2 root root   20 327 22:41 123.txt
-rw-r--r--. 1 root root    0 327 23:09 2.txt
-rw-r--r--. 1 root root    0 327 23:09 3344.txt
-rw-r--r--. 1 root root    7 327 22:38 abc.txt
-rw-------. 1 root root 1832 323 00:54 anaconda-ks.cfg
drwxr-xr-x. 6 root root 4096 327 16:37 gzy
-rw-r--r--. 1 root root 1880 323 01:05 initial-setup-ks.cfg
-rw-r--r--. 1 root root   19 327 19:42 lyg11.txt
-rw-r--r--. 1 root root  991 327 22:43 ly.txt
drwxr-xr-x. 2 root root    6 323 01:07 公共
drwxr-xr-x. 2 root root    6 323 01:07 模板
drwxr-xr-x. 2 root root    6 323 01:07 视频
drwxr-xr-x. 2 root root    6 323 01:07 图片
drwxr-xr-x. 2 root root    6 323 01:07 文档
drwxr-xr-x. 2 root root    6 323 01:07 下载
drwxr-xr-x. 2 root root    6 323 01:07 音乐
drwxr-xr-x. 2 root root    6 323 01:07 桌面

————————————— rename command ——————————————
insert image description here

Guess you like

Origin blog.csdn.net/cailirong123/article/details/129798382