kilamondrow
kilamondrow

Reputation: 31

Filter output of command in Linux

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

Upvotes: 0

Views: 327

Answers (2)

nullPointer
nullPointer

Reputation: 4574

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

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

Upvotes: 0

fancyPants
fancyPants

Reputation: 51888

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")"

Upvotes: 1

Related Questions