Create zip file and ignore directory structure

后端 未结 7 1071
栀梦
栀梦 2020-12-07 10:38

I need to create a zip file using this command:

zip /dir/to/file/newZip /data/to/zip/data.txt

This works, but the created zip file creates

7条回答
  •  鱼传尺愫
    2020-12-07 11:02

    Retain the parent directory so unzip doesn't spew files everywhere

    When zipping directories, keeping the parent directory in the archive will help to avoid littering your current directory when you later unzip the archive file

    So to avoid retaining all paths, and since you can't use -j and -r together ( you'll get an error ), you can do this instead:

    cd path/to/parent/dir/;
    zip -r ../my.zip ../$(basename $PWD)
    cd -;
    

    The ../$(basename $PWD) is the magic that retains the parent directory.

    So now unzip my.zip will give a folder containing all your files:

    parent-directory
    ├── file1
    ├── file2
    ├── dir1
    │   ├── file3
    │   ├── file4
    

    Instead of littering the current directory with the unzipped files:

    file1
    file2
    dir1
    ├── file3
    ├── file4
    

提交回复
热议问题