Search code examples
bashmarkdownjekyllgit-bashhugo

Omit file extension from slug


I am using this bash script to add a markdown file name as a slug in the front matter. However, I would like to omit the extension .md and retain the name only e.g. foo.md I retain foo. Could anyone advise me on how to make this script work?

for f in $(grep --include=*.md -riL "slug:" /your_directory/); 
do sed -i "/date:.*/aslug: \/$(basename $f)\/" $f;
done

Solution

  • The version of basename on my Ubuntu machine will strip a suffix if you provide it, e.g.

    basename path/to/some.md .md
    #=> some
    

    Try changing your sed command accordingly:

    for f in $(grep --include=*.md -riL "slug:" /your_directory/); do
        sed -i "/date:.*/aslug: \/$(basename $f .md)\/" $f;
    done