How to add files to zip archive without a intermediate folder - linux

I'm using zip program in a bash script and i would like to create an archive containing all files in a folder without adding the folder itself to the archive.
I have such files :
script.sh
files/
files/1
files/2
I'm using this command in script.sh
zip -q -9 -r arch.zip files/*
but this creates a files folder in the archive and i would like to get files 1 and 2 directly at the root of the archive.
How can i modify the parameters passed to zip command to prevent it from adding files in this archive ?
Thanks
FIXED :
Here and Here
Obviously, SO search engine is more efficient when the question is posted than before ...

cd files
zip -q -9 -r ../arch.zip *

you should use -j key. Try this way:
zip -q -9 -j arch.zip files/*

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.

Unzip wrongly: All files are scattered in the current directory

On CentOS, I wanted to unzip files in A.zip into ./A/. However, I didn't notice that there were hundreds of files in A.zip and I just use unzip A.zip. So now these extra files are all in the current directory. How could I solve this problem?
Thank you very much for any help!
You can try this -
unzip -Z1 is the zip info mode which basically returns the files which were zipped. The output is then piped onto other command which removes that file based on the input(from the previous command).
Assuming, first you take a proper backup of that folder.
unzip -Z1 t1.zip | xargs rm -f
If the zip files has folders inside of it then
unzip -Z1 t1.zip | xargs rm -rf
t1.zip is the zip file which I tested with.

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

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.

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

Resources