Linux 檢視目錄下的檔名(不帶字尾)

2022-01-30 00:57:39 字數 993 閱讀 2289

目錄現存在以下檔案列表:

[root@localhost logan]# ls

test1.txt test2.txt

需要只提取出檔名,而不需要字尾名,如:test1、test2

[root@localhost logan]# for file_name in `ls ./ `;do basename $file_name .txt;done

test1

test2

basename(選項)(引數)
--help:顯示幫助;

--version:顯示版本號。

basename 名稱 [字尾]

basename 選項

# 顯示檔名,不包含目錄

[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

列出指定目錄下所有檔名

要求 絕對路徑名做輸入引數,列印輸出該路徑下所有檔名 知識點 開啟目錄函式 dir opendir const char path 讀取目錄函式 struct dirent readdir dir dir 返回下乙個目錄項的指標。include include dirent結構體定義如下 struc...

python之獲取目錄下的檔名

背景 為了統計乙個目錄下所有檔名,並且寫到檔案中 工具 python的模組os.walk 功能 遍歷目錄下的所有子檔案及其子資料夾,如果有子資料夾的話,會繼續以子資料夾為目錄遞迴遍歷 返回 如果只有檔案的話,只返回乙個元組 當前目錄字串,資料夾名列表,檔名列表 如果還有資料夾的話,返回多個元組 co...

批處理獲取目錄下所有檔名

由於要處理一些檔案,找了個這樣的批處理 輸出目錄及子目錄下所有的jpg的檔名,不含副檔名 其中,delims 的含義是取消預設的分隔符,所以會把行上的內容照搬下來,而 tokens 表示獲取行上的所有內容,但是會忽略行首的所有空格。另外的引數介紹一下 0 1 2 3 4 5 6 7 8 9 命令列傳...