I have to find specific file with known sha1 sum. I know in which folder the file should be, but there are sub-folders (up to max-depth 4). I know more or less parts of filename (contains words "project" and "screenshoot"), but there are various possible file formats (.ods, .docx, .pdf ...). And of course I know what sha1 sum it has. How to find it?
I have to do this for about 15 files.
find + grep
Use find command
The way this works is as follows:
find
will operate recursively on/that/directory
-type f
allows us to filter out only regular filesexec sha1sum {} \;
will performsha1sum
command with each file as argument ( which is what{}
brackets signify )grep 'known sha1sum'
allows us to filter the output offind
command to get the line of output with the sha1 hashsum that we need.Bash's globstar
Another things that could be done, is to use
bash
'sglobstar
to enable recursive globbing, and iterate that way. Here's how I would search for a file with known sha1sumInstead of iterating via for loop, we can make this even shorter:
While this method might be short, I would be skeptical of this method on a directory with large amount of files, where glob might expand outside of range of maximum amount of command-line arguments. Caveat emptor
Python 3
Of course being a Python aficionado, I couldn't leave without providing a python script for this task. This script takes multiple arguments, so you can specify multiple sha1sums that you need to find, which aligns with the requirement of the question for doing this task for multiple files.
Note that the script assumes you want to search from current working directory down to subdirectories, so ensure you
cd
to desired top directory firstTest run:
This script is also available on my personal GitHub respository, where further development and changes will be added to this script.
How about a combination of
find
,sha1sum
andgrep
: