1

How can I create a zip in a linux distro and
- keep only the relative path to a folder
- put some files in a separate folder inside the zip

I need to do this in a bash script so if i just use the absolute path the entire path will be inside the zip.

Example

/home/user/folder/1.txt
/home/user/folder/2.txt

Output Need It :

test.zip
folder/1.txt
folder/secondfolder/2.txt
2
  • 1
    How does the output relate to the example? Nov 29, 2010 at 9:55
  • Might this be better suited to ServerFault? It seems more like a sysadmin question than a programming question to me.
    – ssokolow
    Nov 29, 2010 at 20:54

4 Answers 4

1
( cd /var/www ; zip -r - test1 ) > output.zip
1
  • this is correct regardless of duplicate +1 Nov 29, 2010 at 17:55
0

Does find . ! -name "*.zip" -type f -print | zip test -@ do what you want?

4
  • zip is capable of recursing a path on its own. Nov 29, 2010 at 10:01
  • I updated the question. This is not what I need.
    – johnlemon
    Nov 29, 2010 at 10:23
  • @user, I updated the question -- adding "need it" is hardly an update! (Also, please come up with a better title?)
    – Arjan
    Nov 29, 2010 at 12:10
  • @Ignacio and Charles, so maybe zip -r -x *.zip would achieve the same?
    – Arjan
    Nov 29, 2010 at 12:30
0

Why don't you just make the directory structure you want and then zip it and then remove it?

cd ~/
mkdir backup1
cp /var/www/test/* backup1/
zip -r output.zip ~/backup1
rm -r backup1
2
  • Because I'm used to command line flexibility and I thought I'm missing something. Good tip though.
    – johnlemon
    Nov 29, 2010 at 16:48
  • man zip doesn't seem to allow you to create directories. If you want to do in one line; just cp /var/www/test/* backup1/ && zip -rm output.zip backup1/ (m for move the files) part is to not have to have feature-bloat; there's no real reason to let zip create files/dirs that don't exist, when you can do it perfectly fine with other commands.
    – jimbob
    Nov 29, 2010 at 16:56
0

$ ln -s /var/www/test backup1
$ zip -r backup1 -j backup1/*

Acceptable?

1
  • You are junking the directories and create a new sim link in the process.. I don't think it's optimal. Thanks anyways.
    – johnlemon
    Nov 29, 2010 at 16:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .