How to perform a for-each loop over all the files under a specified path?

前端 未结 4 500
粉色の甜心
粉色の甜心 2020-12-02 07:00

The following command attempts to enumerate all *.txt files in the current directory and process them one by one:

for line in \"find . -iname \'         


        
4条回答
  •  执笔经年
    2020-12-02 07:26

    Here is a better way to loop over files as it handles spaces and newlines in file names:

    #!/bin/bash
    
    find . -type f -iname "*.txt" -print0 | while IFS= read -r -d $'\0' line; do
        echo "$line"
        ls -l "$line"    
    done
    

提交回复
热议问题