How do I create a ZIP archive that preserves only the structure of the target directory and below?

9,944

Solution 1

sh -c "cd /tmp/sub_dir/pertinent_dir/../  \
&& zip -r pertinent_dir.zip pertinent_dir/*"

Solution 2

You could use jar instead of zip:

jar cfM /tmp/sub_dir/pertinent_dir.zip -C /tmp/sub_dir pertinent_dir
Share:
9,944

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    I need to create a zip archive where the unzipped result preserves the directory structure from the specified directory down. I also need to do this without actually switching to the directory.

    My question is similar to this. After having read a great deal on the issue, I don't feel that anything has completely answered my question.

    For example, I have:

    /tmp
        /sub_dir
            /pertinent_dir
                /sub_folder_1/
                    stuff.pdf
                    stuff.xls
                /sub_folder_2/
                    stuff.pdf
                    stuff.xls
                /sub_folder_3/
                    stuff.pdf
                    stuff.xls
    
    

    I need to be able to perform zip -r /tmp/sub_dir/pertinent_dir.zip /tmp/sub_dir/pertinent_dir/* such that the result only contains pertinent_dir and below. Again, I need to do this regardless of my current directory context. I've tried -j, but that doesn't seem to work. Perhaps it's not possible. Is there something I'm missing?

  • user unknown
    user unknown about 12 years
    You should probably mention that jar is part of the Java installation, and creates compatible zipfiles which can be extracted with unzip and so on.