Search code examples
linuxpipemd5sum

Linux piping find and md5sum not sending output


Trying to loop every file, do some cutting, extract the first 4 characters of the MD5.

Here's what I got so far:

find . -name *.jpg | cut -f4 -d/ | cut -f1 -d. | md5sum | head -c 4

Problem is, I don't see any more output at this point. How can I send output to md5sum and continue sending the result?


Solution

  • md5sum reads everything from stdin till end of file (eof) and outputs md5 sum of full file. You should separate input into lines and run md5sum per line, for example with while read var loop:

    find . -name *.jpg | cut -f4 -d/ | cut -f1 -d. | 
      while read -r a; 
       do   echo -n $a| md5sum | head -c 4; 
      done
    

    read builtin bash command will read one line from input into shell variable $a; while loop will run loop body (commands between do and done) for every return from read, and $a will be the current line. -r option of read is to not convert backslash; -n option of echo command will not add newline (if you want newline, remove -n option of echo).

    This will be slow for thousands of files and more, as there are several forks/execs for every file inside loop. Faster will be some scripting with perl or python or nodejs or any other scripting language with builtin md5 hash computing (or with some library).