I have a script in bash that transfer csv files to a python program from 2 differents directories like so :


for f in DIR1
    /direc/to/python3.7 /dir/scriptspython/.py $DIR1/$f

for n in DIR2
    /direc/to/python3.7 /dir/scriptspython/.py $DIR2/$f

The problem is that i'd like the files from the directories to load one after the other like so : $DIR1/$f(1) then $DIR2/$n(1)...$DIR1/$f(x) then DIR2/$n(x)

My problem looks similar to this thread but with directories instead of files

  • Please clarify: Are DIR1 and DIR2 already populated with CSV files you wish to send to your Python script as parameters, or is the Python script creating CSV files you wish to place into DIR1 and DIR2? – DopeGhoti Feb 12 at 15:49
  • DIR1 and DIR2 are already populated with CSV files, I specify to the python script the path – Fresh_and_innocent_sysadmin Feb 12 at 15:56

So, I assume you have files like this

aa/apple aa/orange aa/perry  bb/apple bb/orange bb/perry

We could use aa/* and bb/* to list the files in both directories, then save them in two arrays, and loop over those:

for (( i=0; i < ${#files1[@]}; i++)); do
    echo "${files1[i]}"
    echo "${files2[i]}"

That would output them in the order aa/apple, bb/apple, aa/orange, etc. The sort order is the default lexicographic sort. The above takes the file count from the first list, so it basically assumes there is an equal number of files in both directories.

(I'm not sure how much sense any of this makes if the lists of filenames aren't equal in the two directories.)

  • I haven't got the same number of files in both directory so I think that'll save the number of loops stored in i and then finish uploading the rest starting from i. – Fresh_and_innocent_sysadmin Feb 13 at 7:57

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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