shell怎么循環(huán)讀取目錄下的文件名

小億
353
2023-11-28 12:25:16
欄目: 編程語言

在shell腳本中,可以使用for循環(huán)結(jié)構(gòu)來讀取目錄下的文件名。以下是一個(gè)示例代碼:

#!/bin/bash

# 指定目錄路徑
directory="/path/to/directory"

# 循環(huán)遍歷目錄中的文件名
for file in "$directory"/*; do
    # 輸出文件名
    echo "$file"
done

在上面的示例中,$directory/*表示目錄下的所有文件,for file in ...會(huì)將每一個(gè)文件名賦值給變量file,然后可以在循環(huán)體中對(duì)文件名進(jìn)行處理。

0