【Linux】查看目錄下的文件名(不帶后綴)


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM