How to compare 2 folders' permission on Unix?

11,734

Solution 1

One way to compare permissions on your two directories is to capture the output of ls -al to a file for each directory and diff those.

Say you have two directories called a and b.

cd a
ls -alrt > ../a.list
cd ../b
ls -alrt > ../b.list
cd ..
diff a.list b.list

If you find that this gives you too much noise due to file sizes and datestamps you can use awk to filter out some of the columns returned by ls e.g.:

ls -al | awk {'printf "%s %s %s %s %s %s\n", $1,$2,$3,$4,$5,$9 '}

Or if you are lucky you might be able to remove the timestamp using:

ls -lh --time-style=+

Either way, just capture the results to two files as described above and use diff or sdiff to compare the results.

Solution 2

If you have the tree command installed, it can do the job very simply using a similar procedure to the one that John C suggested:

cd a
tree -dfpiug > ../a.list
cd ../b
tree -dfpiug > ../b.list
cd ..
diff a.list b.list

Or, you can just do this on one line:

diff <(cd a; tree -dfpiug) <(cd b; tree -dfpiug)

The options given to tree are as follows:

  • -d only scans directories (omit to compare files as well)
  • -f displays the full path
  • -p displays permissions (e.g., [drwxrwsr-x])
  • -i removes tree's normal hierarchical indent
  • -u displays the owner's username
  • -g displays the group name
Share:
11,734
Bill Lin
Author by

Bill Lin

Updated on June 13, 2022

Comments

  • Bill Lin
    Bill Lin almost 2 years

    Given 2 folder: /folder1 and /folder2 and each folder has some files and subfolders inside. I used following command to compare the file difference including sub folder :

    diff -buf /folder1 /folder2
    

    which found no difference in term of folder and file structural .

    However, I found that there are some permission differences between these 2 folders' files. Is there simple way/command to compare the permission of each file under these 2 folders (including sub-folders) on Unix?

    thanks,

  • Artem Russakovskii
    Artem Russakovskii almost 5 years
    May I suggest adding -a to tree to show hidden files?
  • Artem Russakovskii
    Artem Russakovskii almost 5 years
    Any idea how to make this one-liner work with diff -y? For some reason, instead of printing the diff side by side, it prints pretty much everything.
  • Artem Russakovskii
    Artem Russakovskii almost 5 years
    Made it into a handy function: dirdiff () { diff <(cd $1; tree -afpiug ) <(cd $2; tree -afpiug) ;}. Stick this into your .alias or .bashrc or whatever script runs at shell startup and then use like so: dirdiff dir1 dir2.