1.需求描述
現存在以下文件列表:
[root@localhost logan]# ls
test1.txt test2.txt
需要只提取出文件名,而不需要后綴名,如:test1、test2
2.實現代碼
[root@localhost logan]# for file_name in `ls ./ `;do basename $file_name .txt;done
test1
test2
3.basename的用法
3.1 語法
basename(選項)(參數)
3.2 選項
--help:顯示幫助;
--version:顯示版本號。
3.3 命令格式
basename 名稱 [后綴]
basename 選項
3.4 實戰演練
# 顯示文件名,不包含目錄
[root@localhost logan]# basename /home/logan/test1.txt
test1.txt
# 顯示文件名,不包含目錄與后綴
[root@localhost logan]# basename /home/logan/test1.txt .txt
test1
#----或者:-s指定移出的后綴
[root@localhost logan]# basename -s .txt /home/logan/test1.txt
test1
# 將多個參數按照順序輸出
[root@localhost logan]# basename -a /home/logan/test1.txt /home/logan/test2.txt
test1.txt
test2.txt
# 顯示最后一個目錄的名字
[root@localhost logan]# basename /home/logan
logan