Bash脚本获取两个目录中文件的权限差异

发布于 2025-01-03 10:59:58 字数 458 浏览 1 评论 0原文

如何在 Linux 上编写 bash 脚本来确定两个目录中的哪些文件具有不同的权限?

例如,我有两个目录:

fold1 有两个文件:

 1- file1 (-rw-rw-r--)  
 2- file2 (-rw-rw-r--)

fold2 有具有不同权限的同名文件:

1- file1 (-rwxrwxr-x)  
2- file2 (-rw-rw-r--)

我需要一个脚本来输出具有不同权限的文件名, 所以脚本只会打印 file1

我目前正在通过显示文件来手动检查权限:

for i in `find .`; do ls -l $i ls -l ../file2/$i; done

How can I write a bash script on Linux to determine which files in two directories have different permissions?

For example, I have two directories:

fold1 having two files:

 1- file1 (-rw-rw-r--)  
 2- file2 (-rw-rw-r--)

fold2 having same-name files with different permissions:

1- file1 (-rwxrwxr-x)  
2- file2 (-rw-rw-r--)

I need a script to output the file names that have different permissions,
so the script will print only file1

I am currently checking the permissions manually by displaying the files with:

for i in `find .`; do ls -l $i ls -l ../file2/$i; done

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽心 2025-01-10 10:59:58

解析 find . 输出: for i in $(find .) 会给您带来任何带有空格、换行符或其他完全正常字符的文件名的麻烦:

$ touch "one file"
$ for i in `find .` ; do ls -l $i ; done
total 0
-rw-r--r-- 1 sarnold sarnold 0 2012-02-08 17:30 one file
ls: cannot access ./one: No such file or directory
ls: cannot access file: No such file or directory
$ 

使用以下方式 可能因所有者或团体而异,我认为您也应该包括这些。如果您需要包含 SELinux 安全标签,stat(1) 程序也可以通过 %C 指令轻松实现:(

for f in * ; do stat -c "%a%g%u" "$f" "../scatman/${f}" |
    sort | uniq -c | grep -q '^\s*1' && echo "$f" is different ; done

echo 命令...)

示例:

$ ls -l sarnold/ scatman/
sarnold/:
total 0
-r--r--r-- 1 sarnold sarnold 0 2012-02-08 18:00 funky file
-rw-r--r-- 1 sarnold sarnold 0 2012-02-08 18:01 second file
-rw-r--r-- 1 root    root    0 2012-02-08 18:05 third file

scatman/:
total 0
-rw-r--r-- 1 sarnold sarnold 0 2012-02-08 17:30 funky file
-rw-r--r-- 1 sarnold sarnold 0 2012-02-08 18:01 second file
-rw-r--r-- 1 sarnold sarnold 0 2012-02-08 18:05 third file
$ cd sarnold/
$ for f in * ; do stat -c "%a%g%u" "$f" "../scatman/${f}" | sort | uniq -c | grep -q '^\s*1' && echo "$f" is different ; done
funky file is different
third file is different
$ 

Parsing find . output with: for i in $(find .) is going to give you trouble for any filenames with spaces, newlines, or other perfectly normal characters:

$ touch "one file"
$ for i in `find .` ; do ls -l $i ; done
total 0
-rw-r--r-- 1 sarnold sarnold 0 2012-02-08 17:30 one file
ls: cannot access ./one: No such file or directory
ls: cannot access file: No such file or directory
$ 

Since permissions can also differ by owner or by group, I think you should include those as well. If you need to include the SELinux security label, the stat(1) program makes that easy to get as well via the %C directive:

for f in * ; do stat -c "%a%g%u" "$f" "../scatman/${f}" |
    sort | uniq -c | grep -q '^\s*1' && echo "$f" is different ; done

(Do whatever you want for the echo command...)

Example:

$ ls -l sarnold/ scatman/
sarnold/:
total 0
-r--r--r-- 1 sarnold sarnold 0 2012-02-08 18:00 funky file
-rw-r--r-- 1 sarnold sarnold 0 2012-02-08 18:01 second file
-rw-r--r-- 1 root    root    0 2012-02-08 18:05 third file

scatman/:
total 0
-rw-r--r-- 1 sarnold sarnold 0 2012-02-08 17:30 funky file
-rw-r--r-- 1 sarnold sarnold 0 2012-02-08 18:01 second file
-rw-r--r-- 1 sarnold sarnold 0 2012-02-08 18:05 third file
$ cd sarnold/
$ for f in * ; do stat -c "%a%g%u" "$f" "../scatman/${f}" | sort | uniq -c | grep -q '^\s*1' && echo "$f" is different ; done
funky file is different
third file is different
$ 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文