-1

I am using this code to read and print "file name" and "lines numbers", but the code can't read over 1 directory then stop without error msg?!

#!/bin/bash
find . -type d > mydirectory.txt
MyDir=mydirectory.txt
while read -r line; do
    FileDir=$line/*.txt
    for file in ${FileDir}
    do
        awk 'END{q="\047"; print "filename",", nlines"; print q FILENAME q "," q NR q}' "$file"
    done > list.txt
done < mydirectory.txt

first line:

find . -type d > mydirectory.txt

get all directory and sub directory then save it in "mydirectory.txt".

then read every line in "mydirectory.txt" her:

while read -r line; do
        FileDir=$line/*.txt

please what is the wrong, the code search in the first directory only! Regards

  • 5
    You are overwriting list.txt with each iteration of the while loop. It's unclear whether this is the only error (which you fix by moving the >list.txt to after the last done) or whether you mean something else with "the code can't read over 1 directory then stop without error". – Kusalananda Aug 27 '22 at 14:51
  • As the bash tag you added to your question instructs - For shell scripts with errors/syntax errors, please check them with the shellcheck program (or in the web shellcheck server at https://shellcheck.net) before posting here – Ed Morton Aug 27 '22 at 15:42
  • Symply find all files with find . -type f - exec awk bla-bla – gapsf Aug 27 '22 at 16:23
  • @Stéphane Chazelas Could you tell why base utilities that working on files (ls, cp, grep, tar and many other) do not process filenames by itself but relies on shell globbing with its limits (arg length limit, no regexp) and pittfals (explicite qouting, /escaping)? Is it possible working and scripting with globbing disabled totally? Is it problem of legacy and compatibilty only? – gapsf Aug 27 '22 at 19:13
  • 1
    @gapsf You may consider asking that question as a separate question to get more visibility and, therefore, the possibility for input from more people. The comment section is for working out temporary issues relating to questions and answers, and many comments get deleted once they have served their purpose. – Kusalananda Aug 27 '22 at 19:28

1 Answers1

3

It's not entirely clear what your script is intended to do but I THINK it's what this would do:

{
    echo "filename,nlines"
    find . -type f -name '*.txt' -exec \
        awk -v q="'" 'BEGIN{OFS=q","q}
                      ENDFILE{print q FILENAME, FNR q}' {} +
} > list.txt

The above uses GNU awk for ENDFILE.

Ed Morton
  • 31,617