Linux文件
Linux系统里面一切皆文件。首先要理解Linux文件权限和Linux文件的相关时间
1. Linux文件 权限
- Linux下文件的权限类型一般包括读,写,执行。对应字母为 r、w、x
- Linux下权限的粒度有 拥有者 、群组 、其它组 三种。每个文件都可以针对三个粒度,设置不同的rwx(读写执行)权限。
2. Linux文件 时间信息 文件信息有关文件,有3个 文件状态相关 的时间 信息。参考linux中文件的三种time(atime,mtime,ctime)
- atime (access time) :访问时间 :文件中的数据最后被访问的时间
- mtime (modify time) :修改时间:文件内容被修改的最后时间
- ctime (change time) :变化时间:文件的元数据发生变化的时间。比如权限,所有者等
文件相关命令
- ls命令:列出当前目录的所有文件 或者 判断文件是否存在
- ll命令: 列出当前目录的所有文件 并显示文件的读写可执行权限 和 文件mtime 和文件名
- chmod命令:设置或修改文件权限
- stat命令:查看某个文件状态相关的所有信息(包括权限、时间信息)的命令
1.Linux ls 命令 参考菜鸟教程 Linux ls命令
参数
- -l 除文件名称外,亦将文件型态、权限、拥有者、文件大小等资讯详细列出
语法:ls 命令单独使用 列出当前目录的所有文件 只显示文件名
[root@VM_0_8_centos ~]# ls
file file3 file4 file5
语法:ls file 用来判断文件是否存在
#判断文件 file1是否存在,从返回结果看不存在
[root@VM_0_8_centos ~]# ls file1
ls: cannot access file1: No such file or directory
##判断文件 file1是否存在,从返回结果看 file存在
[root@VM_0_8_centos ~]# ls file
file
用法: 判断文件是否存在 并把结果重定向到垃圾箱,也就是删掉输出,不过是文件存在的标准输出还文件不存在的错误输出。
[root@VM_0_8_centos ~]# ls file 1> /dev/null 2>&1
[root@VM_0_8_centos ~]# ls file1 1> /dev/null 2>&1
2.Linux ll 命令 等同于 ls -l 列出当前目录的所有文件 并显示文件的读写可执行权限 和 文件mtime 和文件名等。
#显示该目录下所有文件 包括每个文件的权限,mtime等信息
[root@VM_0_8_centos ~]# ll
total 8
-rw-r--r-- 1 root root 66 Mar 19 18:37 file
-rw-r--r-- 1 root root 88 Mar 20 15:06 file3
-rw-r--r-- 1 root root 0 Mar 20 15:23 file4
-rw-r--r-- 1 root root 0 Mar 20 15:00 file5
3.Linux chmod命令 设置或修改文件权限 菜鸟教程 Linux chmod命令
语法 chmod [ugoa] [+-=] [rwxX] file
- u 表示该文件的拥有者,g 表示与该文件的拥有者属于同一个群体(group)者,o 表示其他以外的人,a 表示这三者皆是。
- + 表示增加权限、- 表示取消权限、= 表示唯一设定权限。
- r 表示可读取,w 表示可写入,x 表示可执行,X 表示只有当该文件是个子目录或者该文件已经被设定过为可执行
例子
#把文件权限添加 所有人都可读权限
chmod ugo+r file1.txt
语法2:chmod abc file 数字来表示权限 语法 这是设置权限,不是取消权限,不是增加权限。 相当于 chmod命令 的= 符合表示唯一设定权限
- 其中a,b,c各代表一个数字,分别表示User、User、Group、及Other的权限
- 各个权限代表的数字 r=4,w=2,x=1
- 若要rwx属性则4+2+1=7;
- 若要rw-属性则4+2=6;
- 若要r-x属性则4+1=5
例子
#设置file3文件的权限 为444 user group other 都是可读权限
[root@VM_0_8_centos ss]# chmod 444 file3
#查看file3的权限
[root@VM_0_8_centos ss]# ll
total 8
-r--r--r-- 1 root root 14 Mar 23 10:35 file3 #-r--r--r-- user group other都是可读权限
#设置file3文件的权限 为644 user为read和write权限 group和 other是可读权限
[root@VM_0_8_centos ss]# chmod 644 file3
#再次查看file3的权限
[root@VM_0_8_centos ss]# ll
total 8
-rw-r--r-- 1 root root 14 Mar 23 10:35 file3
4.Linux ll 命令 可以用来 查看文件 相关的3个时间 ll命令 默认显示mtime
#默认列出时间的 mtime
[root@VM_0_8_centos ss]# ll
#列出文件的 atime
[root@VM_0_8_centos ss]# ll --time=atime
#列出文件的ctime
[root@VM_0_8_centos ss]# ll --time=ctime
5. Linux stat命令 查看某个文件状态相关的所有信息(包括权限、时间信息)的命令 菜鸟教程 Linux stat命令
#查看本目录下有哪些文件
[root@VM_0_8_centos ss]# ll
total 8
-rw-r--r-- 1 root root 63 Mar 22 21:59 file2
drwxr-xr-x 3 root root 4096 Mar 22 20:43 ss
#查看 file2文件 的时间信息
[root@VM_0_8_centos ss]# stat file2
File: ‘file2’
Size: 63 Blocks: 8 IO Block: 4096 regular file
Device: fd01h/64769d Inode: 2228226 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-03-22 19:55:27.000000000 +0800
Modify: 2020-03-22 21:59:24.482165123 +0800
Change: 2020-03-22 21:59:24.482165123 +0800
Birth: -