linux 中怎么把ls 显示的内容进行foreach 遍历

# csh
set dir = "/path/to/directory"
foreach file (`ls $dir/*`)
    echo "文件路径: $file"
end

#bash
for file in *; do
    echo "文件: $file"
done

for txt_file in *.txt; do
    echo "文本文件: $txt_file"
    # 可在此执行其他操作,如重命名:mv "$txt_file" "${txt_file%.txt}.bak"
done

ls | while read -r file; do
    echo "处理文件: $file"
done

 

posted @ 2025-08-29 18:20  HSLKa  阅读(8)  评论(0)    收藏  举报