我正在尝试创建一个bash脚本,该脚本将能够执行find命令来获取目录树中每个文件/目录的权限和完整路径,并为每个唯一权限创建一个文件,并打印该目录的每个完整文件路径权限文件。然后,将来我可以阅读这些文件,并使用它们来授予运行脚本时的权限。
例如:
drwxrwxrwx /home/user/testDirectory
-rwxrwxrwx /home/user/testDirectory/testFile
drwxr-xr-x /home/user/testDirectory/directory2
-rwxr-xr-x /home/user/testDirectory/directory2/test2
上面将创建2个文件(例如777.txt
和755.txt
),每个文件都有2行。
我在为每个唯一权限创建文件然后发送完整文件路径的逻辑上苦苦挣扎。
到目前为止,我所拥有的(我怀疑该数组是否必要,但是我已经按照权限对数组进行排序,可以使用sort命令上的-k 1.2来忽略d标志):
declare -a PERMS
i=0
while read line
do
PERMS[$i]="$line"
(( i++ ))
done < <( find /opt/sas94 -printf ""%M/" "%p/"\n")
试试这个:
#!/bin/bash
while read file; do
stat -c '%A %n' "$file" >> $(stat -c '%a' "$file").txt
done < <(find "$1")
用法:
./script.sh /path/to/directory
stat -c '%A %n' "$file"
打印文件的权限和路径,例如-rw-rw-rw- /foo/bar
stat -c '%a' "$file"
以八进制形式打印权限,例如666
第一个的输出stat
附加到第二个stat
使用后缀创建的文件名中.txt
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句