Linux系统-基本指令(3)
文章目录
- 迅速了解7条指令(2-)
- 详细学习一个一个的指令(2-25.00)
- ls 指令
- 第一个知识点(啥是文件?)
- 第二个知识点(啥是隐藏文件?)
- pwd 与 cd 指令
- 第三个知识点(`.`与`..`两个目录)
- 第四个知识点(文件的系统结构,绝对与相对路径)
- 第五个知识点(`.`与`..`的意义)
- 第六个知识点(什么是家目录)
简介:这篇文章主要介绍一些Linux初期的一些控制指令,这些指令虽然简单,但却是非常的重要,是学习Linux操作系统的基础
迅速了解7条指令(2-)
pwd
指令:打印显示默认所处的路径
[root@hcss-ecs-28ce ~]# pwd
/root
whoami
指令:当前正在使用的Linux
账号名称
[root@hcss-ecs-28ce ~]# whoami
root
ls
指令:显示在当前路径下所有的文件名称
[root@hcss-ecs-28ce ~]# ls
dir test.c txt.c
mkdir
指令:创建一个目录(文件夹)
[root@hcss-ecs-28ce ~]# ls
dir test.c txt.c
[root@hcss-ecs-28ce ~]# mkdir hello
[root@hcss-ecs-28ce ~]# ls
dir hello test.c txt.c
cd
指令:进入一个目录
[root@hcss-ecs-28ce ~]# pwd
/root
[root@hcss-ecs-28ce ~]# cd dir
[root@hcss-ecs-28ce dir]# pwd
/root/dir
touch
指令:新建一个文件
[root@hcss-ecs-28ce dir]# ls
[root@hcss-ecs-28ce dir]# touch file.c
[root@hcss-ecs-28ce dir]# ls
file.c
rm
指令:删除一个文件
[root@hcss-ecs-28ce dir]# ls
file.c
[root@hcss-ecs-28ce dir]# rm file.c
rm: remove regular empty file ‘file.c’? y
[root@hcss-ecs-28ce dir]# ls
[root@hcss-ecs-28ce dir]#
- 补充总结
- 所有的操作系统无论是Linux,windows还是macOS,最初都是通过指令来进行基本操作,全部都是命令行,是没有图形化界面的
- 虽说现在咱们是通过图形化界面的形式使用windows操作系统,但windows仍然保留了命令行的输入方式
cmd
- Linux操作系统因为开源的缘故主要被企业所接受和使用,而企业中所培养的一般是职业工程师,不是很需要借助图形化界面,
所以其操作方式依旧主流的要求以命令行的形式控制和操作,而且图形化界面也是需要消耗CPU资源的,而企业的后端服务器上也不需要图形化界面显示
详细学习一个一个的指令(2-25.00)
ls 指令
- 该指令可以显示当前目录下的所有普通文件和子目录,可以带上选项,选项可以让同一个指令展现出不同的功能,指令和选项是通过
空格与-
连接 - 举个例子:
ls -l
。该指令是让该路径下的普通文件和子目录显示出详细信息,其中ls
是指令,l
是选项,中间带上一个空格和-
[root@hcss-ecs-28ce ~]# ls
dir hello test.c txt.c
[root@hcss-ecs-28ce ~]# ls -l
total 8
drwxr-xr-x 2 root root 4096 May 27 17:19 dir
drwxr-xr-x 2 root root 4096 May 27 17:13 hello
-rw-r--r-- 1 root root 0 May 27 17:11 test.c
-rw-r--r-- 1 root root 0 May 27 17:11 txt.c
- 命令:
ls -a
,该命令的意思是显示出隐藏文件(可能是目录也可能是普通文件)。命令:ls - a -l
,在显示出隐藏文件的基础上,再显示出该路径下普通文件,目录,隐藏文件更详细的文件属性,其中该指令与选项操作ls -a -l
等同于ls -al
。选项可以组合传入,也可以分开传入
[root@hcss-ecs-28ce hello]# ls -a
. .. file.c
[root@hcss-ecs-28ce hello]# ls -a -l
total 8
drwxr-xr-x 2 root root 4096 May 27 17:53 .
dr-xr-x---. 7 root root 4096 May 27 17:13 ..
-rw-r--r-- 1 root root 0 May 27 17:53 file.c
[root@hcss-ecs-28ce hello]# ls -al
total 8
drwxr-xr-x 2 root root 4096 May 27 17:53 .
dr-xr-x---. 7 root root 4096 May 27 17:13 ..
-rw-r--r-- 1 root root 0 May 27 17:53 file.c
- 命令:
ls -ld
,这个命令带了选项d
,当输入命令ls /root
它会显示目录root
里面的内容,当命令中带上选项d
就会让它只显示该目录本身,不会进入该目录显示目录里面的内容
[root@hcss-ecs-28ce /]# ls -l /root
total 8
drwxr-xr-x 2 root root 4096 May 27 21:09 dir
drwxr-xr-x 2 root root 4096 May 27 18:06 hello
-rw-r--r-- 1 root root 0 May 27 17:11 test.c
-rw-r--r-- 1 root root 0 May 27 17:11 txt.c
[root@hcss-ecs-28ce /]# ls -ld /root
dr-xr-x---. 7 root root 4096 May 27 21:08 /root
第一个知识点(啥是文件?)
先抛出一个疑问,当咱们在windows
或Linux
操作系统中去新建了一个文件,这个文件中没输入任何内容,那这个文件是否要占据空间呢?
直接说结论:要占据空间
- 注意,文件是不等同于文件内容的,文件它包括文件名,文件所占据空间的大小,文件创建的时间等,将这些称作文件属性,而文件属性也是数据,也是要在磁盘中进行存储。
- 文件 = 文件内容 + 文件属性,这两部分都是数据,都要保存起来。这也是为啥你创建一个文件,电脑关机后开机,文件仍然在那个位置没丢失的原因(被磁盘给保存下来了,虽说没有文件内容,但仍有文件属性存在)
第二个知识点(啥是隐藏文件?)
上面不是提到了命令:ls -al
。该命令的意思是显示出隐藏文件,那到底啥是隐藏文件呢?它的存在又有什么意义呢?
- 在Linux中,以
.
开头的文件或目录就把它们称作隐藏文件 - 在不通过命令
ls -al
(这里指的是不带选项a
)是看不到.
和..
这两个目录的,默认是隐藏的
[root@hcss-ecs-28ce hello]# touch .text.c
[root@hcss-ecs-28ce hello]# ls
file.c
[root@hcss-ecs-28ce hello]# ls -a
. .. file.c .text.c
- 在
windows
的文件管理器中也是同样存在隐藏文件这个机制的
- 这个隐藏文件可以放置很重要的数据。比如有可能要删除这些垃圾文件直接会
ctrl + A
。隐藏文件隐藏了,默认是不删除隐藏文件的,这样一定程度上能减少咱们的误操作 ,让你不要把重要的数据给删除 - 在windows,Linux操作系统中一些很重要的系统或应用配置文件都会以隐藏文件的形式放置到隐藏文件夹中
pwd 与 cd 指令
pwd
这个指令很简单,就是打印当前默认所处的路径- 在
windows
系统中路径的表示是这种:D:\软件下载\Visual Studio\Common7\IDE\1033
。在Linux
操作系统中则是这种:/root/hello
- 无论是
/
,还是\
,都将它们称为路径分隔符号,两个路径分隔符之间的,一定是一个目录(文件夹),末端文件可能是一个目录或者是普通文件
[root@hcss-ecs-28ce hello]# pwd
/root/hello
cd
这个指令就是切换所在的路径,cd + 指定路径
,可进入到该指定路径下,它也会跟第三个知识点产生联系
[root@hcss-ecs-28ce /]# pwd
/
[root@hcss-ecs-28ce /]# cd /root/hello
[root@hcss-ecs-28ce hello]# pwd
/root/hello
- 命令:
cd ..
,进入到上一级路径
[root@hcss-ecs-28ce hello]# pwd
/root/hello
[root@hcss-ecs-28ce hello]# cd ..
[root@hcss-ecs-28ce ~]# pwd
/root
- 命令:
cd ~
,无论你现在处于哪条路径,输入该命令就直接回到该家目录下,如果你是root
用户,那你的家目录就是/root
,那如果你是普通用户,那你的家目录就是/home/用户名
[root@hcss-ecs-28ce ~]# cd /root/hello
[root@hcss-ecs-28ce hello]# pwd
/root/hello
[root@hcss-ecs-28ce hello]# cd ~
[root@hcss-ecs-28ce ~]# pwd
/root
- 命令:
cd -
,切换最近的一次路径
[root@hcss-ecs-28ce hello]# pwd
/root/hello
[root@hcss-ecs-28ce hello]# cd /
[root@hcss-ecs-28ce /]# pwd
/
[root@hcss-ecs-28ce /]# cd -
/root/hello
[root@hcss-ecs-28ce hello]# pwd
/root/hello
第三个知识点(.
与..
两个目录)
[root@hcss-ecs-28ce hello]# ls -al
total 8
drwxr-xr-x 2 root root 4096 May 27 18:06 .
dr-xr-x---. 7 root root 4096 May 27 17:13 ..
-rw-r--r-- 1 root root 0 May 27 17:53 file.c
-rw-r--r-- 1 root root 0 May 27 18:06 .text.c
- 看代码片段左边的第一列,如果第一个符号是
d
,那就代表它是一个目录,如果是-
,那就代表它是一个文件 .
指的是当前路径,..
指的是上一级路径,目前先理解到这里即可
[root@hcss-ecs-28ce hello]# pwd
/root/hello
[root@hcss-ecs-28ce hello]# cd .
[root@hcss-ecs-28ce hello]# pwd
/root/hello[root@hcss-ecs-28ce hello]# cd ..
[root@hcss-ecs-28ce ~]# pwd
/root
[root@hcss-ecs-28ce ~]# cd ..
[root@hcss-ecs-28ce /]# pwd
/
第四个知识点(文件的系统结构,绝对与相对路径)
- 当一直
cd ..
发现退回到\
目录就退不了,把这个\
称为根目录。可以把根目录理解为所有文件的祖宗结点,它里面可以包含许多普通文件和目录(文件夹),其中一个目录可以看成是一棵子树的根目录,其中又可以包含普通文件和目录。 - 那这样看来
Linux
的所有文件的系统结构是一个多叉树结构,而且该多叉树每一个非叶子结点都是目录,叶子结点可以看成是一个普通文件或目录
[root@hcss-ecs-28ce dir]# pwd
/root/dir
[root@hcss-ecs-28ce dir]# cd ..
[root@hcss-ecs-28ce ~]# pwd
/root
[root@hcss-ecs-28ce ~]# cd ..
[root@hcss-ecs-28ce /]# pwd
/
[root@hcss-ecs-28ce /]# cd ..
[root@hcss-ecs-28ce /]# pwd
/
- 实际上
windows
操作系统中的C盘和D盘就是一个文件夹,只不过它们两个是以图形化界面的形式呈现出来,方便用户进行文件操作,这不就是Linux
操作系统中的根目录/
- 绝对路径:通过观察多叉树的结点,咱们能发现任意一个结点都只有一个父结点,所以该结点从下往上逆推上去直至根目录,该路径是具有唯一性的,从根目录从上往下找到该结点,该路径也是具有唯一性的。把这种具有唯一性的,定位文件的方式,从根目录开始,叫做绝对路径
[root@hcss-ecs-28ce ~]# ls /root/hello
file.c
[root@hcss-ecs-28ce ~]# ls -al /root/hello
total 8
drwxr-xr-x 2 root root 4096 May 27 18:06 .
dr-xr-x---. 7 root root 4096 May 27 17:13 ..
-rw-r--r-- 1 root root 0 May 27 17:53 file.c
-rw-r--r-- 1 root root 0 May 27 18:06 .text.c
- 相对路径:绝对路径有一个很大的弊端,如果这个多叉树的深度太高了,那该路径就会很长,那通过绝对路径进行操作就将变得无意义了。因此就可以通过相对路径去定位文件,即以相对于我自己的当前路径为参照点,去查找定位目标文件的路径,它也是具有唯一性的,只要具有唯一性,便能以路径的方式去定位任意文件
[root@hcss-ecs-28ce dir]# ls /root
dir hello test.c txt.c
[root@hcss-ecs-28ce dir]# pwd
/root/dir
[root@hcss-ecs-28ce dir]# ls /root/hello
file.c
[root@hcss-ecs-28ce dir]# ls ../hello
file.c
- 那定位文件是采用绝对路径,还是采用相对路径呢?其实在之后的操作过程中,大部分用的都是相对路径这种方式。
一般在配置文件,系统级配置文件当中,可以去采用绝对路径
,因为相对路径往往会因为当前位置(所处路径)不同就会失效,而绝对路径是从根目录从上往下直至该文件,其绝对路径是稳定的
第五个知识点(.
与..
的意义)
- 在任何一个目录中都存在
.
和..
。一个点.
可以理解为指向自己这个结点。二个点..
可以理解为指向父结点,它可以从上级路径进来,那也需要有回退到上级路径的办法啊(cd ..
) - 根目录的一个点和两个点都是指向自己。这也是为什么退到顶了,便一直是
/
目录,因为再怎么回退也是处于根目录这个路径 - 那这个一个点
.
指向自己有啥作用呢?cd .
不是一直处于自己这段路径吗?看起来这个点.
的存在好像是无用之举。 - 实则不然,看下面的这段指令,当想要运行这个可执行程序
a.out
,直接输入指令a.out
,就算你目前处于这个路径也执行不了这个可执行程序,当你输入指令./a.out
时,它就能执行(前提是你得处于a.out这个路径下),这也是一个点.
的作用所在
[root@hcss-ecs-28ce dir]# ls
code.c
[root@hcss-ecs-28ce dir]# cat code.c
#include<stdio.h>
int main()
{printf("hello world\n");return 0;
}
[root@hcss-ecs-28ce dir]# gcc code.c
[root@hcss-ecs-28ce dir]# ls
a.out code.c
[root@hcss-ecs-28ce dir]# a.out
-bash: a.out: command not found
[root@hcss-ecs-28ce dir]# ./a.out
hello world
第六个知识点(什么是家目录)
[root@hcss-ecs-28ce ~]# pwd
/root
[root@hcss-ecs-28ce ~]# whoami
root
[xiao@hcss-ecs-28ce ~]$ pwd
/home/xiao
[xiao@hcss-ecs-28ce ~]$ whoami
xiao
-
上面这两段路径
/root
与/home/xiao
都指的是特定用户的家目录,如果你是root
用户,那初始登录时默认所处的路径就是/root
,如果都是普通用户,那默认所处的路径就是/home/用户名
-
无论是
root
还是普通用户,登录系统后都会处于自己的家目录中,不会到别人的家目录中去(这里指的是普通用户,root
是管理员,是个例外),可以通过指令cd ~
,从自己家目录的任意路径回到家目录(/root
或/home/用户名
) -
那
windows
可不可以创建多用户,有没有家目录,答案是有的 。每去创建一个用户,就会有各自的桌面,各自的图片,下载等,且两个用户之间是互不干扰的,其实桌面它也是个文件夹,只不过windows
系统对桌面这个文件夹进行了可视化,因此就有各种软件图标
-
每个家目录需要保存用户私有信息,开机后就会进入到默认登录的所处的路径,这样系统就会根据你所处的路径去加载出相对应的桌面
-
那可能就会存在疑问,为啥在云服务器上的Linux操作系统的用户上,咋就没有相应的桌面,图片,下载等文件夹呢?这是因为它是一个干净的系统,默认就省去了那些东西(其实企业的后端服务器压根就用不上那些东西),但虚拟机是有这么一套东西的,本质上也是windows这套目录形式