Karan Sharma

Ramblings on tech, cycling, and finance

Get filename using bash

A little while ago, I'd to extract the filename (without the extension) from a particular JSON file. There'a a neat basename utility that I didn't know about.

However, it cannot accept STDIN so if you're using it with pipes, you need to use xargs.

Get the filename with extension

cat ~/app.json | jq '.file_path' | xargs basename

Remove the extension

cat ~/app.json | jq '.file_path' | xargs basename  -s .txt