md5 目录树中的所有文件 [英] md5 all files in a directory tree
问题描述
我有一个结构如下的目录:
I have a a directory with a structure like so:
.
├── Test.txt
├── Test1
│ ├── Test1.txt
│ ├── Test1_copy.txt
│ └── Test1a
│ ├── Test1a.txt
│ └── Test1a_copy.txt
└── Test2
├── Test2.txt
├── Test2_copy.txt
└── Test2a
├── Test2a.txt
└── Test2a_copy.txt
我想创建一个 bash 脚本,该脚本对该目录中的每个文件进行 md5 校验和.我希望能够在 CLI 中输入脚本名称,然后输入我想要散列的目录的路径并让它工作.我相信有很多方法可以做到这一点.目前我有:
I would like to create a bash script that makes a md5 checksum of every file in this directory. I want to be able to type the script name in the CLI and then the path to the directory I want to hash and have it work. I'm sure there are many ways to accomplish this. Currently I have:
#!/bin/bash
for file in "$1" ; do
md5 >> "${1}__checksums.md5"
done
这只是挂起,它不工作.也许我应该使用 find?
This just hangs and it not working. Perhaps I should use find?
一个警告 - 我要散列的目录将包含具有不同扩展名的文件,并且可能并不总是具有完全相同的树结构.我也想要一些能在这些不同情况下工作的东西.
One caveat - the directories I want to hash will have files with different extensions and may not always have this exact same tree structure. I want something that will work in these different situations, as well.
推荐答案
使用md5deep
md5deep -r path/to/dir > sums.md5
使用 find
和 md5sum
find relative/path/to/dir -type f -exec md5sum {} + > sums.md5
请注意,当您使用 md5sum -c sums.md5
检查 MD5 总和时,您需要从生成 sums.md5<的同一目录中运行它/代码>文件.这是因为
find
输出相对于您当前位置的路径,然后将其放入 sums.md5
文件中.
Be aware, that when you run check on your MD5 sums with md5sum -c sums.md5
, you need to run it from the same directory from which you generated sums.md5
file. This is because find
outputs paths that are relative to your current location, which are then put into sums.md5
file.
如果这是一个问题,您可以使 relative/path/to/dir
绝对(例如,通过将 $PWD/
放在路径前面).这样您就可以从任何位置对 sums.md5
运行检查.缺点是,现在 sums.md5
包含绝对路径,这让它变大了.
If this is a problem you can make relative/path/to/dir
absolute (e.g. by puting $PWD/
in front of your path). This way you can run check on sums.md5
from any location. Disadvantage is, that now sums.md5
contains absolute paths, which makes it bigger.
你可以把这个函数放到你的 .bashrc
文件中(位于你的 $HOME
目录中):
You can put this function to your .bashrc
file (located in your $HOME
directory):
function md5sums {
if [ "$#" -lt 1 ]; then
echo -e "At least one parameter is expected
"
"Usage: md5sums [OPTIONS] dir"
else
local OUTPUT="checksums.md5"
local CHECK=false
local MD5SUM_OPTIONS=""
while [[ $# > 1 ]]; do
local key="$1"
case $key in
-c|--check)
CHECK=true
;;
-o|--output)
OUTPUT=$2
shift
;;
*)
MD5SUM_OPTIONS="$MD5SUM_OPTIONS $1"
;;
esac
shift
done
local DIR=$1
if [ -d "$DIR" ]; then # if $DIR directory exists
cd $DIR # change to $DIR directory
if [ "$CHECK" = true ]; then # if -c or --check option specified
md5sum --check $MD5SUM_OPTIONS $OUTPUT # check MD5 sums in $OUTPUT file
else # else
find . -type f ! -name "$OUTPUT" -exec md5sum $MD5SUM_OPTIONS {} + > $OUTPUT # Calculate MD5 sums for files in current directory and subdirectories excluding $OUTPUT file and save result in $OUTPUT file
fi
cd - > /dev/null # change to previous directory
else
cd $DIR # if $DIR doesn't exists, change to it to generate localized error message
fi
fi
}
运行source ~/.bashrc
后,就可以像普通命令一样使用md5sums
了:
After you run source ~/.bashrc
, you can use md5sums
like normal command:
md5sums path/to/dir
会在path/to/dir
目录下生成checksums.md5
文件,包含该目录及子目录下所有文件的MD5总和.使用:
will generate checksums.md5
file in path/to/dir
directory, containing MD5 sums of all files in this directory and subdirectories. Use:
md5sums -c path/to/dir
从 path/to/dir/checksums.md5
文件中检查总和.
to check sums from path/to/dir/checksums.md5
file.
请注意,path/to/dir
可以是相对的或绝对的,md5sums
无论如何都可以正常工作.生成的 checksums.md5
文件始终包含相对于 path/to/dir
的路径.您可以通过提供 -o
或 --output
选项来使用与默认 checksums.md5
不同的文件名.除了 -c
、--check
、-o
和 --output
之外的所有选项都传递给 <代码>md5sum.
Note that path/to/dir
can be relative or absolute, md5sums
will work fine either way. Resulting checksums.md5
file always contains paths relative to path/to/dir
.
You can use different file name then default checksums.md5
by supplying -o
or --output
option. All options, other then -c
, --check
, -o
and --output
are passed to md5sum
.
md5sums
函数定义的前半部分负责解析选项.有关详细信息,请参阅此答案.后半部分包含解释性评论.
First half of md5sums
function definition is responsible for parsing options. See this answer for more information about it. Second half contains explanatory comments.
这篇关于md5 目录树中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!