Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2004
    Posts
    5

    Exclamation Unanswered: How to compress a folder using shell script

    ;Hi,

    hello friends,i would like to write a shell script which it can zip a folder in a certain directory.The problem is inside that directory,it contains some file and folder.I just want to zip the folder but not other file. The script should be able to check the directory,as long as it found a folder it will zip it.So is that any way to do that using shell script?Hope somebody can help me,thanks in advance
    Last edited by freelancers; 07-28-04 at 12:53.

  2. #2
    Join Date
    Jun 2004
    Posts
    29

    Use the -f flag

    example code
    Code:
    for file in ls
    do
      if [ -f $file ]
      then 
        gzip $file
      fi
    done
    There are other variants
    -r readable
    -w writable
    -x executable
    and many others - I get mine from
    http://www16.boulder.ibm.com/doc_lin....htm#a2659135b
    because I work in ksh under AIX

  3. #3
    Join Date
    Jun 2004
    Posts
    29

    Ooops

    Spot the deliberate mistake
    it should be
    Code:
    for file in `ls`
    ....

  4. #4
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Or slightly better, use a file expression to derive your file list...

    for file in *
    do ...



    `ls` would be tokenised after the filenames are substituted, so filenames containing spaces would not be assigned to $file as expected.

    e.g.

    $ touch a "b c"
    $ for fname in `ls`; do echo $fname; done
    > a
    > b
    > c

    $ for fname in *; do echo $fname; done
    > a
    > a b


    ...and the -d flag is probably what you want to test if it is a directory

    See: man test
    Last edited by Damian Ibbotson; 07-28-04 at 13:39.

  5. #5
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Cool

    Why not just do:
    Code:
    find myDir/* -prune -type f -exec gzip {} \;
    ???
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •