Karan Sharma

Extracting Filename in Bash

0 minutes (111 words)

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

Tags: #Shell