Skip to content

Instantly share code, notes, and snippets.

@tousinn
Last active December 11, 2015 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tousinn/4679043 to your computer and use it in GitHub Desktop.
Save tousinn/4679043 to your computer and use it in GitHub Desktop.
How to get the File Name, Extension of the File , the File Path under shell
1.How to get the File Name
test_full_file_path="/home/doc/myfile.txt"
file_name=`basename ${test_full_file_path}`
or
file_name=${test_full_file_path##*/}
2.How to get the extension of the file
test_full_file_path="/home/doc/myfile.txt"
file_name=`basename ${test_full_file_path}`
extension_name=${file_name##*.}
3.How to get the file name without the extension
test_full_file_path="/home/doc/myfile.txt"
file_name=`basename ${test_full_file_path}`
file_name_without_extinsion=${file_name%.*}
4.How to get the File Path
test_full_file_path="/home/doc/myfile.txt"
file_path=${test_full_file_path%/*}
【##】:the longest string that matches the pattern from the front of the original string will be deleted
【%】: the shortest string that matches the pattern from the back of the original string will be deleted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment