1

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.
5
  • Does this answer your question? Compare content of two folders/disks (including timestamps,permissions)
    – Jim L.
    Commented Nov 1, 2021 at 15:32
  • No. tree command solution is not applicable here. Since its not installed by default here (Centos 7core) neither I'm allowed to do it manually
    – n1Ls
    Commented Nov 1, 2021 at 16:04
  • Do you want to compare the files in the two directories, or do you want to synchronize them (make them the same including ownership, permissions)? You can use rsync for this purpose, Dry run to only compare and 'normal copying action' to synchronize using a suitable set of options depending on the fine details of what you want.
    – sudodus
    Commented Nov 1, 2021 at 16:04
  • I just wanted to report. Will try out rsync dry run once. Thanks
    – n1Ls
    Commented Nov 1, 2021 at 16:31
  • ... what is the second situation you are facing challenges with?
    – AdminBee
    Commented Nov 2, 2021 at 10:30

1 Answer 1

2

Dry run to identify differences

This command line with rsync works for me with similar directory trees,

rsync -Havvn --existing source/ target | grep -v uptodate$'

Action

Remove the option n to get action.


You must tweak the command line to match the main directories in your case (possibly with remote path via ssh). There are two v's to increase verbosity and get output where only executable flags differ; when action, it will fix it without verbosity flags.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .