john-jones
john-jones

Reputation: 7780

Zip stating absolute paths, but only keeping part of them

zip -r 1.zip /home/username/the_folder

At here, when i unzip 1.zip, it will create /home/username/the_folder, from whichever folder i am unzipping from.

How do I zip, stating the full absolute paths, but make the zip only contain the folder structure starting at, in this case for instance, /home/username?

That way I could be at whatever path i wanted, unzip and it would just create the_folder, and not /home/username/the_folder.

Upvotes: 14

Views: 22342

Answers (4)

john-jones
john-jones

Reputation: 7780

Use this command:

cd path_under_folder_to_zip && \
zip -r 1.zip folder_to_zip >/dev/null && \
mv 1.zip my_current_path

Upvotes: 12

jackjr300
jackjr300

Reputation: 7191

Just use the -j option, works on OSX, I don't know about linux.

zip -j -r 1.zip /home/username/the_folder

Upvotes: 1

Niraj Nawanit
Niraj Nawanit

Reputation: 2451

  1. List item

How about this:

function zipExtraFolder {
    if [ $# -lt 2 ]; then
        echo "provide at least two arguments"
        return
    fi
    folder=$2
    mkdir del
    echo cp -r `dirname $folder` del
    cd del
    echo zip -r ../$1 .
    cd -
    rm -rf del
}

Define above as a shell function in your .bashrc and you should be able to use it whenever you want. The usage will be like below.

zipExtraFolder 1.zip /home/username/the_folder

Upvotes: 1

Tri
Tri

Reputation: 528

Use relative path when specifying the file to zip.

cd /home/username
zip -r 1.zip ./the_folder

Then when you unzip, it'll be a relative path starting at whichever folder you're in when unzipping.

Upvotes: 7

Related Questions