Search code examples
linuxbashmvmd5sum

How to rename files with output from program in bash?


I've been trying to find a simple, one-line solution to rename all files in a directory with their md5 hash. I've thought about using find -exec but I don't know how to pipe the results of md5sum to mv. I've tried permutations of

find . -exec md5sum {} | mv {} \;

and

find . -exec mv {} `md5sum {}`

Perhaps there's a better way of going about it? Please advice a total beginner. All help is appreciated.


Solution

  • You can reuse the answer from: Rename files to md5 sum + extension (BASH)

    By modifying a little bit it will do exactly what you want (I already edited the command for you):

    md5sum * | sed -e 's/\([^ ]*\)  \(.*\)$/mv -v "\2" \1/' | sh
    

    Example of output:

    'a' -> 'b026324c6904b2a9cb4b88d6d61c81d1'
    'b' -> '26ab0db90d72e28ad0ba1e22ee510510'
    'c' -> '6d7fce9fee471194aa8b5b6e47267f03'