I have two main directories say, A and B. Each of them contains 100 sub directories (a1,a2,.. and b1,b2,...) with random name. Those sub directories contain a file of XY type. The X value is same for a particular sub directory say a1 and b1 but the Y value is different. I now can create a XY type file where Y will be the difference between the sub directories Y column only, say Y = Y1(a1) - Y2(b1) with a simple awk command or so. But I don't know how to do this with a bash loop for all the sub directories. What I want a desired script which will create directories 1, 2,.. where each of the directory will contain a XY type file and the XY type files contain X Y(a1-b1) in directory 1, X Y(a2-b2) in directory 2,.., X Y(a100-b100) in direcotry 100. I am quite new in bash so apologize for any trivial question.

Schematic example can be like this: I have two main directories A and B. A has sub directories 0.3427/ 0.3514/ 0.3543/ 0.3792/... and B has sub directories 0.4011/ 0.4014/ 0.4031/ 0.4357/....

0.3427/ sub directory of A contains the data file:

0.00 -8.79583
50.00 -8.79621
100.00 -8.79961
150.00 -8.80721
200.00 -8.81845
250.00 -8.83271
300.00 -8.8495

and 0.4011 sub directory contains the data file:

0.00 -8.78888
50.00 -8.78954
100.00 -8.79458
150.00 -8.80459
200.00 -8.81852
250.00 -8.83561
300.00 -8.85529

Now I want in directory 1 there should be a file say data.dat with the following data:

0.00 0.00695
50.00 0.00667
100.00 0.00503
150.00 0.00262
200.00 -7e-05
250.00 -0.0029
300.00 -0.00579

This desired data.dat file is just the difference of column Y of first sub directories of A and B which can be produce by an awk/paste command. Now I want the same for all the other sub directories present in the A and B. where in directory 2 the data.dat file should contain the difference in Y column of 0.3514/ of A and 0.4014/ of B and so on.

  • For a question like this it's sometimes easier to provide examples; what you have before and what you have afterwards. All the words you've written are unclear on the actual task. Commented Aug 23, 2016 at 14:30
  • @Stephen: Al right, I will edit the question with examples shortly.
    – baban
    Commented Aug 23, 2016 at 14:32
  • @Stephen: I tried to make the question a bit clear with example. Does it make sense now? sorry, for not writing properly.
    – baban
    Commented Aug 23, 2016 at 14:48

1 Answer 1


Try this one.


# Run it in the folder containing dir A and dir B.

A=A #first dir, replace with the propper name
B=B #second dir, replace with the propper name
in_file=f #replace with propper name

B_subs=( $(ls B | sort -n) )

cd "$A"
for d in $( ls | sort -n) ; do
    mkdir ../"$i"
    # the one below is not correct, but gives your results
    # join "$a" "$b" | awk '{print $1, $3-$2}' > ../"$i"/"$out_file"
    # the one below is correct
    join "$a" "$b" | awk '{print $1, $2-$3}' > ../"$i"/"$out_file"
  • Thank you so much, this is perfect answer, code is working perfectly and giving me desire outputs. Sorry, I can't upvote your answer which made my life easy.
    – baban
    Commented Aug 23, 2016 at 18:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .