开发者

Writing shell script to scan a list of folders

I have a file fold开发者_开发百科ers.txt

one
two
three
four
...

that has a list of folder names. [one, two, three and four are names of folders].

Each of these folders has a number of files of different types (different extensions). I want a list of all the files in all the folders of one particular extension, say .txt.

How should my shell script look like?


one way

while read -r folders
do
  # add -maxdepth 1 if recursive traversal is not required
  find "$folders" -type f -iname "*.txt" | while read -r FILE
  do
      echo "do something with $FILE"
  done
done <"file"

or

folders=$(<file)
find $folders -type f -iname "*.txt" | while read -r FILE
do
    echo "do something with $FILE"
done

Bash 4.0 (if recursive find is required)

shopt -s globstar
folders=$(<file)
for d in $folders
do
  for file in $d/**/*.txt
  do
    echo "do something with $file"
  done
done


Simply do it on command line:

xargs ls -l < folders.txt | grep '.txt$'


Given the post is simply asking for a list of files, it's quite simple:

tmp=$IFS
IFS=$(echo -en "\n\b") 
for i in `cat folders.txt` ; do
    ls -l "$i/*.txt"
done
IFS=$tmp
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜