I want output like this: name size and hash:

myfile.txt 222M 24f4ce42e0bc39ddf7b7e879a
mynewfile.txt 353M a274613df45a94c3c67fe646

For name and size only I have

ll -h | awk '{print $9,$10,$11,$12,$5}'

But how can I get hash for every file? I tried:

ll -h | awk '{print $9,$10,$11,$12,$5}' | md5sum

But I get only one hash at all.


You should not parse ls, instead use this:

for f in * .*; do
  [ -f "$f" ] && \
    printf "%s %s %s\n" "$f" $(du -h -- "$f" | cut -f1) $(md5sum -- "$f" | cut -d' ' -f1)
  • The for loop runs trough all files and directories in the current directory.
    • [ -f "$f" ] checks if it's a regular file
    • printf "%s %s %s\n" prints the arguments in the desired format.
    • "$f" the first argument is the filename
    • du -h -- "$f" | cut -f1 the second is the size (human readable), but not the filename, cut cuts all excep the first field away
    • md5sum -- "$f" | cut -d' ' -f1 third is the MD5 sum, but without the filename.
  • Thank you! But how fix this for file names with spaces? – Vitaly Zdanevich Oct 31 '15 at 21:55
  • 1
    @VitalyZdanevich See my edit – chaos Oct 31 '15 at 22:24
  • i would also suggest using tab as the field separator "%s\t%s\t%s\n" so that spaces in the filename can be distinguished from the field-separators. Of course, filenames can contain tabs too but they'e far less common - no easy way to create them. – cas Oct 31 '15 at 22:47
  • 1
    If this output might get re-parsed, maybe use a "/" field separator, as that can't show up in a file name. – Jeff Schaller Nov 1 '15 at 1:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.