If you want to keep your for
statement structure and apply the awk script to each specified file, you can do the following:
for file in $(find words -type f -name ".*[12].txt"); do
awk -f script.awk "$file"
done
The find
command is useful for recursively looking through a directory for any pattern of files.
Edit: If your file names contain things like spaces, the above script may not process them properly, so you can do the following instead:
find words -type f -name ".*[12].txt" -print0 | while read -d $'\0' file
do
awk -f script.awk "$file"
done
or using xargs:
find words -type f -name ".*[12].txt" -print0 | xargs -0 awk -f script.awk
This allows you to delimit your file names with null \0
characters, so variations in name spacing or other special characters will not be a problem. (You can find more information here: Filenames with spaces breaking for loop, and find command, or here: Handling filenames with spaces, or here: loop through filenames returned by find).