2012-02-09 28 views
1

如何在Linux上编写bash脚本来确定两个目录中的哪些文件具有不同的权限?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 

回答

3

解析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 
$ 
+0

真的很好的解释。 +1 – 2012-02-09 07:19:32