Has Directory Content Changed?

Undistraction

How can I check a directory to see if its contents has changed since a given point in time?

I don't need to be informed when it changes, or what has changed. I just need a way to check if it has changed.

struthersneil

If you just need to know if names have changed or files have been added/removed, you can try this:

Dir.glob('some_directory/**/*').hash

Just store and compare the hash values. You can obviously go further by getting more information out of a call to ls, for example, or out of File objects that represent each of the files in your directory structure, and hashing that.

Dir.glob('some_directory/**/*').map { |name| [name, File.mtime(name)] }.hash

UM ACTUALLY I'm being dumb and hash is only consistent for any one runtime environment of ruby. Let's use the standard Zlib::crc32 instead, e.g.

Zlib::crc32(Dir.glob('some_directory/**/*').map { |name| [name, File.mtime(name)] }.to_s)

My concern is that this approach will be memory-hungry and slow if you're checking a very large filesystem. Perhaps globbing the entire structure and mapping it isn't the way--if you have a lot of subdirectories you could walk them recursively and calculate a checksum for each, then combine the checksums.

This might be better for larger directories:

Dir.glob('some_directory/**/*').map do |name| 
  s = [name, File.mtime(name)].to_s
  [Zlib::crc32(s), s.length]
end.inject(Zlib::crc32('')) do |combined, x| 
  Zlib::crc32_combine(combined, x[0], x[1])
end

This would be less prone to collisions:

Dir.glob('some_directory/**/*').map do |name| 
  [name, File.mtime(name)].to_s
end.inject(Digest::SHA512.new) do |digest, x| 
  digest.update x
end.to_s

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to determine if any file in a directory has changed

From Dev

How to determine if any file in a directory has changed

From Dev

inotify: Echo which file has changed in directory

From Dev

Why working directory has not been changed with my script?

From Dev

ListFragment IllegalStateException: "The content of the adapter has changed but ListView did not receive the notification"

From Dev

IllegalStateException:The content of the adapter has changed but ListView did not receive a notification

From Dev

Removing content from a set of inputs when input has changed

From Dev

A filename has an asterisk for some reason - it won't be changed and content not executed

From Dev

IllegalStateException:The content of the adapter has changed but ListView did not receive a notification

From Dev

git diff is showing full file has changed for a single line change but only for few files in a directory

From Dev

How to set the folder name of the installation folder set to default even though directory has been changed in ISWIX?

From Dev

How to resolve "The content of the adapter has changed but ListView did not receive a notification” exception

From Dev

The content of the adapter has changed but ListView did not receive a notification: Cross-Threading issue?

From Dev

How to resolve "The content of the adapter has changed but ListView did not receive a notification” exception

From Dev

How to resolve the error “The content of the adapter has changed but ListView did not receive a notification” in Expandable Listview?

From Dev

java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification - List with Universal Image Loader

From Dev

java.lang.IllegalStateException, The content of the adapter has changed but ListView did not receive a notification

From Dev

i get the Error: java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification

From Dev

home directory changed

From Dev

Event for ckeditor content changed

From Dev

if condition changed by its content

From Dev

Indication whether the content is changed

From Dev

Copy file with changed content

From Dev

database schema has changed

From Dev

Check if an object has changed

From Dev

NSFontAttributeName has changed to String

From Dev

has a file changed of value

From Dev

.ASPX Has Changed Check

From Dev

Determine if a file has changed

Related Related

  1. 1

    How to determine if any file in a directory has changed

  2. 2

    How to determine if any file in a directory has changed

  3. 3

    inotify: Echo which file has changed in directory

  4. 4

    Why working directory has not been changed with my script?

  5. 5

    ListFragment IllegalStateException: "The content of the adapter has changed but ListView did not receive the notification"

  6. 6

    IllegalStateException:The content of the adapter has changed but ListView did not receive a notification

  7. 7

    Removing content from a set of inputs when input has changed

  8. 8

    A filename has an asterisk for some reason - it won't be changed and content not executed

  9. 9

    IllegalStateException:The content of the adapter has changed but ListView did not receive a notification

  10. 10

    git diff is showing full file has changed for a single line change but only for few files in a directory

  11. 11

    How to set the folder name of the installation folder set to default even though directory has been changed in ISWIX?

  12. 12

    How to resolve "The content of the adapter has changed but ListView did not receive a notification” exception

  13. 13

    The content of the adapter has changed but ListView did not receive a notification: Cross-Threading issue?

  14. 14

    How to resolve "The content of the adapter has changed but ListView did not receive a notification” exception

  15. 15

    How to resolve the error “The content of the adapter has changed but ListView did not receive a notification” in Expandable Listview?

  16. 16

    java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification - List with Universal Image Loader

  17. 17

    java.lang.IllegalStateException, The content of the adapter has changed but ListView did not receive a notification

  18. 18

    i get the Error: java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification

  19. 19

    home directory changed

  20. 20

    Event for ckeditor content changed

  21. 21

    if condition changed by its content

  22. 22

    Indication whether the content is changed

  23. 23

    Copy file with changed content

  24. 24

    database schema has changed

  25. 25

    Check if an object has changed

  26. 26

    NSFontAttributeName has changed to String

  27. 27

    has a file changed of value

  28. 28

    .ASPX Has Changed Check

  29. 29

    Determine if a file has changed

HotTag

Archive