1

I'm new to this site (and to programming, more or less), but I'm hoping you can help.

I have numerous directories named 3K, 4K, 5K, etc. Within each directory I have 12 subdirectories named v1 to v12, each containing a file called OUTCAR. I am trying to write a bash command that will allow me to enter each of the subdirectories and gather data from OUTCAR.

The function works with no issues when I enter each subdirectory individually.

I'm using

for file in v{1..12} ; do grep "key_string" OUTCAR | awk '{print(relevant_stuff)}' > output.dat ; done

From the *K fine that contains the v{1..12} subdirectories.

However, I'm getting an error telling me that OUTCAR doesn't exist for each v{1..12}. I know it does, so I'm guessing that I haven't properly directed the command to cd into each subdirectory first. Any tips?

Thanks!

2
  • Should all the output go into one output.dat file or do you want an output.dat file beside each OUTCAR file? Overall, it's probably easier to use a find command like: "find -name OUTCAR" and either do the grepping/awking via xargs or iterate the result with a "while read" loop. Commented Feb 4, 2015 at 16:00
  • I am aiming to produce a separate output.dat file for each OUTCAR, printed into each v1 to v12 subdirectory. Commented Feb 4, 2015 at 16:02

1 Answer 1

1

You would be better of using this find command from top level directory where these sub directories exist:

find . -type d -name 'v[1-9][[1-9]' \
    -exec awk '/key_string/ {print FILENAME ":" $0}' {}/* >> output.dat \;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for your response! Could you clarify -type and -name for me?
You can get details from man find. -type d only finds directories and -name matches a pattern agains directory names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.