Zip a folder including its sub-folders. | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Zip a folder including its sub-folders.

UNIX for Dummies Questions & Answers


Tags
sub folders, subfolders, unix, winzip, zip

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-10-2008
gdog gdog is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 10 July 2008, 12:17 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Zip a folder including its sub-folders.

Hi,

I have a folder that contains a few sub-folders. I would like to zip that folder and KEEP the subfolders. What it does at the moment is taking all the files within the subfolders and zipping them into one big file (zip -r ...). Does anyone know the UNIX command to keep the subfolders in the zipped files?

Thanks.
Sponsored Links
    #2  
Old 07-10-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Last Activity: 16 September 2014, 8:55 AM EDT
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 1,673
Thanks: 39
Thanked 193 Times in 176 Posts
Hi.

I rarely use zip / unzip, but it worked for me as expected. This script creates a structure, zips it, renames the old directory, unzips from the archive, and compares the 2 resulting directory structures:

Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate zip collection and expansion of directories.

#  ____
# /
# |   Infrastructure BEGIN

echo
set -o nounset

debug=":"
debug="echo"

## The shebang using "env" line is designed for portability. For
#  higher security, use:
#
#  #!/bin/sh -

TREE=tree
TREE=dtree
TREE=tree-sage

## Use local command version for the commands in this demonstration.

set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) zip diff
set -o nounset

echo

# Remove and create directory structure.

rm -rf d1 d2 d1.zip old-d1

mkdir -p d1/d2/d3
echo hi >d1/hi-in-d1
echo hi >d1/d2/hi-in-d1-d2
echo hi >d1/d2/d3/hi-in-d1-d2-d3

echo " Directory to be zipped:"
$TREE ./d1

# |   Infrastructure END
# \
#  ---

echo
echo " Results from processing:"
zip -r d1 d1
ls -go d1.zip

echo
echo " Unzipping:"
mv d1 old-d1
unzip d1

echo
echo " Newly unzipped d1:"
$TREE d1

echo
echo " Old d1:"
$TREE old-d1

echo
echo " Comparison of d1 and old-d1:"
CMP="diff -r"
if $CMP d1 old-d1
then
  echo " Directories d1 and old-d1 have the same content."
else
  echo " Directories d1 and old-d1 fail to compare."
fi

exit 0

Producing:

Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
SunOS 5.10
GNU bash 3.00.16
zip - no version provided for /usr/bin/zip.
diff - no version provided for /usr/bin/diff.

 Directory to be zipped:
./d1
/     d2
/     /     d3
/     /     /     hi-in-d1-d2-d3
/     /     hi-in-d1-d2
/     hi-in-d1

 Results from processing:
  adding: d1/ (stored 0%)
  adding: d1/d2/ (stored 0%)
  adding: d1/d2/d3/ (stored 0%)
  adding: d1/d2/d3/hi-in-d1-d2-d3 (stored 0%)
  adding: d1/d2/hi-in-d1-d2 (stored 0%)
  adding: d1/hi-in-d1 (stored 0%)
-rw-r--r--   1     829 Jul 10 10:45 d1.zip

 Unzipping:
Archive:  d1.zip
   creating: d1/
   creating: d1/d2/
   creating: d1/d2/d3/
 extracting: d1/d2/d3/hi-in-d1-d2-d3
 extracting: d1/d2/hi-in-d1-d2
 extracting: d1/hi-in-d1

 Newly unzipped d1:
d1
d2
/     d3
/     /     hi-in-d1-d2-d3
/     hi-in-d1-d2
hi-in-d1

 Old d1:
old-d1
d2
/     d3
/     /     hi-in-d1-d2-d3
/     hi-in-d1-d2
hi-in-d1

 Comparison of d1 and old-d1:
Common subdirectories: d1/d2 and old-d1/d2
Common subdirectories: d1/d2/d3 and old-d1/d2/d3
 Directories d1 and old-d1 have the same content.

Perhaps you can see from this what you should do next. The man pages and small experiments are often the best road to solutions ... cheers, drl
Sponsored Links
    #3  
Old 07-10-2008
gdog gdog is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 10 July 2008, 12:17 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Hi drl,

I ran your code on my machine alright, but when I access the d1 zipfile through windows I get this:



Now, I would like to see the d2 and d3 subfolders in d1.zip with hi-in-d1-d2-d3 and hi-in-d1-d2 in there respective subfolders. Do you know how to do that by any chance?

Thanks,
gdog

PS: I don't mind doing it with tar if it's easier.

Last edited by gdog; 07-10-2008 at 12:17 PM..
    #4  
Old 07-10-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Last Activity: 16 September 2014, 8:55 AM EDT
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 1,673
Thanks: 39
Thanked 193 Times in 176 Posts
Hi.

You know the directories are there, or at least their properties exist in the zip file because unzip can re-create them.

I don't know anything about winzip. I'd look through all the menus and help information to start with. Perhaps someone will stop by with an answer for that question ... cheers, drl
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
Tar-ing folders in a folder HappyPhysicist UNIX for Dummies Questions & Answers 5 08-31-2011 09:34 AM
moving files from one folder to many folders realspirituals Shell Programming and Scripting 2 08-18-2011 04:30 AM
Remove all folders within another folder except the lastest 1, 2 or 3. damang111 Shell Programming and Scripting 14 02-02-2011 05:40 PM
Move all files but not folders to a new folder Hopper_no1 Shell Programming and Scripting 4 12-09-2010 10:44 AM
delete all folders/files and keep only the last 10 in a folder melanie_pfefer Shell Programming and Scripting 3 11-17-2006 02:33 PM



All times are GMT -4. The time now is 03:28 PM.