博客园不常在线

有问题联系微信

微信号

微信公众号

Linux系列:文件权限、查找

一.文件权限

1.查看当前登录用户:whoami

[root@centos-master test]# whoami
root

2.修改文件权限

chmod [who] [+|-|=] [mode]

who:

文件所有者:u

文件所属组:g

其他人:       o

所有的人: a  不写的话就是a

+:添加权限

-:减少权限

=:覆盖原来的权限

mode: 
r:读

w:写

x:执行

[root@centos-master test]# ls -l
total 0
-rw-r--r-- 1 root root 0 Dec  8 21:58 test.txt
[root@centos-master test]# chmod o+w test.txt
[root@centos-master test]# ls -l
total 0
-rw-r--rw- 1 root root 0 Dec  8 21:58 test.txt
[root@centos-master test]# chmod g+w test.txt
[root@centos-master test]# ls -l
total 0
-rw-rw-rw- 1 root root 0 Dec  8 21:58 test.txt
[root@centos-master test]# chmod +x test.txt
[root@centos-master test]# ls -l
total 0
-rwxrwxrwx 1 root root 0 Dec  8 21:58 test.txt
[root@centos-master test]# chmod =w test.txt
[root@centos-master test]# ls -l test.txt
--w------- 1 root root 0 Dec  8 21:58 test.txt
[root@centos-master test]# chmod a=w test.txt
[root@centos-master test]# ls -l test.txt
--w--w--w- 1 root root 0 Dec  8 21:58 test.txt
[root@centos-master test]#

数字设定法:

-:没有权限

r:4

w:2

x:1

3:代表x+w ;5:r+x ;7:r+w+x

7:-- rwx -- 文件所有者

6:-- rw -- 文件所属组

5:-- rx -- 其他人

[root@centos-master test]# chmod a=wrx test.txt
[root@centos-master test]# ls -l test.txt
-rwxrwxrwx 1 root root 0 Dec  8 21:58 test.txt
[root@centos-master test]# chmod 755 test.txt
[root@centos-master test]# ls -l test.txt
-rwxr-xr-x 1 root root 0 Dec  8 21:58 test.txt
[root@centos-master test]# chmod 644 test.txt
[root@centos-master test]# ls -l test.txt
-rw-r--r-- 1 root root 0 Dec  8 21:58 test.txt
[root@centos-master test]# chmod -001 test.txt
[root@centos-master test]# ls -l test.txt
-rw-r--r-- 1 root root 0 Dec  8 21:58 test.txt

3.修改文件的所有者和所属组:chown、chgrp

chown root test.txt 

chown root:root test.txt

chgrp root test.txt

非管理员权限请加上sudo

 

[root@centos-master test]# ls -l test.txt
-rw-r--r-- 1 root root 0 Dec  8 21:58 test.txt
[root@centos-master test]# cd /home/
[root@centos-master home]# ls -l
total 4
drwx------ 2 nexus nexus 4096 Dec  5 16:32 nexus
[root@centos-master home]# cd -
/test
[root@centos-master test]# chown nexus test.txt
[root@centos-master test]# ls -l test.txt
-rw-r--r-- 1 nexus root 0 Dec  8 21:58 test.txt
[root@centos-master test]# ^C
[root@centos-master test]# chown nexus:nexus  test.txt
[root@centos-master test]# ls -l test.txt
-rw-r--r-- 1 nexus nexus 0 Dec  8 21:58 test.txt
[root@centos-master test]#
[root@centos-master test]# sudo chgrp root  test.txt
[root@centos-master test]# ls -l test.txt
-rw-r--r-- 1 nexus root 0 Dec  8 21:58 test.txt
[root@centos-master test]#

 

[root@centos-master test]# mkdir newDir
[root@centos-master test]# ls -l
total 4
drwxr-xr-x 2 root  root 4096 Dec  8 22:31 newDir
-rw-r--r-- 1 nexus root    0 Dec  8 21:58 test.txt
[root@centos-master test]# chmod 000 newDir/
[root@centos-master test]# ls -l
total 4
d--------- 2 root  root 4096 Dec  8 22:31 newDir
-rw-r--r-- 1 nexus root    0 Dec  8 21:58 test.txt
[root@centos-master test]# cd newDir/
[root@centos-master newDir]# cd ..
[root@centos-master test]# ls -l newDir/
total 0
[root@centos-master test]# cd newDir/
[root@centos-master newDir]# touch hello.c
[root@centos-master newDir]# ls
hello.c
[root@centos-master newDir]# cd ..
[root@centos-master test]# ls
newDir  test.txt
[root@centos-master test]# ls -l newDir/
total 0
-rw-r--r-- 1 root root 0 Dec  8 22:33 hello.c
[root@centos-master test]# su nexus
[nexus@centos-master test]$ ls -l newDir/
ls: cannot open directory newDir/: Permission denied
[nexus@centos-master test]$

二.文件查找

查找文件属性:find

1.文件名:find +查找的目录+-name+"文件的名字"

[root@centos-master test]# find -name  "test.txt"
./test.txt
./newDir/test.txt
./newDir/dir/test.txt
[root@centos-master test]# find /test/ -name "test.txt"
/test/test.txt
/test/newDir/test.txt
/test/newDir/dir/test.txt
[root@centos-master test]# find -name "test*"
./test.txt
./newDir/test.txt
./newDir/dir/test.txt
[root@centos-master test]# touch test.
[root@centos-master test]# find -name "test?"
./test.
[root@centos-master test]#

2.文件大小:find +查找目录+-size+ +10k

find -size +10M -size -100M 按区间搜索
[root@centos-master test]# find -size -10k
.
./text.
./test.txt
./test.
./newDir
./newDir/test.txt
./newDir/dir
./newDir/dir/test.txt
./newDir/hello.c
[root@centos-master test]# find -size +10M
[root@centos-master test]# find -size +10M -size -100M
[root@centos-master test]#

3.文件类型:find + 查找目录 +-type +/d/f/b/c/s/p/l

[root@centos-master test]# find -type f
./text.
./test.txt
./test.
./newDir/test.txt
./newDir/dir/test.txt
./newDir/hello.c
[root@centos-master test]# mkfifo  hello
[root@centos-master test]# find -type p
./hello
[root@centos-master test]#

文件的检索:grep

[root@centos-master test]# grep -r "hello.c" /test/
/test/newDir/test.txt:[root@centos-master test]# vim hello.c
/test/newDir/test.txt:hello.c
/test/newDir/test.txt:[root@centos-master test]# cat hello.c
/test/newDir/test.txt:[root@centos-master test]# ln hello.c  hello2.c
/test/newDir/test.txt:hello2.c  hello.c
/test/newDir/test.txt:-rw-r--r-- 2 root root 13 Dec  8 21:27 hello.c
/test/newDir/test.txt:-rw-r--r-- 2 root root 13 Dec  8 21:27 hello.c
/test/newDir/test.txt:[root@centos-master test]# ln hello.c hello3.c
/test/newDir/test.txt:-rw-r--r-- 3 root root 13 Dec  8 21:27 hello.c
/test/newDir/test.txt:-rw-r--r-- 2 root root 13 Dec  8 21:27 hello.c
/test/newDir/test.txt:[root@centos-master test]# cat hello.c
[root@centos-master test]#
posted @ 2020-12-08 23:00  Code技术分享  阅读(3)  评论(0编辑  收藏  举报