導航:首頁 > 文件處理 > 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讀取子文件夾名稱相關的資料

熱點內容
蘋果手機在設置怎麼卸載app 瀏覽:317
我的世界基岩版正版伺服器地址 瀏覽:562
vci命令 瀏覽:834
快剪輯app如何提取音樂 瀏覽:950
gcc編譯器適合單片機開發嗎 瀏覽:886
自做app如何聯網 瀏覽:303
vs的編譯運行 瀏覽:89
app反編譯後怎麼看代碼 瀏覽:766
蘇寧程序員需要注意什麼 瀏覽:557
主力離場指標源碼 瀏覽:345
makefilelinux命令 瀏覽:214
電解壓力表三根線怎麼接 瀏覽:255
夠近物聯app怎麼樣 瀏覽:329
網路中心伺服器是什麼 瀏覽:64
php匹配多次 瀏覽:348
安卓手機怎麼找到不是正常文件的 瀏覽:291
應屆生如何成為程序員 瀏覽:648
麥德龍app怎麼更改配送方式 瀏覽:43
用ae壓縮視頻文件 瀏覽:480
stc單片機跑馬燈 瀏覽:612