8

I am tryng to do checksum on all .jar files I can find in directory and its sub directories. Then print the filename with the checksum value to a file.

this is what I have.

md5sum | find -name *.jar >result.txt

I am trying to join two commands together that I know work individually.

Any help appreciated.

Nat Ritmeyer
  • 5,634
  • 8
  • 45
  • 58
James Mclaren
  • 666
  • 4
  • 10
  • 25

3 Answers3

13

You could use something like this to execute a command on each file:

find . -name "*.jar" -exec md5sum {} \; >result
iabdalkader
  • 17,009
  • 4
  • 47
  • 74
  • Thank you for this. It worked perfect. Any addition, any way to only write the file name in the file and not the full folder path. – James Mclaren Feb 08 '13 at 16:41
  • @JamesMclaren yes if you add `-printf "%f\n"` it will print just the filename but I don't think it will work as `md5sum` needs the full path, you could remove the path after executing the command with `sed` or `awk` – iabdalkader Feb 08 '13 at 17:00
3

This will also work to recursively hash all files in the current directory or sub-directories (thanks to my sysadmin!):

md5sum $(find . -name '*.jar') > result.txt

The above will prepend "./" to the filename (without including the path).

Using the -exec suggestion from mux prepends "*" to the filename (again, without the path).

The listed file order also differed between the two, but I am unqualified to say exactly why, since I'm a complete noob to bash scripting.

Edit: Forget the above regarding the prepend and full path, which was based on my experience running remotely on an HPC. I just ran my sysadmin's suggestion on my local Windows box using cygwin and got the full path, with "*./" prepended. I'll need to use some other fanciness to dump the inconsistent path and prepending, to make the comparison easier. In short, YMMV.

kwolson
  • 31
  • 3
1

You can also pipe the results to xargs:

find . -name "*.jar" | xargs md5sum > result.txt
Adam Erickson
  • 6,027
  • 2
  • 46
  • 33