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

熱點內容
雲伺服器有無限流量嗎 瀏覽:656
奔騰t55壓縮比 瀏覽:620
python不彈出cmd窗口 瀏覽:921
cad解壓不到e盤 瀏覽:709
phpfopen用法 瀏覽:805
cuda文件和cpp混合編譯 瀏覽:191
鴻蒙自帶主題在哪個文件夾 瀏覽:941
c寫一個簡單的中文編譯器 瀏覽:413
反編譯圖片怎麼弄 瀏覽:100
水空調需要壓縮機嗎 瀏覽:824
h3cdns配置命令 瀏覽:735
曹操傳找不到視頻解壓文件 瀏覽:939
zblogphp插件 瀏覽:649
好興動app怎麼掌上取現 瀏覽:133
python折舊率 瀏覽:539
漣源雲伺服器 瀏覽:992
linux中代表什麼 瀏覽:33
androidphone模塊 瀏覽:35
linux中斷表 瀏覽:177
文本寫的java如何編譯運行 瀏覽:524