0

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
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
  • Ok so I now have 50 empty directories called test.. Not very clear about how to do the second part? I would like to have an index file in each which would be duplicated/same Thanks – Antony Jan 11 '18 at 18:50
  • the last part does that; it copies a pre-made file over to all the directories it can find. – Rinzwind Jan 11 '18 at 20:45
  • so do I indicate the file and directory to be copied from (and add content) Like home/dir/index.html to **/*(/) ? – Antony Jan 11 '18 at 21:13
  • Sorry Rinzwind I am concerned that I could screw something. Lets say if I had a file called indextest.html and it was in say Downloads/test. Then if mkdir -p /home/antony/Downloads/win{00..50} $ ls foo/ is the directory how would I write ? cheers – Antony Jan 12 '18 at 11:21
  • Then you change "file2" for "indextest.html" and that file is copied to every sub directory from the current location. – Rinzwind Jan 12 '18 at 11:24
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
0

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???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.