Skip to content

Instantly share code, notes, and snippets.

@tennisonchan
Last active April 12, 2016 03:43
Show Gist options
  • Save tennisonchan/c66e463753d8f5f54268cdfbf8b4ab96 to your computer and use it in GitHub Desktop.
Save tennisonchan/c66e463753d8f5f54268cdfbf8b4ab96 to your computer and use it in GitHub Desktop.
To extract the path and the file name from a fullpath
fullpath_name="/usr/local/a-file-name"
filename1=${fullpath_name##*/}
pathname1=${fullpath_name%/*}
absolute_path=$(readlink -f "$0")
echo $filename1 # return a-file-name
echo $pathname1 # return /usr/local/
echo $absolute_path # return /usr/local/a-file-name
### OR using basename and dirname
filename2=$(basename $fullpath_name)
pathname2=$(dirname $0)
pathname3=$(dirname $absolute_path)
echo $filename2 # return a-file-name
echo $pathname2 #return /usr/local/
echo $pathname3 #return /usr/local/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment