Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Zip a folder including its sub-folders. Post 302213561 by drl on Thursday 10th of July 2008 11:51:41 AM
Old 07-10-2008
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
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

zip nesting empty folders

I'm using the following command to zip a project file, but when it finishes, the resulting zip file contains all the directories above the file I wanted zipped, myapp.app, each one empty until you get to the actual app. zip -r myapp.app.zip ... (0 Replies)
Discussion started by: groundlevel
0 Replies

2. Windows & DOS: Issues & Discussions

How can I upload a zip folder on a unix path from my windows folder?

Hello, I am an amature at UNIX commands and functionality. Please could you all assist me by replying to my below mentioned querry : How can I upload a zip folder on a unix path from my windows folder? Thanks guys Cheers (2 Replies)
Discussion started by: ajit.yadav83
2 Replies

3. UNIX for Dummies Questions & Answers

Zip command (zip folder doesn't include a folder of the same name)

Hi guys, I have a question about the zip command. Right now I have a directory with some files and folders on it that I want to compress. When I run the zip command: zip foo -r I am getting a foo.zip file that once I unzip it contains a foo folder. I want to create the foo.zip, but that... (1 Reply)
Discussion started by: elioncho
1 Replies

4. UNIX for Dummies Questions & Answers

Create zip without including directories

Hi guys, I'm trying to do the following: zip -r /tmp/foo.zip public/accounts/foo But the zip that's been made has the whole "public/accounts/foo" path. I want only the foo folder to be zipped. How can I do this? Thanks, Elías (2 Replies)
Discussion started by: elioncho
2 Replies

5. Shell Programming and Scripting

Zip all the files including directories - subdirectories

Hi, Is is possible to zip a folder and all its contents within the folder ( including sub-directories and files) into a zip file? and can regain the same structure if unzipped? Thanks (6 Replies)
Discussion started by: rudoraj
6 Replies

6. Shell Programming and Scripting

Find file and zip without including directory path

Does anyone know of a way to zip the resulting file from a find command? My approach below finds the file and zips the entire directory path, which is not what I need. After scanning the web, it seems to be much easier to perform gzip, but unfortunately the approach must use zip. find `$DIR`... (5 Replies)
Discussion started by: koeji
5 Replies

7. Shell Programming and Scripting

Copy Folders and ZIP IT

HI All I have one master folder : ABCXYZ I have sub folder in there : AB XY AZ AC PR AL Now i want to copy AB , PR ,AL in to one new folder and zip it with time stamp. like Pre_02192013_12_32.zip Zip folder should be in master folder. (2 Replies)
Discussion started by: pareshkp
2 Replies

8. Shell Programming and Scripting

How to auto zip all folders?

Hi, How do I write a script that will automatically find and zip any folder to the same name in a directory and affected recursively?. The zip file should be place in the same directory where the source folder is. Help is appreciated. Thanks in advance. ex: Orange Crayon Blue Crayon... (5 Replies)
Discussion started by: Frozen77
5 Replies

9. Shell Programming and Scripting

Gunzip,grep and zip across folders in a subdirectory

I have a simple function that greps within a folder into files that are already gunzipped. This was already written for me so from my understanding fn=$1 specifies that it will look into any file in that folder that is gunzipped and the rest of the script greps the data its looking for and puts it... (3 Replies)
Discussion started by: red56
3 Replies

10. UNIX for Beginners Questions & Answers

Tar.gz/zip folders in a directory with _EBASE string

Hi Folks - Happy Friday and I hope you all are well! What's the easiest way to tar.gz / zip all direct children directories in a folder that have the string _EBASE (suffix)? Thank you! (6 Replies)
Discussion started by: SIMMS7400
6 Replies
echo(1) 							   User Commands							   echo(1)

NAME
echo - echo arguments SYNOPSIS
/usr/bin/echo [string...] DESCRIPTION
The echo utility writes its arguments, separated by BLANKs and terminated by a NEWLINE, to the standard output. If there are no arguments, only the NEWLINE character will be written. echo is useful for producing diagnostics in command files, for sending known data into a pipe, and for displaying the contents of environ- ment variables. The C shell, the Korn shell, and the Bourne shell all have echo built-in commands, which, by default, will be invoked if the user calls echo without a full pathname. See shell_builtins(1). sh's echo, ksh's echo, and /usr/bin/echo understand the back-slashed escape charac- ters, except that sh's echo does not understand a as the alert character. In addition, ksh's echo, does not have an -n option. sh's echo and /usr/bin/echo only have an -n option if the SYSV3 environment variable is set (see ENVIRONMENT VARIABLES below). If it is, none of the backslashed characters mentioned above are available. csh's echo and /usr/ucb/echo, on the other hand, have an -n option, but do not under- stand the back-slashed escape characters. OPERANDS
The following operand is supported: string A string to be written to standard output. If any operand is "-n", it will be treated as a string, not an option. The following character sequences will be recognized within any of the arguments: a Alert character.  Backspace. c Print line without new-line. All characters following the c in the argument are ignored. f Form-feed. New-line. Carriage return. Tab. v Vertical tab. \ Backslash. n Where n is the 8-bit character whose ASCII code is the 1-, 2- or 3-digit octal number representing that character. USAGE
Portable applications should not use -n (as the first argument) or escape sequences. The printf(1) utility can be used portably to emulate any of the traditional behaviors of the echo utility as follows: o The Solaris 2.6 operating environment or compatible version's /usr/bin/echo is equivalent to: printf "%b " "$*" o The /usr/ucb/echo is equivalent to: if [ "X$1" = "X-n" ] then shift printf "%s" "$*" else printf "%s " "$*" fi New applications are encouraged to use printf instead of echo. EXAMPLES
Example 1: Finding how far below root your current directory is located You can use echo to determine how many subdirectories below the root directory (/) is your current directory, as follows: o Echo your current-working-directory's full pathname. o Pipe the output through tr to translate the path's embedded slash-characters into space-characters. o Pipe that output through wc -w for a count of the names in your path. example% /usr/bin/echo $PWD | tr '/' ' ' | wc -w See tr(1) and wc(1) for their functionality. Below are the different flavors for echoing a string without a NEWLINE: Example 2: /usr/bin/echo example% /usr/bin/echo "$USER's current directory is $PWDc" Example 3: sh/ksh shells example$ echo "$USER's current directory is $PWDc" Example 4: csh shell example% echo -n "$USER's current directory is $PWD" Example 5: /usr/ucb/echo example% /usr/ucb/echo -n "$USER's current directory is $PWD" ENVIRONMENT VARIABLES
See environ(5) for descriptions of the following environment variables that affect the execution of echo: LANG, LC_ALL, LC_CTYPE, LC_MES- SAGES, and NLSPATH. SYSV3 This environment variable is used to provide compatibility with INTERACTIVE UNIX System and SCO UNIX installation scripts. It is intended for compatibility only and should not be used in new scripts. EXIT STATUS
The following error values are returned: 0 Successful completion. >0 An error occurred. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ |CSI |enabled | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ SEE ALSO
echo(1B), printf(1), shell_builtins(1), tr(1), wc(1), ascii(5), attributes(5), environ(5), standards(5) NOTES
When representing an 8-bit character by using the escape convention n, the n must always be preceded by the digit zero(0). For example, typing: echo 'WARNING:7' will print the phrase WARNING: and sound the "bell" on your terminal. The use of single (or double) quotes (or two backslashes) is required to protect the "" that precedes the "07". Following the , up to three digits are used in constructing the octal output character. If, following the n, you want to echo addi- tional digits that are not part of the octal representation, you must use the full 3-digit n. For example, if you want to echo "ESC 7" you must use the three digits "033" rather than just the two digits "33" after the . 2 digits Incorrect: echo"0337 | od -xc produces: df0a (hex) 337 (ascii) 3 digits Correct: echo "00337" | od -xc produces: lb37 0a00 (hex) 033 7 (ascii) For the octal equivalents of each character, see ascii(5). SunOS 5.10 20 Jan 2000 echo(1)
All times are GMT -4. The time now is 01:32 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy