Reading files in order in a BASH script

So, I have a bash script that combines a series of subdirectories, searches for files containing a specific line, and then prints the total values ​​of the files and the total values ​​of the directory for line entries. The code is as follows.

  for dir in $(find * -type d); do
  echo "directory: $dir" >> $OUT
  for f in $(find $dir/* -type f); do
    echo -n "$(basename $f) " >> $OUT
    grep -c -h $1 $f >> $OUT
  done
  echo -n "directory total: " >> $OUT
  grep -c -h $1 $dir/*.* | awk '{SUM += $1} END {print SUM}' >> $OUT
  done

When he does this, if I read in 10 files, he lists them in a text file as file1.txt, file10.txt, file2.txt , etc.

Is there a way so that I can print the file file1.txt, file2.txt, file3.txt, etc. and actually specify the 10th file?

I'm new to bash, so I'm just wondering if anyone knew about this. Any help is greatly appreciated. And yes, the files are literally called file1.txt, file2.txt, etc. This is basically just for getting familiar with bash scripts.

+3
4
find -type d | while read -r dir
do
    echo "directory: $dir"
    find "$dir" -maxdepth 1 -type f | sort --version-sort | while read -r f
    do
        echo -n "$(basename "$f") "
        grep -c -h "$1" "$f"
    done
    grep -c -h "$1" "$dir/*" | awk -F: '{SUM += $1} END {print "directory total:", SUM}'
done > "$OUT"
  • while read for, ( )
  • .
  • -maxdepth 1 find,
  • sort --version-sort, , .
  • AWK
  • echo -n "$(basename "$f") " echo -n "${f##*/} "
+2

(, file10 1). file<num>, - sort -k1.5n. - - , , :)

:

, find $dir/* -type f, find $dir -type f. globbing . ?

Btw, , >> $OUT:

for dir in $(find * -type d); do
  echo "directory: $dir"
  for f in $(find $dir/* -type f); do
    echo -n "$(basename $f) "
    grep -c -h $1 $f
  done
  echo -n "directory total: "
  grep -c -h $1 $dir/*.* | awk '{SUM += $1} END {print SUM}'
done >> $OUT
0

:

for f in $(find $dir/* -type f); do

:

for f in $(find $dir/* -type f | sort --numeric-sort); do
0

, , , , . , , ...

for f in $(find $dir -type f | cut -d. -f1 | cut -d'e' -f3 | sort -n); do
    echo -n "  file$(basename $f).txt: "
    grep -c -h $1 $dir/"file$f"'.txt'
  done

, , , .

0

Source: https://habr.com/ru/post/1792450/


All Articles