提取文件名和目录

提取文件名和目录

1)提取文件名

[root@server0 thread]# echo $var
/mnt/thread/2.py
[root@server0 thread]# echo ${var##*/}
2.py

2)提取目录

[root@server0 thread]# echo $var
/mnt/thread/2.py
[root@server0 thread]# echo ${var%/*}
/mnt/thread

3)提取后缀

[root@server0 thread]# tmp=${var##*/}
[root@server0 thread]# echo ${tmp#*.}
Py

/mnt/thread/2.py
[root@server0 thread]# echo $tmp
2.py
[root@server0 thread]# echo ${tmp%.*}
2

用命令提取文件和目录名
用basename提取文件名

[root@server0 thread]# echo $var
/mnt/thread/2.py
[root@server0 thread]# echo $(basename $var)
2.Py
3.`

提取不带后缀的文件名

用dirname提取目录名

[root@server0 thread]# dirname $var
/mnt/thread
原文地址:https://www.cnblogs.com/hanfei-1005/p/5708068.html