Score:4

How do I zip a folder without including all folders in the path?

cn flag

I am attempting to zip a website's home directory but the archive contains the entire path of folders before getting to the one to be zipped. The command zip -r compressed_data.zip /home/vivek/public_html results in compressed_data.zip containing home->vivek->public_html rather than simply public_html and its contents.

What switch can I add to the command to omit including all folders in the path?

Score:5
in flag

By default, the zip process will store the full path relative to the current directory, so if you want to have an archive that looks like this:

/config
/files
/lib
.htaccess
index.html

Then you will need to be in public_html to begin with. That said, you can account for this with a one-liner:

cd /home/vivek/public_html && zip -r ~/compressed_data.zip . * ; cd -

This will change directories, compress the contents, then return you to the home directory.

If you would like to do this as part of a script that is called via a cronjob or other process, create a file like this:

#!/bin/bash
dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
(cd "/home/vivek/public_html" && zip -r "${dir}/compressed_data.zip" ./*)

You can read more about the limits of zip from its manual page

Mark Lee avatar
cn flag
Thanks a lot @terdon and for the bonus of the bash script. I had gone to the manual page but come away without making any progress.
terdon avatar
cn flag
@MarkLee all I did was a trivial edit to this answer. The person who answered (and who deserves your thanks) is [matigo](https://askubuntu.com/users/1222991/matigo), not me!
Mark Lee avatar
cn flag
Thanks @matigo. The system should put your name at the head of the list. I didn't notice it. All praises wrongly given are yours.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.