授权命令chmod 想必不用我多说了,大家都知道,那么如何以刁钻的角度去使用它,让它用最简洁的方式达到你的目的呢?

先来看两个截图,图1

图2

这两张图是test 这个文件夹下的目录结构图以及权限图。那么接下来我要将test这个目录以及子目录的所有.sh 的文件设置为只有可执行权限要怎么设置呢?执行以下命令后会发现 tesh.sh 和./test1/test1.sh 的权限已经变成---x--x--x 而其它保持不变

[root@oracle1 test]# find . -name "*.sh" -exec chmod 111 {} \;
[root@oracle1 test]# ll
total 0
drwxr-xr-x 2 root root 38 Sep 29 10:31 test1
drwxr-xr-x 2 root root  6 Sep 29 10:13 test2
-rw-r--r-- 1 root root  0 Sep 29 10:38 test.bash
---x--x--x 1 root root  0 Sep 29 10:22 test.sh
-rw-r--r-- 1 root root  0 Sep 29 10:38 test.txt

find .  -name "*.sh"  与 find . -name *.sh 的区别是少了个双引号,结果就是一个递归,一个不递归。

[root@oracle1 test]# find ../test -name "*.sh"
../test/test1/test1.sh
../test/test.sh
[root@oracle1 test]# find ../test -name *.sh
../test/test.sh

有人会说了 chmod 有个 -R 参数就可以实现递归了。 但是chmod -R 实现的是无差别攻击所以并不适用,以上授权命令还可以写成这样。效果上是一样的。

[root@oracle1 test]# chmod 111 `find . -name "*.txt"`
[root@oracle1 test]# ll
total 0
d--x--x--x 2 root root 38 Sep 29 10:31 test1
d--x--x--x 2 root root  6 Sep 29 10:13 test2
-rw-r--r-- 1 root root  0 Sep 29 10:38 test.bash
---x--x--x 1 root root  0 Sep 29 10:22 test.sh
---x--x--x 1 root root  0 Sep 29 10:38 test.txt

那么问题又来了 我一个目录下要是只有文件与文件夹的区别呢?  好的 ,这个时候你就可以用 find  命令的 -type 参数了 type 后面 d代表目录,f 代表 文件

给文件设置为只有可执行权限

[root@oracle1 test]# chmod 111 `find . -type f`
[root@oracle1 test]# ll
total 0
d--x--x--x 2 root root 38 Sep 29 10:31 test1
d--x--x--x 2 root root  6 Sep 29 10:13 test2
---x--x--x 1 root root  0 Sep 29 10:38 test.bash
---x--x--x 1 root root  0 Sep 29 10:22 test.sh
---x--x--x 1 root root  0 Sep 29 10:38 test.txt

给目录设置为755权限

[root@oracle1 test]# chmod 755 `find . -type d`
[root@oracle1 test]# ll
total 0
drwxr-xr-x 2 root root 38 Sep 29 10:31 test1
drwxr-xr-x 2 root root  6 Sep 29 10:13 test2
---x--x--x 1 root root  0 Sep 29 10:38 test.bash
---x--x--x 1 root root  0 Sep 29 10:22 test.sh
---x--x--x 1 root root  0 Sep 29 10:38 test.txt

 

以下是取反,不懂find 用法的可以查看我的另一篇文章,以下命令就是不以 .bash 结尾的文件加权。-not 可以换成 !

find . -not -name "*.bash" -exec chmod o+x {} \;

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐