basename
命令用于去掉文件名的目录和后缀(strip directory and suffix from filenames),对应的dirname命令用于截取目录
示例一 # 获取到最后文件名sort
[root@local ~]# basename /usr/bin/sort
sort
示例二 # 去除文件名后缀
[root@local ~]# basename /usr/include/stdio.h .h
stdio
[root@local ~]# basename /usr/include/stdio.h stdio.h
stdio.h
示例三 # 去除文件名后缀方式的另外一种方法
[root@local ~]# basename -s .h /usr/include/stdio.h
stdio
示例四 # 获取多个目录下的文件列表,以换行符\n为分隔
[root@local ~]# basename -a dir1/file1 dir2/file2
file1
file2
示例五 # 获取多个目录下的文件列表,以NUL为分隔
[root@local ~]# basename -a -z dir1/file1 dir2/file2
file1file2
批量获取某文件夹下所有json文件的文件名
find ./ -name '*.json' | xargs basename -s .json > file.json