提取文件名和目录

提取文件名和目录

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
posted @ 2016-07-26 16:52  夏日花开  阅读(222)  评论(0)    收藏  举报