8

running sha256sum folder/file` returns

711ad4b9939e0e20e591d753103717f40e794babc4129a0670fd342309bec5af *folder/file

I want to output the sum with just the filename next to it so that it looks like:

711ad4b9939e0e20e591d753103717f40e794babc4129a0670fd342309bec5af file

How do I go about changing the output?

1
  • 2
    sed is your friend - you'll have to get somebody else to tell you how to use it ;)
    – new123456
    Apr 20, 2011 at 21:33

3 Answers 3

11

You could try

sha256sum /path/to/file | head -c 64

This is for taking only the hash of 64 characters. As there were mentioned before, you should read the man of sed for more advanced manipulation.

But to have the output that you want, I mean without folders, a simple way is running the command from the folder where the file is located ;)

In your case:

cd folder/ ; sha256sum file
1
  • 1
    To get the first part before the space, regardless of it's length (not just 64), yes, sed can be used, or cut, or awk... I personally find cut most readable. sha256sum /path/to/file | sed 's/ .*//' OR sha256sum /path/to/file | cut -d' ' -f1 OR sha256sum /path/to/file | awk -F' ' '{print $1}'
    – Do-do-new
    Dec 4, 2021 at 15:01
3

Something like

sha256sum folder/file | sed 's, .*/, ,'

should do the job. I didn't test it though.

1
  • tested it and it works :)
    – rjdkolb
    Jul 13, 2017 at 9:59
0

Don't run sha256sum folder/file then. Change to the directory first:

( cd folder && sha256sum file )

You must log in to answer this question.

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