linux 批量授权,chmod+find 批量授权

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

find . -name "*.sh" -exec chmod 111 {} \; ​​​​​​​

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.tx

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

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

把 文件里面.txt 改成执行权限

[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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用 `rename` 命令来批量修改文件的后缀。 格式为:`rename 's/原后缀/新后缀/' *原后缀` 例如,将所有以 .txt 为后缀的文件后缀改为 .md,命令为: ``` rename 's/txt/md/' *txt ``` 注意, 这只是一种通用的做法,如果你的系统里没有rename命令,可能需要使用其他命令,比如find+mv。 ### 回答2: 要在Linux系统中批量修改文件后缀,可以使用shell脚本来完成。以下是一种可能的解决方案: 首先,打开终端并进入要修改文件后缀的目录。可以使用cd命令来导航到目标目录,例如: cd /path/to/directory 然后,创建一个新的shell脚本文件,例如rename.sh,并用文本编辑器打开它: vi rename.sh 向该脚本文件中添加以下代码: #!/bin/bash for file in *; do if [ -f "$file" ]; then newname="${file%.*}.newext" mv "$file" "$newname" fi done 在这段代码中,“newext”是你想要的新文件后缀名,可以根据需要自行更改。然后保存并退出脚本文件。 接下来,给所创建的脚本文件添加可执行权限,使用以下命令: chmod +x rename.sh 最后,运行该脚本文件以批量修改文件后缀,使用以下命令: ./rename.sh 脚本将会遍历目标目录中的所有文件,并将它们的后缀名替换为“newext”。请确保在运行脚本之前备份目标目录中的文件,以防出现错误。 注意:本解决方案假设你已经安装了Bash shell。如果你使用的是其他shell,请相应地更改脚本文件的开头。 ### 回答3: 在Linux中,我们可以使用Shell脚本或者命令行工具来实现批量修改文件后缀。 1. 使用Shell脚本: 首先,创建一个新的Shell脚本文件,例如rename.sh。在脚本文件中,我们可以使用for循环来遍历所有的文件,并使用mv命令来修改文件的后缀。 以下是一个简单的示例代码: ```shell #!/bin/bash for file in *.txt; do mv "$file" "${file%.txt}.doc" done ``` 上述代码中,脚本会将所有的txt文件的后缀修改为doc。 保存并退出脚本文件,然后在终端中运行脚本文件: ```shell $ bash rename.sh ``` 这将完成文件后缀修改的批处理操作。 2. 使用命令行工具: 另一种方法是使用rename命令来批量修改文件后缀。rename命令可以通过正则表达式来匹配文件名,并将符合条件的文件重命名。 以下是一个使用rename命令的示例代码: ```shell $ rename 's/\.txt$/.doc/' *.txt ``` 上述命令将所有以txt结尾的文件的后缀修改为doc。 运行上述命令后,符合条件的文件的后缀将会被修改。 无论是使用Shell脚本还是命令行工具,都可以方便地实现Linux下的批量修改文件后缀操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值