Last active
April 12, 2016 03:43
-
-
Save tennisonchan/c66e463753d8f5f54268cdfbf8b4ab96 to your computer and use it in GitHub Desktop.
To extract the path and the file name from a fullpath
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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