Linux文件与shell的基本概念


Linux文件与shell的基本概念

shell外壳程序

用户与操作系统进行交互,是通过shell外壳程序进行的。用户不能直接与操作系统进行交互,一是因为直接与操作系统交互,使用难度大,二是容易引发错误,导致操作系统崩溃。计算机的发明者在发明计算机时考虑到了这个问题,于是在操作系统与用户之间提供了shell外壳程序,让用户通过shell外壳程序间接的与操作系统进行交互。

在这里插入图片描述

注意:shell外壳程序是所有外壳程序的统称,不是指的具体某一种外壳程序。

查看当前Linux的shell外壳程序

[slowstep@localhost ~]$ echo $BASH
/bin/bash
[slowstep@localhost ~]$ ls -al /bin/bash
-rwxr-xr-x. 1 root root 964536 Nov 25  2021 /bin/bash

centos7下的shell外壳程序是bash

Linux文件

文件类型

查看Linux中文件的详细信息使用ll命令

[slowstep@localhost dirtest]$ ll
total 36
-rwxrwxr-x. 1 slowstep slowstep  8360 Aug 12 10:52 a.out
drwxrwxr-x. 2 slowstep slowstep    30 Aug 11 22:24 dir
-rw-rw-r--. 1 slowstep slowstep   164 Aug 12 11:10 test.c
-rw-rw-r--. 1 slowstep slowstep 16927 Aug 12 11:11 test.i

文件类型(Linux中第一个字符表示文件类型)

- //普通文件
d //目录
p //管道文件
b //块设备
c //字符设备
l //链接文件

其中最常见的是d-

文件后缀

在Linux中,文件后缀不作为区分文件类型的标准。

[slowstep@localhost dir]$ ll
total 12
-rwxrwxr-x. 1 slowstep slowstep 8376 Aug 11 22:24 a.out
[slowstep@localhost dir]$ mv a.out a.txt
[slowstep@localhost dir]$ ll
total 12
-rwxrwxr-x. 1 slowstep slowstep 8376 Aug 11 22:24 a.txt
//即使更改文件后缀,文件类型还是'-'

Linux操作系统不以文件后缀区分文件类型,但是Linux操作系统下的软件就不一定了。

[slowstep@localhost dirtest]$ ls
test.c
[slowstep@localhost dirtest]$ gcc test.c 
[slowstep@localhost dirtest]$ ls
a.out  test.c
[slowstep@localhost dirtest]$ mv test.c test.txt
[slowstep@localhost dirtest]$ ls
a.out  test.txt
[slowstep@localhost dirtest]$ gcc test.txt
test.txt: file not recognized: File format not recognized  //不能识别文件格式
collect2: error: ld returned 1 exit status

文件权限(重点)

权限的介绍

Linux中的文件有权限的概念,文件并不是想改就改,想看就看的,需要一定的权限。文件的权限分为3种,读(r),写(w),执行(x)。

文件不仅有权限,还有对应的人:拥有者,所属组,other

其中拥有者表示这个文件是谁的,所属组表示这个文件属于哪一个组。除了拥有者和所属组之外的其他人,都是文件的other

权限详解

查看文件

[slowstep@localhost dir]$ ll
total 12
-rwxrwxr-x. 1 slowstep slowstep 8376 Aug 11 22:24 tmp.txt

tmp.txt这个文件前面有一行-rw-rw-r--
把这一行拆开,第一个-表示这个文件是一个普通文件,不是一个目录
第一个-后面的首个rw-是文件的拥有者具有的权限
第一个-后面的第二个rw-是文件的所属组具有的权限
第二个rw-后面的r--是other具有的权限
拥有者具有rw-的权限,这表示拥有者具有读写文件的权限,但是没有执行文件的权限
所属组具有rw-的权限,这表示所属组具有读写文件的权限,但是没有执行文件的权限
other具有r--的权限,这表示other只具有读文件的权限,没有写文件和执行文件的权限

怎么证明拥有者具有rw-的权限?
我们去掉拥有者的rw权限,然后看一下拥有者是否具有读写权限。
Linux中,修改文件权限的命令是chmod

[slowstep@localhost dir]$ ll
total 0
-rw-rw-r--. 1 slowstep slowstep 0 Aug 12 20:35 tmp.txt
[slowstep@localhost dir]$ chmod u-rw tmp.txt 
[slowstep@localhost dir]$ ll
total 0
----rw-r--. 1 slowstep slowstep 0 Aug 12 20:35 tmp.txt
[slowstep@localhost dir]$ cat tmp.txt 
cat: tmp.txt: Permission denied   //请求被拒绝
[slowstep@localhost dir]$ echo "hellow" >tmp.txt 
-bash: tmp.txt: Permission denied  //请求被拒绝,这也说明了文件确实有权限这个概念。

修改文件权限不仅可以使用chmod u/g/o +(-) r/w/x 还可以使用8进制的方法。
r权限要么是有,要么是没有,对应2种状态
w权限要么是有,要么是没有,对应2种状态
x权限要么是有,要么是没有,对应2种状态
所以可以用8进制数字转化为2进制来表示权限
例如7的2进制是111,表示三种权限都有。0的二进制是000,表示三种权限都没有

[slowstep@localhost dir]$ ll
total 0
----rw-r--. 1 slowstep slowstep 0 Aug 12 20:35 tmp.txt
[slowstep@localhost dir]$ chmod 000 tmp.txt 
[slowstep@localhost dir]$ ll
total 0
----------. 1 slowstep slowstep 0 Aug 12 20:35 tmp.txt
[slowstep@localhost dir]$ chmod 777 tmp.txt
[slowstep@localhost dir]$ ll
total 0
-rwxrwxrwx. 1 slowstep slowstep 0 Aug 12 20:35 tmp.txt

第一个0表示拥有者的权限,第二个0表示所属组的权限,第三个0表示other的权限。

下面说明目录的权限,目录的权限也是rwx

目录的x权限:表示能否进入这个目录

[slowstep@localhost dir]$ ll
total 0
drwxrwxr-x. 2 slowstep slowstep 6 Aug 12 20:43 newdir
[slowstep@localhost dir]$ chmod u-x newdir
[slowstep@localhost dir]$ ll
total 0
drw-rwxr-x. 2 slowstep slowstep 6 Aug 12 20:43 newdir
[slowstep@localhost dir]$ cd newdir
-bash: cd: newdir: Permission denied

注意:权限只被认证一次

这里虽然slowstep既是目录的拥有者,也是目录的所属组,所属组具有目录的x权限。但是由于权限只被认证一次,slowstep是目录的拥有者,所以被直接认证为拥有者的权限,即rw-,所属组的权限就不在认证

目录的r权限:能否查看目录中的内容

[slowstep@localhost dir]$ ll
total 0
d-wxrwxr-x. 2 slowstep slowstep 20 Aug 12 20:50 newdir
[slowstep@localhost dir]$ ls newdir
ls: cannot open directory newdir: Permission denied
[slowstep@localhost dir]$ cd newdir
[slowstep@localhost newdir]$ ll
ls: cannot open directory .: Permission denied

目录的w权限:能否在目录下创建文件或者文件夹,能否删除该目录下的文件或者目录,能否对该目录下的文件或者目录移动位置,能否对该目录下的文件或者目录进行重命名

[slowstep@localhost dir]$ ll
total 0
dr-xrwxr-x. 2 slowstep root 20 Aug 12 20:50 newdir
[slowstep@localhost dir]$ tree
.
└── newdir
    └── test.c

1 directory, 1 file
[slowstep@localhost dir]$ mv newdir/test.c test1.txt
mv: cannot move ‘newdir/test.c’ to ‘test1.txt’: Permission denied
[slowstep@localhost dir]$ mv newdir/test.c .
mv: cannot move ‘newdir/test.c’ to ‘./test.c’: Permission denied
[slowstep@localhost dir]$ rm newdir/test.c 
rm: cannot remove ‘newdir/test.c’: Permission denied
修改文件的拥有者和所属组

chown,chgrp

//chown修改拥有者
//chgrp修改所属组
需要有root的权限才可以修改
[slowstep@localhost dirtest]$ ll
total 4
-rw-rwx---. 1 slowstep slowstep 2 Aug 11 21:13 test.txt
[slowstep@localhost dirtest]$ chown root test.txt 
chown: changing ownership of ‘test.txt’: Operation not permitted
[slowstep@localhost dirtest]$ sudo chgrp root test.txt 
[sudo] password for slowstep: 
[slowstep@localhost dirtest]$ ll
total 4
-rw-rwx---. 1 slowstep root 2 Aug 11 21:13 test.txt
[slowstep@localhost dirtest]$ ll
total 4
-rw-rwx---. 1 slowstep root 2 Aug 11 21:13 test.txt
[slowstep@localhost dirtest]$ sudo chown root:slowstep test.txt 
[sudo] password for slowstep: 
[slowstep@localhost dirtest]$ ll
total 4
-rw-rwx---. 1 root slowstep 2 Aug 11 21:13 test.txt
root不受权限约束
[slowstep@localhost dirtest]$ ll
total 4
----------. 1 slowstep slowstep 178 Aug 11 13:17 test.txt
[slowstep@localhost dirtest]$ cat test.txt 
cat: test.txt: Permission denied
[slowstep@localhost dirtest]$ su
Password: 
[root@localhost dirtest]# ll
total 4
----------. 1 slowstep slowstep 178 Aug 11 13:17 test.txt
[root@localhost dirtest]# echo "h" >test.txt 
[root@localhost dirtest]# cat test.txt 
h
[root@localhost dirtest]# exit
[slowstep@localhost dirtest]$ ll
total 4
----------. 1 slowstep slowstep 2 Aug 11 21:13 test.txt
[slowstep@localhost dirtest]$ cat test.txt 
cat: test.txt: Permission denied
  • 19
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值