当您知道其校验和时查找文件?

Kiw*_*iwy 33 find aix hashsum

我有md5sum一个文件,但我不知道它在我的系统上的什么位置。是否有任何简单的选项find可以根据文件来识别文件md5?还是我需要开发一个小脚本?

我正在不使用 GNU 工具的 AIX 6 上工作。

Rah*_*til 37

使用find

find /tmp/ -type f -exec md5sum {} + | grep '^file_md5sum_to_match'
Run Code Online (Sandbox Code Playgroud)

如果您进行搜索,/则可以排除/proc/sys查看以下find命令示例:

我也做了一些测试,find花更多的时间和更少的 CPU 和 RAM,而 ruby​​ 脚本花费的时间更少,但更多的 CPU 和 RAM

测试结果

[root@dc1 ~]# time find / -type f -not -path "/proc/*" -not -path "/sys/*" -exec md5sum {} + | grep '^304a5fa2727ff9e6e101696a16cb0fc5'
304a5fa2727ff9e6e101696a16cb0fc5  /tmp/file1


real    6m20.113s
user    0m5.469s
sys     0m24.964s
Run Code Online (Sandbox Code Playgroud)

查找与 -prune

[root@dc1 ~]# time find / \( -path /proc -o -path /sys \) -prune -o -type f -exec md5sum {} + | grep '^304a5fa2727ff9e6e101696a16cb0fc5'
304a5fa2727ff9e6e101696a16cb0fc5  /tmp/file1

real    6m45.539s
user    0m5.758s
sys     0m25.107s
Run Code Online (Sandbox Code Playgroud)

红宝石脚本

[root@dc1 ~]# time ruby findm.rb
File Found at: /tmp/file1

real    1m3.065s
user    0m2.231s
sys     0m20.706s
Run Code Online (Sandbox Code Playgroud)


Rah*_*til 12

脚本解决方案

#!/usr/bin/ruby -w

require 'find'
require 'digest/md5'

file_md5sum_to_match = [ '304a5fa2727ff9e6e101696a16cb0fc5',
                         '0ce6742445e7f4eae3d32b35159af982' ]

Find.find('/') do |f|
  next if /(^\.|^\/proc|^\/sys)/.match(f) # skip
  next unless File.file?(f)
  begin
        md5sum = Digest::MD5.hexdigest(File.read(f))
  rescue
        puts "Error reading #{f} --- MD5 hash not computed."
  end
  if file_md5sum_to_match.include?(md5sum)
       puts "File Found at: #{f}"
       file_md5sum_to_match.delete(md5sum)
  end
  file_md5sum_to_match.empty? && exit # if array empty then exit

end
Run Code Online (Sandbox Code Playgroud)

基于概率更快的 Bash 脚本解决方案

#!/bin/bash
[[ -z $1 ]] && read -p "Enter MD5SUM to search file: " md5 || md5=$1

check_in=( '/home' '/opt' '/tmp' '/etc' '/var' '/usr'  )
last_find_cmd="find / \\( -path /proc -o -path /sys ${check_in[@]/\//-o -path /} \\) -prune -o -type f -exec md5sum {} +"
last_element=${#check_in}
echo "Please wait... searching for file"
for d in ${!check_in[@]}
do

        [[ $d == $last_element ]] && eval $last_find_cmd | grep "^${md5}" && exit

        find ${check_in[$d]} -type f -exec md5sum {} + | grep "^${md5}" && exit


done
Run Code Online (Sandbox Code Playgroud)

测试结果

[root@dc1 /]# time bash find.sh 304a5fa2727ff9e6e101696a16cb0fc5
Please wait... searching for file
304a5fa2727ff9e6e101696a16cb0fc5  /var/log/file1

real    0m21.067s
user    0m1.947s
sys     0m2.594s
Run Code Online (Sandbox Code Playgroud)


Ant*_*hon 7

如果您决定安装 gnu find 无论如何(并且因为您表示对您的评论之一感兴趣),您可以尝试以下操作:

find / -type f \( -exec checkmd5 {} YOURMD5SUM \; -o -quit \) 
Run Code Online (Sandbox Code Playgroud)

并将checkmd5它作为参数获取的文件的 md5sum 与第二个参数进行比较,如果匹配则打印名称并以 1 退出(否则为 0)。该-quitfind一旦发现停。

checkmd5 (未测试):

#!/bin/bash

md=$(md5sum $1 |  cut -d' ' -f1)

if [ $md == $2 ] ; then
  echo $1
  exit 1
fi
exit 0
Run Code Online (Sandbox Code Playgroud)