LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 08-10-2009, 10:28 AM   #1
Tekken
Member
 
Registered: Jun 2009
Posts: 48

Rep: Reputation: 15
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...
 
Old 08-10-2009, 11:10 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Some info is missing:
the required structure of your directory tree
where does the content to be pushed come from
 
Old 08-11-2009, 01:28 AM   #3
Tekken
Member
 
Registered: Jun 2009
Posts: 48

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Wim Sturkenboom View Post
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

Last edited by Tekken; 08-13-2009 at 09:35 AM.
 
Old 08-11-2009, 03:01 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by Tekken View Post
attached JPEG file
That's sort of difficult. Do you know the pattern of how the folders are created?

Last edited by konsolebox; 08-11-2009 at 03:11 AM.
 
Old 08-11-2009, 03:14 AM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
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.
 
Old 08-11-2009, 03:48 AM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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.

Last edited by Wim Sturkenboom; 08-11-2009 at 03:49 AM. Reason: Added PS
 
Old 08-11-2009, 12:10 PM   #7
Tekken
Member
 
Registered: Jun 2009
Posts: 48

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Wim Sturkenboom View Post
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.
 
Old 08-12-2009, 12:20 AM   #8
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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.
 
Old 08-12-2009, 02:07 AM   #9
Tekken
Member
 
Registered: Jun 2009
Posts: 48

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Wim Sturkenboom View Post
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.
 
Old 08-13-2009, 12:35 AM   #10
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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
}
 
Old 08-13-2009, 09:38 AM   #11
Tekken
Member
 
Registered: Jun 2009
Posts: 48

Original Poster
Rep: Reputation: 15
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 ...???
 
Old 08-13-2009, 11:51 AM   #12
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
That does not create the same directory structure as you gave in the jpeg.
 
Old 08-13-2009, 11:55 AM   #13
Tekken
Member
 
Registered: Jun 2009
Posts: 48

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Tekken View Post
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.

Last edited by Tekken; 08-13-2009 at 02:32 PM.
 
Old 08-15-2009, 02:07 AM   #14
Tekken
Member
 
Registered: Jun 2009
Posts: 48

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Tekken View Post
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.
 
Old 08-15-2009, 02:08 AM   #15
Tekken
Member
 
Registered: Jun 2009
Posts: 48

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by konsolebox View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with script to organise files into folders as part of DVD backup script jasybee2000 Linux - Newbie 5 06-15-2009 08:29 PM
Script to create folders and subfolders leupi Linux - Newbie 12 07-18-2008 11:39 AM
Cannot create folders with bash script called from php keyF Linux - Software 4 06-25-2006 11:58 AM
Samba can create new files and folders but access denied in any new folders k.king Linux - Networking 2 01-15-2006 07:14 AM
how do I create links to folders? liontamer13 Linux - Newbie 2 12-18-2004 04:05 AM


All times are GMT -5. The time now is 10:12 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration