Linux self-study journey-basic commands (5)

Linux self-study journey-basic commands (5)


Preface

1. In the previous section, we talked about "hard links" and soft links, mainly about the difference between the two links and some of their basic working principles. If you haven't read it yet, please click the link below to watch:
hard link and soft link

2. Then in this section we will continue to talk about the commands that can be operated on both directories and files


Tip: The following is the content of this article

1. Commands that can operate on both directories and files

1.rm command

Remember the rmdir command we talked about before. Its function is only to delete an empty directory. We said that it is not commonly used in daily use. After all, the function is limited, so in general, we will have a command to delete it. Commands for empty directories, rm can delete directories or files

  • Command name: rm
  • The full name of the command: remove files or directories
  • Location: /bin/rm
  • Execution authority: all users
  • Function description: delete directory or file
命令格式:
rm [选项] 文件或目录名称
常用选项:
-r:递归删除(用于删除目录,删目录必须加该选项)
-f:强制删除(就是直接删除一个文件或目录,系统不会提醒你是否删除什么的)
-i:交互式删除(就是你每次删除一个东西,系统都会问你是否删除--这个选项你用rm
的时候它本身是自带这个-i功能的)

Give an example of deleting files and deleting directories:

(删除文件)
[root@localhost ceshi]# ls -hl
总用量 4.0K
-rw-r--r--. 1 root root    0 1月  19 02:42 jj
drwxr-xr-x. 2 root root 4.0K 1月  19 02:42 ml
[root@localhost ceshi]# rm jj 
rm:是否删除普通空文件 "jj"?y
[root@localhost ceshi]# ls
ml
[root@localhost ceshi]# 

(如上,我用rm xx的方式后面随便接了一个文件,然后系统会询问是否删除,
y=是,n=否,是之后就直接删除了)
(删除目录)
[root@localhost ceshi]# ll -lh
总用量 4.0K
drwxr-xr-x. 2 root root 4.0K 1月  19 02:42 ml
[root@localhost ceshi]# rm ml/
rm: 无法删除"ml/": 是一个目录
[root@localhost ceshi]# rm -r ml/
rm:是否删除目录 "ml"?y
[root@localhost ceshi]# ls
[root@localhost ceshi]# 

(如上,我直接rm 后面就接一个目录的话他会显示无法删除,但是我加了-r之后他就可以删了。)


2.cp command

The cp command can be used to copy files or copy directories

  • Command name: cp
  • The full name of the command: copy files and directories
  • Location: /bin/cp
  • Execution authority: all users
  • Function description: copy files or directories
命令格式
cp [选项] 源文件 目标文件
常用选项:
-a:相当于-dpr选项的集合
-d:如果复制的文件是软链接,那么复制到目标路径的时候也是软链接
-i:如果你复制一个文件到目标路径,目标路径已经有一模一样文件名的文件的话会提示是否覆盖
-p:复制后的文件保留所有文件属性(包括所有者,所属组,权限和时间)
-r:递归复制(用于复制目录)

Give two examples:

(直接复制)
[root@localhost ceshi]# 
[root@localhost ceshi]# ls
a
[root@localhost ceshi]# cp a /tmp/ceshi/a.txt
[root@localhost ceshi]# ls
a  a.txt
[root@localhost ceshi]# cp xx xx:后面接你要复制的文件和你要复制到的地方就行,然后在复制的时候
是可以对文件进行改名的,如上我复制a到/tmp/ceshi/下,并改名为a.txt)
(加了-a的cp[root@localhost ceshi]# ll
总用量 0
-rw-r--r--. 1 root root 0 1月  19 02:49 a
-rw-r--r--. 1 root root 0 1月  19 03:17 a.txt
[root@localhost ceshi]# cp -a a /tmp/ceshi/a.sh
[root@localhost ceshi]# ll
总用量 0
-rw-r--r--. 1 root root 0 1月  19 02:49 a
-rw-r--r--. 1 root root 0 1月  19 02:49 a.sh
-rw-r--r--. 1 root root 0 1月  19 03:17 a.txt
[root@localhost ceshi]#

(加了-a选项的cp就相当于拥有-d:复制软链接,-r:递归复制,-p:保留文件属性,
我们可以看到复制出来的a.sh和源文件a一模一样的修改时间)


3.mv command

Since we have a command to copy files or directories, we will definitely cut the command. Copy and cut two steps. The mv command is mainly used for file movement (can be regarded as cutting)

  • Command name: rm
  • The full name of the command: move files
  • Location: /bin/mv
  • Execution authority: all users
  • Function description: file or directory movement
命令格式
mv [选项] 源文件 移动到的目标路径
常用选项:
-f:如果移动到的目标路径中有同名文件则不提示是否覆盖,直接覆盖。
-i:如果目标路径中有同名文件,则显示是否覆盖(默认不加-i,自带-i效果)
-v:显示详细信息

Just give two examples:

(直接mv[root@localhost ceshi]# ls
a  a.sh  a.txt
[root@localhost ceshi]# mv a /tmp/aaaaaaa
[root@localhost ceshi]# ls
a.sh  a.txt
[root@localhost ceshi]# ls /tmp/
aaaaaaa  ceshi  snljh2  snljh3  yum.log
[root@localhost ceshi]# mv后面接要移动的文件,然后在接要移动到哪,如上,我将a移动到/tmp/下,
然后将他改名为aaaaaaa,最后ls查看/tmp/目录,发现有移动后的文件)
(带-v的mv[root@localhost ceshi]# ls
a.sh  a.txt
[root@localhost ceshi]# mv -v a.sh /tmp/
"a.sh" -> "/tmp/a.sh"
[root@localhost ceshi]# ls /tmp/
a.sh  ceshi  snljh2  snljh3  yum.log
[root@localhost ceshi]# ls
a.txt
[root@localhost ceshi]# 

(如上,我加了-v选项的话下面会显示我的这个文件移动到哪了,也就是显示过程)

to sum up

So this time we talked about three commands { 1.rm: command to delete files or directories 2.cp: copy command 3.mv: cut command }



In the next section, I will talk about the basic permissions under Linux. I am Jiehua. See you next time.

Guess you like

Origin blog.csdn.net/qq313088385/article/details/112798654