# Creating directories inside a directory, from variable values, to redirect output from sed

I am trying to take a file, modify this file by using a value from a for loop (using sed) and redirecting it to a directory that has been created during the same for loop.

Original file > Make directory > process file (make changes) > redirect output to directory

My problem is at making the directory with the values of my variables.

read NaMeoFFiLe
NaMeDiR="${startepsilonvalue}plus10steps" mkdir "${NaMeDiR}"
for ((i=$stteps; i<=$lsteps; i+=1)); do
k=$(bc <<<"""scale=1;$i /10")
echo $k mkdir "${NaMeDiR}/${k}" ls "${NaMeDiR}"
sed -e '0,/epsilon = ./{//c\epsilon = '"$startepsilonvalue" -e '}' \ "${NaMeoFFiLe}" > "${k}_${NaMeiFFiLe}"
cat "${NaMeDiR}/${k}/${k}_${NaMeoFFiLe}" &
done


The thing is that I can create the first directory, outside the for loop, but then inside the for loop it only creates the first and last directory and it won't do the changes of the sed command.

The error that I get is that the file does not exist in the directory that I look for which is obvious because the directory hasn't been created.

Is there anything that I am missing?

• Your variable naming convention hurts my eyes - but I notice you have NaMeiFFiLe in place of NaMeoFFiLe in at least one instance. Also you mkdir "${NaMeDiR}/${k}" you actually re-direct the sed output to "${k}_${NaMeiFFiLe}" in the current directory – steeldriver Jan 15 at 17:27
• In fact, we avoid naming variables like that precisely so as not to have this sort of error. NameDir or namedir or Namedir are all clear and easy to read. NaMeDiR is like you are trying to make this as hard for yourself as possible! – terdon Jan 15 at 17:42
• Sorry, I am a bit new to all this. I read somewhere that using capital letters at naming variables was useful to make sure you would not overwrite them. But I'll take your advice! Thank you – Nankin Jan 15 at 17:52

First off, as written, $k can sometimes start with a dot (e.g. .1, .2, etc.), which in Unix defines the directory to be a hidden directory. The first thing I would check is whether those directories are created as hidden: ls -A  Second, you are creating the ${k}_${NaMeiFFiLe} on the parent directory, NOT in the new directories that you are creating. Again, since $k starts with a dot in many cases, it may be hidden on the parent directory. Given the flow of your code, I think you meant to save the file within the new sub-directories. If true, that would look something like this (I replaced the regex with ... for simplification):
sed -e '...' "${NaMeoFFiLe}" > "${NaMeDiR}/${k}/${k}_${NaMeoFFiLe}"  And just as a side note, the mkdir has an option flag -p that can create full directory hierarchies including sub-directories, which means that you wouldn't need an outer loop mkdir: mkdir -p "${NaMeDiR}/\${k}"