Skip to content

Instantly share code, notes, and snippets.

@huzhifeng
Created May 11, 2014 01:40
Show Gist options
  • Save huzhifeng/d039e7300d6900a50bae to your computer and use it in GitHub Desktop.
Save huzhifeng/d039e7300d6900a50bae to your computer and use it in GitHub Desktop.
Extract filename, extension and dir in bash, Refer to http://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash
#!/bin/bash
full_path="/var/www/public/ch1.sec1.index.html"
dir=${full_path%/*}
filename=$(basename $full_path)
filename_with_ext=${full_path##*/}
filename_without_ext=${filename_with_ext%.*}
ext=${full_path##*.}
echo "Full path: $full_path" # /var/www/public/ch1.sec1.index.html
echo "Dir: $dir" # /var/www/public
echo "File name: $filename_with_ext" # ch1.sec1.index.html
echo "File name without ext: $filename_without_ext" # ch1.sec1.index
echo "Ext: $ext" # html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment