1

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.

1 Answer 1

5

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)
done
  • 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.
4
  • Thank you! But how fix this for file names with spaces? Oct 31, 2015 at 21:55
  • 1
    @VitalyZdanevich See my edit
    – chaos
    Oct 31, 2015 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, 2015 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, 2015 at 1:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .