cmp(1) will of course tell me if the contents of two files are identical, but in order to test restoring from backups I also wanted to compare the relevant (!) file attributes.

So if I did something simple like

mv foo.bar foo.bar.save
deja-dup --restore foo.bar # or some other backup tool

how do I compare the attributes of foo.bar and foo.bar.save and test for sufficient equality in a shell script (or similar). I can do

stat foo.bar{,.save}

and manually inspect the output remembering to ignore inode, atime, and ctime (as well as link count, for some reason?), but this is error prone. Is there a cmp-with-attributes tool somewhere which include SELinux and other attributes? Must work on Fedora and ext4 file systems; ideally on "all" systems. Do I need to hack something up in perl?

(No point having backups if you don't test that they work right.)

  • 1
    stat has two options, -c or --format, and --printf, that you can use to make it print the data you are interested in, in the format you want, so you can later compare it with diff or another tool.
    – angus
    Aug 29 '12 at 9:03
getallattr() {
  lsattr -d -- "$1" | cut -d ' ' -f1
  getfattr -dm- -- "$1" | tail -n +2 | sort
  stat -c '%u %g %a %s %x %y' -- "$1"

To retrieve all the attributes (at least those that can easily be restored). You could also include the md5sum in there.

And then do

diff <(getallattr file1) <(getallattr file2)

(ksh93, zsh, bash syntax).

  • "getfattr -dm -" already takes care of ACLs, so no need for an extra command. Aug 30 '12 at 5:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.