-1

I want create per each line of text file create nested directory.

for (( i=0;i<=$(cat filename | wc -l);i++ ))
do
mkdir /PATH/$i
done
  • 1
    What is the question ? – dash-o Oct 30 '19 at 12:02
  • above code. do not work. – Hossein Najafzade Rahaghi Oct 30 '19 at 12:07
  • Possible duplicate of [Looping through the content of a file in Bash](https://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash) because then it is just a matter replacing `echo` by `mkdir` (possibly with option `-p` ). – Gabriel Devillers Oct 30 '19 at 12:12
  • Counting how many lines there are if you don't actually care about the number is just an antipattern. The proper way to read a file is `while read -r line; do ... ; done – tripleee Oct 30 '19 at 12:14
  • Then you are confused. `for` makes sense when you already have the items you want to loop over in memory; but reading the file into memory so you can loop over its items is simply a waste of memory. – tripleee Oct 30 '19 at 12:16
  • 1
    @tripleee I think this shouldn't be marked as a duplicated. OP does not care for the actual lines but only for the line numbers. The "duplicate" is misleading. – Socowi Oct 30 '19 at 12:25
  • 1
    @tripleee Not really a duplicate of that, as OP doesn't seem to care *what* the lines are, just how many there are. – Shawn Oct 30 '19 at 12:25

1 Answers1

1

You are missing the third part of for (( init; condition; increment)). The correct version should be

for (( i=0; i<=$(cat filename | wc -l); i++ )); do
    mkdir /PATH/$i
done

However, this is a very crude way to tackle the problem. A better approach would be the following (courtesy of tripleee)

seq 0 $(wc -l <filename) | xargs -I mkdir /PATH/{}

Note that both commands will create one directory more than the file has lines. For a file with two lines the directories 0, 1, and 2 will be created. If this was an bug in your original script, change the 0 in the scripts to 1.

Socowi
  • 25,550
  • 3
  • 32
  • 54