Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating directories with a for loop in bash

So I would like to create x amount of Directories according to the count of a variable.

#!/bin/bash
countOfCaptureFiles=$(wc -l < allCaptures.txt)
echo $countOfCaptureFiles

start=1
end=countOfCaptureFiles
for((i=$start;i<=$end;i++))
do
mkdir "SnortAlert${i}"
done

In my current testing environment the value of countOfCaptureFiles is 3, so I was hoping to create 3 Directories named SnortAlert1 SnortAlert2 SnortAlert3.

I run the script like so: ./myscript.sh

However when I do this I get errors and no output and I believe it has something to do with assinging end=countOfCaptureFiles but I am not sure how to fix this, any help would be appreciated!

like image 602
firetiger443 Avatar asked Mar 09 '26 17:03

firetiger443


1 Answers

Your code is working. But you can minimize using external programs (like wc, cat) like the following.

#!/bin/bash
i=1
while read line;do
    mkdir "SnortAlert${i}"
    let i=i+1
done <allCaptures.txt
like image 183
thatseeyou Avatar answered Mar 12 '26 09:03

thatseeyou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!