0

How do I print out just the hashsum and file name with sha256sum command? I want Hashsum and just the filename instead of the full path.

Command:

sha256sum /mydir/someOtherDir/file.txt

Output:

123Hashsum /mydir/someOtherDir/file.txt

Desired Output:

123Hashsum file.txt
3
  • 2
    The easiest way to get your expected result might be to use cd /mydir/someOtherDir/; sha256sum file.txt instead of sha256sum /mydir/someOtherDir/file.txt Commented May 3, 2019 at 9:24
  • There are ways to do this, for instance by changing the working directory or post-processing sha256sum's output. However, you may want to explain why you want to do this. If you want to use an aliased version of the command for better readability that's fine. But if you want to parse your expected output it wouldn't make sense to post-process the original output just so that you parse it a second time after that. Also: Do you need support for multiple files in different directories, for instance sha256sum /dir1/file1 /dir2/dir3/file2? Commented May 3, 2019 at 9:25
  • I was testing some C code to get hashsums of files. I know it's better to use C libraries like openssl, but this was just a small hack for a workaround using a system call. The files coming in aren't necessarily in the corresponding folder. Commented May 3, 2019 at 9:40

2 Answers 2

1

You can read the output into variables

read -r sha file < <(sha256sum /mydir/someOtherDir/file.txt)

Then you can read just the filename with basename

echo "$sha" "$(basename "$file")"
Sign up to request clarification or add additional context in comments.

1 Comment

or echo "$sha ${file##*/}" in bash, but not sh
0

You can try piping to sed as below (works with absolute paths only) :

sha256sum /mydir/someOtherDir/file.txt | sed 's:/.*/::'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.