How can I use unzip in linux to extract only dir2 from zipfile.zip/dir1/dir2? - linux

I have zip file with 1 directory that stored another directory and the second hold some files. How can I unzip in one command in order to get dir 2 along with it's files.
thanks,

Did you try typing man unzip?
unzip zipfile.zip dir1/dir2
If you want to ignore the zip's directory structure, use the -j (Junk paths) option:
unzip -j zipfile.zip dir1/dir2 -d dir2
This will store all files recursively found under dir1/dir2 into dir2 under the current folder.

Related

Zip and Tar compress entire top directory and not (sub)directory

I'm trying to only zip the directory where I am exactly, this is part of a bigger bash script so I need to cd into the directory where I want to extract files and then exit.
However, using either tar or zip, the entire top directory path is recreated and not just the subdirectory that I'm interested in.
I get the following error:
zip warning: name not matched: $PWD/*
What I'm trying to do:
#Sub Directory and contents will be compressed
cd "$Sub_Dir"
Zipped_Files=$(basename "$Sub_Dir")
zip -r "$Zipped_Files".zip "$PWD/*"
#or
zip -j "$Zipped_Files".zip "$PWD/*"
#or
#tar -zcf "$Zipped_Files".zip "$PWD"
echo "Files have been compressed"
You have already cd into the sub dir, zip -r "$Zipped_Files".zip ./* should work.

Zip folder exclude some folders

I'm trying to backup my www-folder but hidden folders like .config inside www are added to the backup. I want to exclude the folder "backups" and all folders (and files) starting with a dot.
The problem is that it copies all the hidden folders like .config to the zip-file.
Current code:
zip -r /var/www/backups/site/$(date +\%Y-\%m-\%d-\%H-\%M).zip /var/www -x "*backups*" "*.*" "*/.*"
This should work for you.
zip -r --exclude=*backups* --exclude=*/.* /var/www/backups/site/$(date +\%Y-\%m-\%d-\%H-\%M).zip /var/www
Use a linux find command with an exclude flag, then pipe it into zip.
The following command will exclude all paths under the current directory containing the keywords "backups" or files with "/." in the path and then pipe the files into zip.
find . | grep -v "\(backups\|/\.\)" | xargs zip archive.zip

Extract zip files contents and rename the directory dynamically

I have an application zip file created using Play Framework. It create the zip file with name A-1.0.zip. This zip file contains the directory with name A-1.0. (1.0 changes according to the version)
I wanted to extract the zip file and rename the folder from A-1.0 to A. So that my application init.d script finds the directory to start the application. This shuld be done dynamically using shell script.
Is there a way where i can extract all the zip files into A folder instead of extracting into A-1.0 and renaming?? Please help!
The following is what I tried....
unzip A-1.0.zip -d ~/A
(I know that it is very dumb of me to do this !!)
This extracted the file into ~/A/A-1.0/[contents]
I need to extract all the [contents] into ~/A instead of ~/A/A-1.0/. I dunno how to do this using command line.....
My init.d script searched for ~/A/bin/A -Dhttp.port=6565 -Dconfig.file=~/A/conf/application.conf to start the Play! application.
To make this script working, I extract all into A-1.0/ then I rename with mv ~/A-1.0 ~/A manually.
I didn't find any specific unzip option to perform this automatically, but managed to achieve this goal by creating a temporary symbolic link in order to artificially redirect the extracted files this way
ln -s A A-1.0
unzip A-1.0.zip
rm A-1.0
From the unzip man page it boils down to:
unzip A-1.0.zip 'A-1.0/*' -d /the/output/dir
^ ^
| |
| +- files to extract (note the quotes: unzip shall parse the wildcard instd of sh)
+- The archive
EDIT: This answer does not preserve subdirectories. It works fine if one doesn't have or need the subdirectory structure.
I found that you can combine the answer from #géza-török with the -j option mentioned by #david-c-rankin (in the comment below the question). Which leads to unzip -j A-1.0.zip 'A-1.0/*' -d /the/output/dir. That would only process the files inside A-1.0/ and output them straight into the given output directory.
Source: https://linux.die.net/man/1/unzip (look at -j)
I was looking to unzip all .zip files in the current directory into directories with names of the zip files (even after you rename them)
the following command is not elegant but works
cd to/the/dir
find *.zip | cut -d. -f1 | xargs -I % sh -c "unzip %.zip -d %; ls % | xargs -I # sh -c 'mv %/#/* %; rm -rf %/#'"

Zip including hidden files

In Linux I can zip all(except hidden files) in current directory by doing:
zip 1.zip *
But how do I include the hidden files?
EDIT: The correct way is zip -r 1.zip .
The commands shown in my previous answer below are incorrect because they also include the parent directory.
Have you tried this:
zip yourfile.zip sourcedir/* .*
or you in your case
zip 1.zip * .[^.]*
It should include all hidden files also.
Or you can add more simple
zip 1.zip ./
Just to be sure it is not forgotten since this is a forum for developers and a good number of us use git.
An easy way to get only what you want in the zip is to use git archive -o filename.zip branch
If you want to zip all files (+hidden files)
Kindly using: zip -r namefiles.zip .
The "." is all files in folder.
zip -r namefiles.zip "folder will zip"
On macOS 10.15.7 I had to separatelly add all dot leading files (\.*) and rest of the files (*):
zip -r file.zip \.* *
if you don't have rights to save zip file in current dir you can go to dir where you have rights and type
zip -r 1.zip /path/to/source/dir/.
However when if in .../some_dir you type
unzip 1.zip
then your files will be decompress into .../some_dir/path/to/source/dir/
zip -r 1.zip .* -x "../*"
Just doing zip -r 1.zip .* will include the parent folder as well so the trick is to exclude the parent folder using -x "../*"
If you'd like to save some subdirectory of the current directory recursively with hidden and regular files just type
$ zip -r backup_subdirectory.zip backup_subdirectory/. backup-subdirectory/*
And for unzipping:
$ unzip backup_subdirectory.zip
Or even simpler by using tar for creating an archive:
$ tar czvf backup_subdirectory.tar.gz backup_subdirectory/
And for extracting all files from the archive:
$ tar xzvf backup_subdirectory.tar.gz

Create zip file and ignore directory structure

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 a directory structure mimicking the directory to the raw file. It is a lot of extra folders that I don't need.
I didn't find an answer in a cursory glance over the man page or a Google hunt.
You can use -j.
-j
--junk-paths
Store just the name of a saved file (junk the path), and do not
store directory names. By default, zip will store the full path
(relative to the current directory).
Using -j won't work along with the -r option.
So the work-around for it can be this:
cd path/to/parent/dir/;
zip -r complete/path/to/name.zip ./* ;
cd -;
Or in-line version
cd path/to/parent/dir/ && zip -r complete/path/to/name.zip ./* && cd -
you can direct the output to /dev/null if you don't want the cd - output to appear on screen
Use the -j option:
-j Store just the name of a saved file (junk the path), and do not
store directory names. By default, zip will store the full path
(relative to the current path).
Somewhat related - I was looking for a solution to do the same for directories.
Unfortunately the -j option does not work for this :(
Here is a good solution on how to get it done:
https://superuser.com/questions/119649/avoid-unwanted-path-in-zip-file
Alternatively, you could create a temporary symbolic link to your file:
ln -s /data/to/zip/data.txt data.txt
zip /dir/to/file/newZip !$
rm !$
This works also for a directory.
Just use the -jrm option to remove the file and directory
structures
zip -jrm /path/to/file.zip /path/to/file
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

Resources