Create a flat file and directory structure | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Create a flat file and directory structure

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-20-2013
heros heros is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 20 May 2013, 4:35 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Create a flat file and directory structure

Hi All,

is there any work around to generate the file and directory structure like below at left side at Output? and exclude all file except .abc .txt

Current Directory structure


Code:
|-------------files
|---------------Share
|-----------------dir1
|-----------------dir2
|-----------------dir3
|-----------------dir4
|-----------------dir5
|-----------------dir6
|-----------------dir7
|-----------------dir8
|-------------------IMG
|---------------------IMG001
|---------------------IMG002
|-----------------------1234.abc
|-----------------------abcd.txt
|-------------------00001
|---------------------IMG
|-----------------------IMG1
|-----------------------IMG2
|-------------------Sample
|---------------------hello
|-----------------------prod
|-------------------------NAD
|---------------------------NAT
|-------------------------TEXT
|---------------------------TEXTILE1
|---------------------00002
|-----------------------IMAG
|-------------------------IMAG1
|-------------------------IMAG2
|-----------------dir9
|-----------------dir10
|-----------------dir11

print Output file look like this


Code:
1 files    
1.1 Share
1.1.1 dir1
1.1.2 dir2    
1.1.3 dir3
1.1.4 dir4
1.1.5 dir5
1.1.6 dir6
1.1.7 dir7
1.1.8 dir8
1.1.8.1 IMG
1.1.8.1.1 IMG001    
1.1.8.1.2 IMG002    
1.1.8.1.2.1 1234.abc        
1.1.8.1.2.2 abcd.txt    
1.1.8.2 00001            
1.1.8.2.1 IMG                
1.1.8.2.1.1    IMG1            
1.1.8.2.1.2    IMG2            
1.1.8.3    Sample                
1.1.8.3.1 hello                
1.1.8.3.1.1    prod            
1.1.8.3.1.1.1 NAD            
1.1.8.3.1.1.1.1 NAT            
1.1.8.3.1.1.1.2    TEXT        
1.1.8.3.1.1.2.2.1 TEXTILE1    
1.1.8.3.2 00002                
1.1.8.3.2.1 IMAG            
1.1.8.3.2.1.1 IMAG1        
1.1.8.3.2.1.2 IMAG2        
1.1.9 dir9                
1.1.10 dir10            
1.1.11 dir11

Thank you

Last edited by heros; 02-20-2013 at 09:08 PM..
Sponsored Links
    #2  
Old 02-20-2013
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 30 September 2014, 1:08 AM EDT
Location: St. Gallen, Switzerland
Posts: 6,233
Thanks: 121
Thanked 453 Times in 412 Posts
Is this a one-time or re-occuring task? If is just a one time job, it is easier to just create the directories manually than writing a script for parsing this input file.
The -p switch of mkdir might come handy when creating the directories.
Sponsored Links
    #3  
Old 02-20-2013
heros heros is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 20 May 2013, 4:35 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
My apologies, I mean File and Directory Structure is my current structure directory, but i want to generate a full path of file and directory structure and print to a file, not to create a directory

thanks for response

---------- Post updated 02-21-13 at 10:19 AM ---------- Previous update was 02-20-13 at 06:43 PM ----------

is this possible for unix?
    #4  
Old 02-20-2013
Chubler_XL's Avatar
Chubler_XL Chubler_XL is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 30 September 2014, 12:25 AM EDT
Posts: 2,882
Thanks: 107
Thanked 909 Times in 850 Posts
This comes close, only issue is unix sorts alphabetically so order is
dir1
dir11
dir12
dir2


Code:
find ./files \( -type d -o -name "*.abc" -o -name "*.txt" \) -print | awk '
function ppath(p, f, c) {
   s=index(f, "/");
   if (!p) return ppath(substr(f,1,s-1), substr(f,s+1), "");
   if (!s) pf=p"/"f;
   else pf=p"/"substr(f,1,s-1);
   if (!(pf in found)) {
      found[p]++;
      found[pf]=0
   }
   if (!s) return (c?c".":"") found[p] " " f
   return ppath(pf, substr(f, s+1), (c?c".":"") found[p])
}
{ print ppath("", $0, "") }


output:


Code:
1 files
1.1 Share
1.1.1 dir1
1.1.2 dir10
1.1.3 dir11
1.1.4 dir2
1.1.5 dir3
1.1.6 dir4
1.1.7 dir5
1.1.8 dir6
1.1.9 dir7
1.1.10 dir8
1.1.10.1 00001
1.1.10.1.1 IMG
1.1.10.1.1.1 IMG1
1.1.10.1.1.2 IMG2
1.1.10.2 IMG
1.1.10.2.1 IMG001
1.1.10.2.2 IMG002
1.1.10.2.2.1 1234.abc
1.1.10.2.2.2 abcd.txt
1.1.10.3 Sample
1.1.10.3.1 00002
1.1.10.3.1.1 IMAG
1.1.10.3.1.1.1 IMAG1
1.1.10.3.1.1.2 IMAG2
1.1.10.3.2 hello
1.1.10.3.2.1 prod
1.1.10.3.2.1.1 NAD
1.1.10.3.2.1.1.1 NAT
1.1.10.3.2.1.2 TEXT
1.1.10.3.2.1.2.1 TEXTILE1
1.1.11 dir9

---------- Post updated at 02:49 PM ---------- Previous update was at 01:49 PM ----------

Another solution without recursion:


Code:
find ./files \( -type d -o -name "*.abc" -o -name "*.txt" \) -print | awk '{
  c=split($0,v,"/");
  f=v[1];
  for(i=2;i<=c;i++)  {
      if(!(f in P)) P[f]=0;
      if(!(f"/"v[i] in P)) P[f]++;
      P[f"/"v[i]];
      printf "%s",P[f] (i<c?".":"")
      f=f"/"v[i]
  }
  print " " v[i-1];
}'


Last edited by Chubler_XL; 02-20-2013 at 11:50 PM.. Reason: Optimised
Sponsored Links
    #5  
Old 02-21-2013
heros heros is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 20 May 2013, 4:35 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
very appreciate the scripts, sir could you please explain a little bit more whats going on of your script?

thank again.
Sponsored Links
    #6  
Old 02-21-2013
Chubler_XL's Avatar
Chubler_XL Chubler_XL is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 30 September 2014, 12:25 AM EDT
Posts: 2,882
Thanks: 107
Thanked 909 Times in 850 Posts
The find statement produces a list of files and directories that match the criteria (directory or name ends in .txt or .abc) something like this:


Code:
./files
./files/Share
./files/Share/dir1
./files/Share/dir10
...etc

Each directory component is broken out and put into a array (found[] in first script and v[] in second)
Each element in the array counts how many children sit below it

Code:
v[.]=1
v[./files]=1
v[./files/Share]=11
v[./files/Share/dir8]=3
...

Now if a new directory or file ./files/Share/dirX is processed the count in v[./files/Share] will be incremented to 12 because no entry exists for v[./files/Share/dirX] this allows the output of 1.12.1 dirX to be printed.
Sponsored Links
    #7  
Old 02-21-2013
heros heros is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 20 May 2013, 4:35 AM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Thank so much for your help sir.

---------- Post updated 02-22-13 at 09:11 AM ---------- Previous update was 02-21-13 at 02:15 PM ----------

Sir have a question, what about exclude some folder and their succeeding sub directory, I tried to add something like this:


Code:
\( -type d -o -name "*.dat" -o -name "*.txt" -o ! -name "warcraft" \)

is not working, is there any work around?

need to exclude directory and their succeeding subdirectory.

Thanks

Last edited by heros; 02-22-2013 at 12:47 AM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Create Directory structure Aswex Shell Programming and Scripting 5 04-03-2012 07:03 AM
How we can create the master file through shell to show the tree structure of the directory? rohit22hamirpur Shell Programming and Scripting 1 08-10-2011 10:37 AM
find mv and create directory structure fistikuffs UNIX for Dummies Questions & Answers 8 02-09-2011 05:37 AM
create tar archive without preserving directory structure? Special_K UNIX for Dummies Questions & Answers 2 03-18-2009 11:52 AM
How to create a directory structure with getting input from a file. itsjoy2u Shell Programming and Scripting 12 09-29-2008 02:09 PM



All times are GMT -4. The time now is 11:50 AM.