修改文件属主
示例:
1 在 /opt 目录下建立一个文件夹 software
2 将 software 目录分配给用户 shaofa
su root
mkdir /opt/software
chown -R shaofa /opt/software
ls -ld /opt/software
mkdir /opt/software
笔记:
文件的权限
本章讨论文件的权限问题
考虑以下几个方面:
- owner : 文件的属主 owner
- r : 文件是否可读 read
- w : 文件是否可写 write
- x: 文件是否可以执行 excute
比如,系统中有用户: shaofa 和 ming
一个文件 simple.txt ,由 shaofa 创建
那么,ming 能访问这个访问吗?是否可读?是否可写
使用 ls 命令查看一个文件
ls -l simple.txt -rw-r--r--. 1 shaofa shaofa 13 4月 21 03:48 simple.txt
其中,rw-r--r-- 表示该文件的访问权限
类型 : 目录 d / 文件 -
权限 :
链接/子项数 :属主 :组名 : 文件大小 : 修改时间 : 文件名
r w - r - - r - -
权限分为三部分:自己 | 同组 | 别人
例如 :
r w x r w x r w x
r w x r w x r - -
r - - - - - - - -
在文件夹窗口里,文件 | 属性 | 权限 查看
修改文件权限
chmod , 即change file mode 修改文件的访问权限
比如,一个文件simple.txt ,想让别人也能修改 chmod o+w simple.txt
其中,
o ,表示 other 别人 +w ,表示增写 write 权限
chmod a+w simple.txt
所有人 all 添加w权限
chmod a-w simple.txt
所有人减去w权限
chmod u+w simple.txt
仅用户 user 自己的权限 +w
chmod o-w simple.txt
修改别人 other 的权限 -w
1 只有文件的属主或者root 可以修改文件权限
2 省略写法
chmod +w simple.txt
chmod -w simple.txt
默认是修改自己和本组的权限
chown , 即change owner 修改文件的属主
一般来说,每个用户只操作自己的用户目录
所以 chown 命令并不常用
示例:
1 在 /opt 目录下建立一个文件夹 software
2 将 software 目录分配给用户 shaofa
su root
mkdir /opt/software
chown -R shaofa /opt/software
ls -ld /opt/software
只有文件的 owner 和 root 才有权更改属主
可执行脚本
脚本 Script ,一种解释执行的程序
Linux 下常见的三种脚本程序
- Shell 脚本 *.sh
- Perl 脚本 *.pl
- Python 脚本 *.py
脚本程序,本质是一个文本文件
1 它是一个文本文件
2 它具有可执行权限
脚本程序都是由解释器来执行的
Shell脚本解释器: /bin/sh Perl
脚本解释器: /bin/perl Python
脚本解释器:/bin/python3
执行一个脚本时,以下两种方式等效
./hello.py /bin/python3 hello.py
shell脚本
Shell脚本,按Shell的语法写出来的脚本
是 Linux 自带的脚本语言
相当于Windows下的 DOS 批处理脚本
1 编辑一个文本文件,保存为 hello.sh
#!/bin/sh
echo "hello,world"
2 添加可执行权限
chmod +x hello.sh
3 执行程序
./hello.sh
/bin/sh hello.sh
1 第一行必须声明解释器
#!/bin/sh
2 必须有 x 权限,才能够执行
比如,一个程序只有作者执行,别人不能执行
rwxr--r--
3 执行程序时,必须加上路径
./hello.sh
/home/shaofa/hello.sh
python脚本
Python脚本,比Shell要强大一些
1 编辑一个文本文件,保存为 hello.py
2 添加可执行权限
chmod +x hello.py
3 执行程序 ./hello.py