2

I have three copies of the same folder but the sizes of these are different. Why?

I have a folder "Documents" that I have copied from an encrypted lvm on the hard drive of my desktop to an encrypted luks partition on an external drive. I have then copied this folder from the external drive to an encrypted lvm on the hard drive of my laptop.

So I have three copies of the same folder:

  • Desktop
  • External drive
  • Laptop

I have used diff between desktop and external drive --> no differences.

I have used diff between laptop and external drive --> no differences.

But I have different folder sizes for the three cases.

I have used

du -bs --apparent-size Documents

to measure the sizes. Here are the results:

  • Desktop: 1735750760
  • External drive: 1735742568
  • Laptop: 1735746664

So, we notice that there is a slight difference between the sizes. Small difference but still there. I was wondering why that was so. Do you have an explanation?

1 Answer 1

3

You might have noticed that the difference between the different folders are as follows:

    Desktop:        1735750760
                  -       8192
    External drive: 1735742568  
                  +       4096  
    Laptop:         1735746664  

To my experience, such differences of 4K blocks usually result from changes of directory sizes. If you compare the sizes of the directories itself (on ext filesystems usually multiples of 4K) you might find the difference.
(e.g. directories sometimes get smaller when they are restructured during a copy operation and deleted entries are thereby purged)

To find Directories which differ in size, you would have to compare to Directories against each other. The following script could assist you with that:

#!/bin/bash
DIR1="${1%/}/"
DIR2="${2%/}/"

diff \
  <(find $DIR1 -type d -printf "%k %p\n" | sed -e "s|$DIR1||" | sort -k2) \
  <(find $DIR2 -type d -printf "%k %p\n" | sed -e "s|$DIR2||" | sort -k2)

Copy the code to a new file (e.g. dircomp.sh) and make it executable chmod +x dircomp.sh. Then call it like ./dircomp.sh /home/lemenhir/ /media/lemenhir/ExternalDrive/.

The output should now list only directories with different sizes and could look like this:

< 4 Documents/Appartement/Bail
> 8 Documents/Appartement/Bail

whereby lines starting with < are listing directories located in the directory given as the first parameter (e.g. /home/lemenhir/) and lines starting with > are listing directories located in the directory given as the second parameter (e.g. /media/lemenhir/ExternalDrive/).

If single lines appear, then the directory doesn't exist in the opposite directory.

Hope this helps.

8
  • Ah, well noticed! I didn't see that pattern of changes of multiples of 4096. Do you have an idea on how to find folders that have a size of 4096 or folders that have a size of 8192?
    – le menhir
    Apr 11, 2020 at 16:38
  • It's not that easy. ;-) You are looking for folders which have a different size between the three folders. If the folders are still the same and not modified in any kind, you could compare two of them like this: diff <(find DIR1/ -type d -printf "%k %p\n" | sort -k2) <(find DIR2/ -type d -printf "%k %p\n" | sort -k2) , whereby you would have to replace DIR1 and DIR2 with the path of the two directories you want to compare. The number before the directories name is the amount of 1K blocks used by the directory.
    – bey0nd
    Apr 12, 2020 at 9:49
  • Ok, I tried your code, thank you. But how is it a comparison? It gives a number of 1K blocks for a directory, but from which folder? Dir1 or Dir2? Ins't there a way to list only where there is a difference in a folder for DIR1 and DIR2? Sorry, I don't know much about shell scripts.
    – le menhir
    Apr 12, 2020 at 11:14
  • If you run find DIR1/ -type d -printf "%k %p\n" | sort -k2, you'll get a list of all directories within DIR1/. diff compares the two lists created by the two <(...) subsections of the command. The output from diff provides you with two lines that differ between the two lists. Lines starting with '<' come from DIR1/. lines starting with '>' from DIR2/. If you only get single lines, than this line is missing in the opposite list.
    – bey0nd
    Apr 12, 2020 at 11:24
  • ok, thanks for the clarification. But at the moment, i get a line with < and a line with > for every sub-folder even if the number of 1K blocks is the same. All < and then all >. How can you do that with only different numbers of 1K blocks?
    – le menhir
    Apr 12, 2020 at 18:05

You must log in to answer this question.

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