ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
using Python3, i want to hash the contents of a tree of files to verify that all the files in one tree are the same as all the files in another tree even if the files are in random order, as long as the names are correct with the correct content matching the names. what can you suggest?
using Python3, i want to hash the contents of a tree of files to verify that all the files in one tree are the same as all the files in another tree even if the files are in random order, as long as the names are correct with the correct content matching the names. what can you suggest?
I'd sort the files by name/path, MD5 each one, and store the pair in an array. Do same for other tree, then compare the two arrays. Depending on the number of files, of course; a few hundred should be a huge problem, but if you're talking thousands, it could take a bit, or may require a temp file. Loads of ways to approach.
I’d iterate over the” tree, looks for each file in “the other tree” with the same filename, and do the comparison. The code for that is fairly obvious.
One good speed optimization is to not even look at the file contents unless the files have the same sizes. Reading sizes is a very fast operation.
You can compare the content of two files without calculating any hash. Hint: cmp(1)
how do i use cmp(1) to compare every file (in a directory or tree or whatever) to all the others? the reason i would use a hash is so i don't have to read a file again every time i need to compare it to another file.
I’d iterate over the” tree, looks for each file in “the other tree” with the same filename, and do the comparison. The code for that is fairly obvious.
One good speed optimization is to not even look at the file contents unless the files have the same sizes. Reading sizes is a very fast operation.
the reason i would use a hash is to be able to "compare" the file to many other files. once i have a digest of its contents, comparing digests is nearly as trivial as comparing sizes. compare two digests completes as "unequal" if the first part (potentially 32 bits or 64 bits) is unequal. ISTM that at this point, comparing size is a waste of time though if i were using C i might find a way to integrate such comparisons together.
all i find online is a Windows version. my project is for Linux. i do not find a Linux version in the online search results (first 25 results).
i would be curious if it considers a file hard linked under 2 or more names to be duplicates or not. what i have put together, so far, does not. it does this by recording inodes as file "names", although it also records all links and shows (hard) links of each inode in the results.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.