Search code examples
for-loopmkdir

How do I create a file in a for loop that includes the variable and added text?


I have the following code, where I would like to create directories that are named by the variable number and then _DOS is added to the filename. (Basically I want the code to create directories named: 1_DOS 2_DOS 3_DOS, and then save the files POSCAR and log inside the created directory.) However, when I run the job, it is not creating the desired directories. I think my error is in mkdir $i_DOS. I think the code is looking for i = i_DOS and not finding it. When I run this, the error returns as mkdir: missing operand Do you have any suggestions?

 for i in 1 2 3
 do
 mkdir $i_DOS
 cp POSCAR $i_DOS
 cp log $i_DOS
 done
 exit

Thank you in advance.


Solution

  • use {} braces around the variable i to resolve ambiguity.

     for i in 1 2 3
     do
     mkdir ${i}_DOS
     cp POSCAR ${i}_DOS
     cp log ${i}_DOS
     done
     exit