home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2049 < prev    next >
Internet Message Format  |  1990-12-28  |  5KB

  1. From: dansmith@well.sf.ca.us (Daniel Smith)
  2. Newsgroups: alt.sources
  3. Subject: ls2dir: simulate a directory hierarchy based on an ls-lR file
  4. Message-ID: <21626@well.sf.ca.us>
  5. Date: 13 Nov 90 03:52:40 GMT
  6.  
  7.  
  8.     Save this part as the README:
  9.  
  10.     I happened to be learning from the new Sed & Awk handbook a few nights
  11. ago, and ended up writing something I've wanted for a long time.  This
  12. is a sh, sed, and mostly awk script that can create a directory hierarchy
  13. based on a file in ls-lR format.  The files created only have one line,
  14. the permissions, ownership, and file size that was present in the ls-lR.
  15. This should handle ls-lRg (group, 9 fields) files just fine.
  16.  
  17.     The directory "lsdir" is created first, and the hierarchy starts from
  18. that point.  This script only provides the commands, you should pipe
  19. to sh to actually make the files and directories.  I did this for sanity's
  20. sake, so that you can get a feel for what is going to happen before committing.
  21. Type "ls2dir~ for more usage.
  22.  
  23.     Some caveats and more hype:
  24.  
  25.     I just seem to find things faster when I can navigate through
  26. files and directories (if you want my cd, push, and pop aliases/scripts,
  27. let me know).  This could use a perl or sh/grabchars based script as a wrapper
  28. that would make up uucp/ftp commands depending on location in the
  29. resulting hierarchy (think of the restore command...).  I also make
  30. a .dirinfo file in each directory in the form of:
  31.  
  32. directory path
  33. Total Files: XX Sum of File Sizes: XXXXXXXX
  34.  
  35.     in the lsdir directory, you'll also find a .totals file such as:
  36.  
  37. Total Dirs: 15 Files: 382 File Sizes:5316909
  38.  
  39.     No doubt this could be done in 10 lines of perl (with comments!)
  40.  
  41.     caveats:
  42.  
  43.     Don't run this over NFS if you can help it!  I haven't (I know
  44. better than to try this with the uunet ls-lR :-), and if you want to remain
  45. on good terms with those you share the network with, you won't.  Seriously,
  46. this can create *thousands* of files.  Find a local disk and go for it.
  47. A June, 1990 uunet ls-lR took 44 *minutes* on a standalone Sony NEWS 1750.
  48. I haven't bothered trying this on my Sparc at work.  Figure that you will
  49. need 9-10 times as much disk space as the original ls-lR.  It's also possible
  50. that you could run out of inodes.  But it's a good tool nonetheless! :-)
  51. If anything, those making NFS servers can use it to benchmark throughput.
  52. I recommend using this on ls-lR files that have absolute pathnames.
  53.  
  54.                 Daniel
  55.  
  56.               Daniel Smith, Island Graphics, Marin County, CA
  57.    dansmith@well.sf.ca.us   daniel@island.com   unicom!daniel@pacbell.com
  58.      phone: (415) 491 1000 (w) disclaimer: Island's coffee was laced :-)
  59.              "Salesmen are the Ferengi of the software world"
  60.  
  61. #! /bin/sh
  62. # This is a shell archive, meaning:
  63. # 1. Remove everything above the #! /bin/sh line.
  64. # 2. Save the resulting text in a file.
  65. # 3. Execute the file with /bin/sh (not csh) to create:
  66. #    ls2dir
  67. # This archive created: Mon Nov 12 19:17:59 1990
  68. # By:    Daniel Smith (island.com!daniel)
  69. export PATH; PATH=/bin:/usr/bin:$PATH
  70. echo shar: "extracting 'ls2dir'" '(1858 characters)'
  71. if test -f 'ls2dir'
  72. then
  73.     echo shar: "will not over-write existing file 'ls2dir'"
  74. else
  75. cat << \SHAR_EOF > 'ls2dir'
  76. #! /bin/sh
  77.  
  78. if [ ! ${1-""} ]; then
  79.     cat << +++
  80. usage: $0 some_ls-lR_file
  81.  
  82.     You'll see a shell script being created.  You can either
  83.     redirect to a file, or make the directory hierarchy directly
  84.     (it will be under the dir "lsdir") by piping.  examples:
  85.  
  86.     $0 some_ls-lR_file > some_script
  87.     $0 some_ls-lR_file | sh -x
  88.  
  89. +++
  90.     exit 1
  91. fi
  92.  
  93. #    this line is silly, I'm just looking for the inverse of basename(1)
  94. basedir=`grep "/.*:" $1 | head -1 | tr '/' '\012' | sed '/:/d' | tr '\012' '/'`
  95.  
  96. #    this part adapted from X11 mkdirhier...
  97. parts=`echo lsdir/$basedir | sed 's,\(.\)/\(.\),\1 \2,g' | sed 's,/$,,'`;
  98. path="";
  99. for p in $parts; do
  100. if [ x"$path" = x ]; then
  101.     dir=$p;
  102. else
  103.     dir=$path/$p;
  104. fi;
  105. if [ ! -d $dir ]; then
  106.     echo mkdir $dir;
  107.     echo chmod a+rx $dir;
  108. fi;
  109. path=$dir;
  110. done;
  111.  
  112.  
  113. cat $1 | awk  '
  114. BEGIN {
  115.     root = "lsdir"
  116.     totfilestr = "Total Files"
  117.     sumfilestr = "Sum of File Sizes"
  118.     seendir = 0
  119.     seenbase = 0
  120.     filecount = 0
  121.     filesum = 0
  122.     totfilecount = 0
  123.     totdircount = 0
  124.     totfilesum = 0
  125. }
  126.  
  127. /^.*otal/    {    # kludge to get basedir set...
  128.     if (seenbase == 0) {
  129.         split (base, bdir, ":")
  130.         seenbase++
  131.     }
  132. }
  133.  
  134.  
  135. NF == 1 && /:$/ {
  136.     if (seendir > 0) {
  137.         printf ("echo %s > %s/%s/.dirinfo\n", bdir[1], root, bdir[1])
  138.  
  139.         printf ("echo %s: %d\t%s:%d >> %s/%s/.dirinfo\n",     \
  140.                  totfilestr, filecount,            \
  141.                 sumfilestr, filesum, root, bdir[1])
  142.     }
  143.     seendir = 1
  144.     filesum = 0
  145.     filecount = 0
  146.     totdircount++
  147.     split ($0,bdir,":")
  148.     printf ("mkdir %s/%s\n", root, bdir[1])
  149.     next
  150. }
  151.  
  152. NF >= 8 && /^-/ {    # handle files
  153.     filesum += $(NF - 4)
  154.     totfilesum += $(NF - 4)
  155.     filecount++
  156.     totfilecount++
  157.     printf ("echo  \"")
  158.     for (i = 1; i < NF; i++)
  159.         printf ("%s ", $i)
  160.     printf ("\" > %s/%s/%s\n", root, bdir[1], $NF)
  161. }
  162.  
  163. END {
  164.     printf ("echo Total Dirs: %d\tFiles: %d\t File Sizes:%d",\
  165.             totdircount, totfilecount, totfilesum)
  166.     printf (" > %s/.totals\n", root)
  167. } ' base=$basedir -
  168. SHAR_EOF
  169. chmod +x 'ls2dir'
  170. fi
  171. exit 0
  172. #    End of shell archive
  173. -- 
  174.               Daniel Smith, Island Graphics, Marin County, CA
  175.    dansmith@well.sf.ca.us   daniel@island.com   unicom!daniel@pacbell.com
  176.      phone: (415) 491 1000 (w) disclaimer: Island's coffee was laced :-)
  177.              "Salesmen are the Ferengi of the software world"
  178.