Preface or today to write an automated packaging script, using the path name to get the last file name. Here is a record of the implementation process. Of course, in the end I will also give the official approach. (PS: Very embarrassing, realized that the original bash Shell has a ready-made function)
The get file name assumes that the given path name is:
/tmp/csdn/zhengyi/test/zhengyi.txt
The awk solution uses a "/" delimiter, and then prints out the last part. The implementation code is as follows:
Resfile= ' Echo/tmp/csdn/zhengyi/test/adb.log | Awk-f "/" ' {Print $NF} '
The official solution (basename) Bash shell itself provides the basename command, which allows you to get the last file name of the pathname directly, implementing the code as follows:
Resfile= ' Basename/tmp/csdn/zhengyi/test/adb.log '
Get Directory Name official solution (dirname) The Bash shell itself provides the dirname command, which is particularly handy to get the directory name of the path directly, implementing the code as follows:
Dirpath= ' Dirname/tmp/csdn/zhengyi/test/adb.log '
The awk solution allows for flexible use of delimiters, mixed with regular expressions:
Dirpath= ' Echo/tmp/csdn/zhengyi/test/adb.log | Awk-f '/[^/]*$ ' {print '} '
Awk+for Loop Method:
Echo/tmp/csdn/zhengyi/test/adb.log | awk ' begin{res= '; fs= "/";} {for (i=2;i<=nf-1;i++) res= (res "/" $i);} End{print Res} '
Bash Shell Parse path get file name and directory name