I have a directory named ../words that contains a number of subdirectories(words/blabla,words/blabla), in turn each subdirectory contains two types of files (.*1.txt) and (.*2.txt) . What I need, it is to run an AWK script against each one of these files.

Could it be something like?

for d in words/*
    for f in .*[0-9].txt
    awk -f script.awk ${f}

If you're using a modern shell like bash or zsh, you can use something like:

for i in words/*/*{1,2}.txt; do awk -f script.awk "$i"; done

If, for whatever reason, you need compatibility with /bin/sh, you can use this slightly more verbose and less readable solution

for i in words/*/*1.txt words/*/*2.txt; do awk -f script.awk "$i"; done 
  • It works perfectly!! as I expected. – Firefly Feb 27 '16 at 13:00

This should work:

for f in words/*/.*[12].txt
    awk -f script.awk "$f"
  • I get this error, probably because the version of bash on cygwin awk: iterate.awk: 2: fatal: can not open the file /*/.* words [12] .txt 'for reading (No such file or directory) My fault, I haven't informed of those details. – Firefly Feb 27 '16 at 13:04

try this for d in words/*; do for f in ./words/$d/*{0..9}.txt; do awk -f script.awk $f; done

  • There are several things wrong here: for d in words/* selects everything under words not only directories - you would need for d in words/*/ to glob for dirs only then for f in ./words/$d/*{0..9}.txt is actually equivalent to for f in ./words/words/*/*{0..9}.txt so most likely not what OP wants and finally, you need the closing done. Also, don't forget to quote your vars. – don_crissti Feb 26 '16 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.