LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Script to create folders recurssively (https://www.linuxquestions.org/questions/linux-server-73/script-to-create-folders-recurssively-746450/)

Tekken 08-10-2009 09:28 AM

Script to create folders recurssively
 
Hi,

I want a perl script which can create folders structure recurssively and push some content in to the folders, i want to create some 10000 to 20000 folders using the script at one shot. Can any one help me in this regard.
Thanks...

Wim Sturkenboom 08-10-2009 10:10 AM

Some info is missing:
the required structure of your directory tree
where does the content to be pushed come from

Tekken 08-11-2009 12:28 AM

Quote:

Originally Posted by Wim Sturkenboom (Post 3637952)
Some info is missing:
the required structure of your directory tree
where does the content to be pushed come from

Hi,

Please find the attached JPEG file for the folders structure,i need to write a script which can create the structure in the attachement. Content should be pushed recursively to all the folders from some other location in the same system where we will execute the script.

Let me know if you need any more info.
Thanks

konsolebox 08-11-2009 02:01 AM

Quote:

Originally Posted by Tekken (Post 3638750)
attached JPEG file

That's sort of difficult. Do you know the pattern of how the folders are created?

chrism01 08-11-2009 02:14 AM

Hmmmm, looks bit like homework from the picture.
However, as konsolebox hints, there doesn't seem to be a pattern there...
If that's the case, I'd prob just create a file with csv recs for each complete set of paths, then use Perl to read the csv file and create dirs by walking down each csv rec.

Wim Sturkenboom 08-11-2009 02:48 AM

Is there a relation between the shown directory structure and content (structure). I mean, how do you know what to push where? You must have some idea and you should tell us (e.g. what the content directory looks like).

Please provide complete information.

As it stands now, you can follow chrism01's solution but you will have to enter 10000 or 20000 records in a csv file. It more or less defeats the onject of a script.

PS If you tried anything yourself, please provide the details.

Tekken 08-11-2009 11:10 AM

Quote:

Originally Posted by Wim Sturkenboom (Post 3638851)
Is there a relation between the shown directory structure and content (structure). I mean, how do you know what to push where? You must have some idea and you should tell us (e.g. what the content directory looks like).

Please provide complete information.

As it stands now, you can follow chrism01's solution but you will have to enter 10000 or 20000 records in a csv file. It more or less defeats the onject of a script.

PS If you tried anything yourself, please provide the details.

Hi Wim,

My requirement is some thing like this,

I want a script which can help me in creating 10000 folders (structure shown in JPEG). I want to execute the script in below way,

script <No.of folders> <file to insert> <file No>

here $1 is the number of folders i want ot create, file to insert will be the file i would like to insert and it has to go in to each folder and file no is the number which i want to assign for the file.

I am looking for a perl script for this as i want to run this in both unix and windows.Is it possible to create such script??

Please help me here.

Wim Sturkenboom 08-11-2009 11:20 PM

Hi Tekken,

it's still not quite clear to me how you visualize that it works.

You pass no of folders to a script. Will that create one branch with subbranches like below where the number is 4?
Code:

root
  |
  +--dir0001
      |
      +--dir0002
            |
            +--dir0003
                |
                +--dir0004

And if you run the script again with a number of 6 it will create like below?
Code:

root
  |
  +--dir0001
  |    |
  |    +--dir0002
  |        |
  |        +--dir0003
  |              |
  |              +--dir0004
  +--dir0005
      |
      +--dir0006
            |
            +--dir0007
                |
                +--dir0008
                      |
                      +--dir0009
                          |
                          +--dir0010

The only difficulty I see is to determine what the last used directory is so one can continue from there when you run the next time.

Next you want to copy one specific file (the second argument) into each created directory. That is certainly possible.

Last you have fileno (the last argument) and I'm not quite sure what must happen with that? Does the copied file have to be renamed to that number (e.g. file3344)?

1)
Is that indeed what you need?
2)
What makes me curious is why you want to do something like that (what is the use). Can you explain that?
3)
I'm not a perl coder, so can't help with perl code.

Tekken 08-12-2009 01:07 AM

Quote:

Originally Posted by Wim Sturkenboom (Post 3639996)
Hi Tekken,

it's still not quite clear to me how you visualize that it works.

You pass no of folders to a script. Will that create one branch with subbranches like below where the number is 4?
Code:

root
  |
  +--dir0001
      |
      +--dir0002
            |
            +--dir0003
                |
                +--dir0004

And if you run the script again with a number of 6 it will create like below?
Code:

root
  |
  +--dir0001
  |    |
  |    +--dir0002
  |        |
  |        +--dir0003
  |              |
  |              +--dir0004
  +--dir0005
      |
      +--dir0006
            |
            +--dir0007
                |
                +--dir0008
                      |
                      +--dir0009
                          |
                          +--dir0010

The only difficulty I see is to determine what the last used directory is so one can continue from there when you run the next time.

Next you want to copy one specific file (the second argument) into each created directory. That is certainly possible.

Last you have fileno (the last argument) and I'm not quite sure what must happen with that? Does the copied file have to be renamed to that number (e.g. file3344)?

1)
Is that indeed what you need?
2)
What makes me curious is why you want to do something like that (what is the use). Can you explain that?
3)
I'm not a perl coder, so can't help with perl code.

Hi Wim,

The last argument file number is used to number the file across all the folders in ot which it is pushed, if enter 1 as a number it should cop the file with that number in all the folders.

Could you please pass me the script if you have any for such purpose. I will try to modify it as per my requirement and see if it works or not.

Wim Sturkenboom 08-12-2009 11:35 PM

Quote:

The last argument file number is used to number the file across all the folders in ot which it is pushed, if enter 1 as a number it should cop the file with that number in all the folders.
What is the difference between file_to_insert and file_no ?


Anyway
Maybe the below TCL script (as said, I'm not a perl coder) can get you on the way. Please note that it does not provide any error checking, validation or whatever. It has one extra command line argument indicating the number of the first directory to create.
Code:

#! /usr/bin/tcl

# extract the arguments from the command line
set no_of_dirs [lindex $argv 0]
set file [lindex $argv 1]
set file_no [lindex $argv 2]
set start_dir [lindex $argv 3]

# create directory structure
set path ""
for {set cnt $start_dir} {$cnt < [expr $no_of_dirs + $start_dir]} {incr cnt} {
    append path "dir$cnt/"
}
file mkdir $path

# copy file to each folder; append file_no
unset path
for {set cnt $start_dir} {$cnt < [expr $no_of_dirs + $start_dir]} {incr cnt} {
    append path "dir$cnt/"
    set copy [format %s%s.%s $path $file $file_no]
    file copy $file $copy
}


Tekken 08-13-2009 08:38 AM

Hi,

I've wrote a script which will create 10000 folders, below is the code

for fld in {1..10000};
do
mkdir Folder$fld
done

this creates the folders, but now i want to copy a file from some location to all these 10000 folders created. can any one help me with the code ...???

Wim Sturkenboom 08-13-2009 10:51 AM

That does not create the same directory structure as you gave in the jpeg.

Tekken 08-13-2009 10:55 AM

Quote:

Originally Posted by Tekken (Post 3641856)
Hi,

I've wrote a script which will create 10000 folders, below is the code

for fld in {1..10000};
do
mkdir Folder$fld
done

this creates the folders, but now i want to copy a file from some location to all these 10000 folders created. can any one help me with the code ...???

Hi,

I've modified my script little more and wrote it as below,

#!/bin/bash
if [[ $1 == "Folder" ]];
then
for i in {1..10};
do
mkdir $1$i
done
else
if [[ ! $# == 1 ]];
then
echo "Pass the commandline argument"
fi
fi

Now i want to include some code in the above line which would help me in copying a file to all the directories created which i will specify on the command line in place of shell 2nd argument.Also how do we write a code which will take input from command line.

ex: script <foldername> <file>

Please help me here.
Thanks,
Tekken.

Tekken 08-15-2009 01:07 AM

Quote:

Originally Posted by Tekken (Post 3638750)
Hi,

Please find the attached JPEG file for the folders structure,i need to write a script which can create the structure in the attachement. Content should be pushed recursively to all the folders from some other location in the same system where we will execute the script.

Let me know if you need any more info.
Thanks

Hi,

I've modified my script little more and wrote it as below,

#!/bin/bash
if [[ $1 == "Folder" ]];
then
for i in {1..10};
do
mkdir $1$i
done
else
if [[ ! $# == 1 ]];
then
echo "Pass the commandline argument"
fi
fi

Now i want to include some code in the above line which would help me in copying a file to all the directories created which i will specify on the command line in place of shell 2nd argument.Also how do we write a code which will take input from command line.

ex: script <foldername> <file>

Please help me here.
Thanks,
Tekken.

Tekken 08-15-2009 01:08 AM

Quote:

Originally Posted by konsolebox (Post 3638814)
That's sort of difficult. Do you know the pattern of how the folders are created?

Hi,

I've modified my script little more and wrote it as below,

#!/bin/bash
if [[ $1 == "Folder" ]];
then
for i in {1..10};
do
mkdir $1$i
done
else
if [[ ! $# == 1 ]];
then
echo "Pass the commandline argument"
fi
fi

Now i want to include some code in the above line which would help me in copying a file to all the directories created which i will specify on the command line in place of shell 2nd argument.Also how do we write a code which will take input from command line.

ex: script <foldername> <file>

Please help me here.
Thanks,
Tekken.


All times are GMT -5. The time now is 08:25 PM.