Compare ownership and permissions of all files in 2 directories in bash
2
votes
2
answers
1094
views
I'm trying to fetch file ownership and permissions of all files in 2 directory and compare them. Report file with same name but different file ownership or permission.
I have fetched file ownership and permission of all files in first directory to file1.txt and second directory to file2.txt
My script progress:
[root@test]# cat file1.txt
644 root root /home/user2/sample-test/abc
644 root root /home/user2/sample-test/bcd
644 root root /home/user2/sample-test/efg
644 root root /home/user2/sample-test/mama
644 root root /home/user2/sample-test/ngins2
644 root root /home/user2/sample-test/nils45
644 root root /home/user2/sample-test/sample2
644 root root /home/user2/sample-test/t1
644 root root /home/user2/sample-test/t2
644 root root /home/user2/sample-test/test1
755 root root /home/user2/sample-test
644 root root /home/user2/sample-test1/abc
644 root root /home/user2/sample-test1/ppp
644 root root /home/user2/sample-test1/werwre
755 root root /home/user2/sample-test1
644 root root /home/user2/testing123
[root@test]# cat file2.txt
644 root root /home/user2/sample-test/ip
644 root root /home/user2/sample-test/new-file
644 root root /home/user2/sample-test/ngins2
644 root root /home/user2/sample-test/nils45
644 root root /home/user2/sample-test/sample2
755 root root /home/user2/sample-test
755 root root /home/user2/sample-test/test1.sh
644 apache apache /home/user2/sample-test1/ppp
644 apache fes /home/user2/sample-test1/abc
644 root root /home/user2/sample-test1/perms.saved
644 root root /home/user2/sample-test1/test
644 root root /home/user2/sample-test1/test1
644 root root /home/user2/sample-test1/werwre
755 root root /home/user2/sample-test1
755 root root /home/user2/sample-test1/1.sh
644 root root /home/user2/testing123
find /path/to/dir1 -depth -exec stat --format '%a %U %G %n' {} + | sort -n" >> file1.txt
find /path/to/dir2 -depth -exec stat --format '%a %U %G %n' {} + | sort -n" >> file2.txt
t1=
cat file1.txt
t5=cat file2.txt
#find lines only in file1
only1=$(comm -23 "$t1"_sorted "$t5"_sorted)
#find lines only in file2
only2=$(comm -13 "$t1"_sorted "$t5"_sorted)
I'm facing challenges while handling these 2 situations:
1. If file is missing in dir1 or dir2 should be handled.
Consider files in dir1 are correct files and files in dir2 are having messed up permissions/ownerships.
I just want to compare files which have same name in dir1 and dir2 but different ownership/permission.
Asked by n1Ls
(61 rep)
Nov 1, 2021, 01:41 PM
Last activity: Jul 20, 2025, 09:51 AM
Last activity: Jul 20, 2025, 09:51 AM