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.



Output Need It :

  • 1
    How does the output relate to the example? – Ignacio Vazquez-Abrams Nov 29 '10 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 '10 at 20:54
( cd /var/www ; zip -r - test1 ) > output.zip
  • this is correct regardless of duplicate +1 – RobotHumans Nov 29 '10 at 17:55

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

  • zip is capable of recursing a path on its own. – Ignacio Vazquez-Abrams Nov 29 '10 at 10:01
  • I updated the question. This is not what I need. – danidacar Nov 29 '10 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 '10 at 12:10
  • @Ignacio and Charles, so maybe zip -r -x *.zip would achieve the same? – Arjan Nov 29 '10 at 12:30

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
  • Because I'm used to command line flexibility and I thought I'm missing something. Good tip though. – danidacar Nov 29 '10 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 '10 at 16:56

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


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.