1

I am struggling with the correct syntax to create a series of directories. Let say 50. These directories will have an index file in them all the same. I have this Bash code but cant seem to work out the code syntax to get it to work. can someone talk me through it please. Thanks

for f in foo/bar{00..50}; do 
   mkdir -p $f && printf "text\n goes\n here" > $f/index
Rinzwind
  • 309,379
Antony
  • 1,453

3 Answers3

2

Easier method:

mkdir -p foo/bar{00..50}
$ ls foo/
bar00  bar02  bar04  bar06  bar08  bar10  bar12  bar14  bar16  bar18  bar20  bar22  bar24  bar26  bar28  bar30  bar32  bar34  bar36  bar38  bar40  bar42  bar44  bar46  bar48  bar50
bar01  bar03  bar05  bar07  bar09  bar11  bar13  bar15  bar17  bar19  bar21  bar23  bar25  bar27  bar29  bar31  bar33  bar35  bar37  bar39  bar41  bar43  bar45  bar47  bar49

So the 1st part can be:

 mkdir -p foo/bar{00..50}

And then all you need to create are the "index" files with the text.


Regarding the 2nd part I would use a reference file

touch file2
vi file2 (and add content)
shop -s globstar && for f in **/*(/); do cp -p file2 "$f/index"; done
Rinzwind
  • 309,379
2

bash for loop syntax is

for ...; do ...; done

So

for f in foo/bar{00..10}; do 
  mkdir -p $f && printf "text\n goes\n here\n" > $f/index
done

Gives :

$ tree foo
foo
├── bar00
│   └── index
├── bar01
│   └── index
├── bar02
│   └── index
├── bar03
│   └── index
├── bar04
│   └── index
├── bar05
│   └── index
├── bar06
│   └── index
├── bar07
│   └── index
├── bar08
│   └── index
├── bar09
│   └── index
└── bar10
    └── index

11 directories, 11 files
Max
  • 644
1

This is how I have done it

create child directories

mkdir -p ./blabla{1..10}

copy file needed (in this case its called indexmultitest.html) to parent directory cp /home/antony/Downloads/Indextests/indexmultitest.html /home/antony/Downloads/blabla/

move into parent directory cd /home/antony/Downloads/blabla/

copy file needed into directories for d in */; do cp indexmultitest.html "$d"; done

This gives you amount of directories each with designated file in it.

Now a question if I wanted to create a series of directories which didnt have predictable names such as blabla01, blabla02, blabla03 etc so someone could guess the names of the directories on my server???

Antony
  • 1,453