Bash 获取文件夹中所有文件的 md5sum
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11114968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash get md5sum of all files in a folder
提问by user577732
Hi I'm looking to see what file is changing in a directory i'd like to get the md5sum of every file and write it to a text file. Then after i know a file has changed i'd like to run it again so i can diff the output files to see what exactly changed. Here is what i've tried however it doesn't work as i need.
嗨,我正在查看目录中的哪些文件正在更改,我想获取每个文件的 md5sum 并将其写入文本文件。然后在我知道文件已更改后,我想再次运行它,以便我可以比较输出文件以查看究竟发生了什么变化。这是我尝试过的,但是它不能按我的需要工作。
Also not only do i need to get the md5sum of every file in a folder including subdirectories i need it to not follow symlinks
此外,我不仅需要获取文件夹中每个文件的 md5sum,包括子目录,我需要它不遵循符号链接
#!/bin/bash
#
cd /sys/class
for i in $(find . -type f)
do
ls -lt "$i" >> /home/george/Desktop/before.txt
done
echo "Finished!"
Thank you for any help
感谢您的任何帮助
===Edit===
===编辑===
I put my actual paths in as i don't really see a need to hide them. Anyway running this returned only a few files (outputted file below) which are the files in the folders meaning it's not going into subdirectories and finding those files too. Btw sorry my bash is way rusty
我把我的实际路径放进去,因为我真的认为没有必要隐藏它们。无论如何,运行它只返回几个文件(下面输出的文件),这些文件是文件夹中的文件,这意味着它不会进入子目录也不会找到这些文件。顺便说一句对不起,我的 bash 生锈了
--w------- 1 root root 4096 Jun 20 03:03 ./gpio/export
--w------- 1 root root 4096 Jun 20 03:03 ./gpio/unexport
-rw-r--r-- 1 root root 4096 Jun 20 03:03 ./firmware/timeout
-r--r--r-- 1 root root 4096 Jun 20 03:04 ./drm/version
===Edit2===
===编辑2===
Not exactly sure why some of these files aren't being found for instance /sys/class/backlight/intel_backlight/brightness
不完全确定为什么找不到其中一些文件,例如 /sys/class/backlight/intel_backlight/brightness
And many others like that there are so many files that aren't being found for some reason
和许多其他人一样,由于某种原因,有很多文件没有被找到
回答by tripleee
The cdis unnecessary, and with type -fyou are already in fact bypassing symlinks. So the loop is unnecessary, too:
这cd是不必要的,type -f实际上你已经绕过了符号链接。所以循环也是不必要的:
find /path/to/directory -type f -exec md5sum {} + >before.txt
If your findis too old to support -exec {} +try with -exec {} \;instead.
如果您find太老而无法支持,请-exec {} +尝试使用-exec {} \;。
For the md5sumcomparison, you could try simply removing identical lines;
为了进行md5sum比较,您可以尝试简单地删除相同的行;
fgrep -vxf before.txt after.txt | less
This is assuming the list in before.txtwill fit into fgrep; but if you are dealing with a few dozen thousand files tops, it can probably cope. This will not identify deleted files from before.txt, though.
这是假设列表before.txt中将适合fgrep; 但如果你正在处理几十万个文件,它可能可以应付。但是,这不会从 中识别已删除的文件before.txt。
回答by Miquel
If your file list size is small enough that you can do it all in memory, you might consider sorting before.txt by the hash. If you do the same for after.txtyou'd be able to go line by line on each of the files and identify matches even if the filename has changed. You'd also be able to skip over deleted or added files with less problems than if you had to interpret a diff before.txt after.txt
如果您的文件列表大小足够小,您可以在内存中完成所有操作,您可以考虑按哈希对 before.txt 进行排序。如果您这样做,after.txt即使文件名已更改,您也可以逐行处理每个文件并识别匹配项。您还可以跳过已删除或添加的文件,问题比必须解释diff before.txt after.txt
If using file modification date is an option, what you can do is use ls -lt | headto filter out the newest file and keep that. Then when you want to check for changes, ls -ltagain and go through anything that's newer than the date you stored. This should work nicely regardless of file list size, but will be vulnerable to someone modifying last modification date (which would require root privileges)
如果使用文件修改日期是一个选项,您可以做的是使用ls -lt | head过滤掉最新的文件并保留它。然后,当您想要检查更改时,ls -lt再次检查比您存储的日期更新的任何内容。无论文件列表大小如何,这都应该可以很好地工作,但是很容易受到修改最后修改日期的人的影响(这需要 root 权限)

