MEMO: Get directory name / filename without extension using shell command

Get directory name / filename without extension using shell command

  • 目次

    How to get directory name / filename without extension from path using shell command.

    Tips

    $ export filepath="./dir/subdir/filename.ext"
    
    # Get directory name
    $ echo $(dirname "$filepath")
    ./dir/subdir
    
    # Get directory name of hierarchy one level up
    $ echo $(echo $filepath | awk -F "/" '{ print $(NF -1) }')
    subdir
    
    # Get filename without extention
    $ echo $(basename $filepath "~" | sed 's/\.[^\.]*$//')
    filname
    

    Reference

    comments powered by Disqus