導航:首頁 > 文件處理 > bash讀取子文件夾名稱

bash讀取子文件夾名稱

發布時間:2023-02-28 11:30:23

㈠ 如何用shell獲取linux目錄下的文件名

獲取所有常規文件的文件名並列印出來的腳本listfile.sh如下

#!/bin/bash

dir="/*"
dir=$1$dir
for f in $dir
do
if [ -f $f ]
then
echo $f
fi
done

使用方法:
$ listfile.sh PATH

原理:
PATH參數是路徑,將路徑後加上「/*」,代表該目錄下的所有文件和目錄名,利用for循環比較每個文件是否是常規文件( -f比較運算符),若if表達式為真則列印

舉例:
ls -l
total 36
-rwxrwxr-x 1 lipeng lipeng 48 Nov 29 20:08 aaa.sh
drwxrwxr-x 2 lipeng lipeng 4096 May 4 2015 byteorder
drwxrwxr-x 8 lipeng lipeng 4096 May 3 2015 hello
-rwxrwxr-x 1 lipeng lipeng 122 Nov 29 20:16 listfile.sh
-rw-rw-r-- 1 lipeng lipeng 177 Aug 1 03:10 main.cpp
drwxrwxr-x 2 lipeng lipeng 4096 Sep 13 16:42 matrix
drwxrwxr-x 5 lipeng lipeng 4096 Apr 28 2015 modbus
drwxrwxr-x 2 lipeng lipeng 4096 Sep 13 10:10 shtest
drwxrwxr-x 2 lipeng lipeng 4096 Sep 16 18:21 test

$ ./listfile.sh .
./aaa.sh
./listfile.sh
./main.cpp

閱讀全文

與bash讀取子文件夾名稱相關的資料

熱點內容
蘇寧程序員需要注意什麼 瀏覽:557
主力離場指標源碼 瀏覽:345
makefilelinux命令 瀏覽:214
電解壓力表三根線怎麼接 瀏覽:255
夠近物聯app怎麼樣 瀏覽:329
網路中心伺服器是什麼 瀏覽:64
php匹配多次 瀏覽:347
安卓手機怎麼找到不是正常文件的 瀏覽:291
應屆生如何成為程序員 瀏覽:648
麥德龍app怎麼更改配送方式 瀏覽:43
用ae壓縮視頻文件 瀏覽:480
stc單片機跑馬燈 瀏覽:612
伺服器四口卡怎麼用 瀏覽:609
匹配python注釋 瀏覽:408
什麼是程序員才懂得梗 瀏覽:396
pdftodwgconverter 瀏覽:960
linux找不到sqlplus 瀏覽:399
什麼app五險在哪裡查詢 瀏覽:925
單片機農歷 瀏覽:438
g70如何編程 瀏覽:648