Unzip wrongly: All files are scattered in the current directory - linux

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.

Related

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

How to Delete the Old Contents of a Zip File When Making a New Zip

Currently I'm using the following command to zip all my folders and files with the exception of a few.
zip -r Archive.zip . -x '*.git*' '*.DS_Store*' '*.pyc*'
The problem with this command is it doesn't delete files in the older zip file that I deleted.
So I know that I can just run the rm command on the zip file before making a new one. However, I wanted to see if there's a way I can essentially combine the zip command above with an rm command.
You can delete a specific file from zip archive with -d flag
-d
--delete
Remove (delete) entries from a zip archive. For example:
zip -d foo foo/tom/junk foo/harry/\* \*.o

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

Zip folder recursively, save for a given sub folder (Linux)

In Linux zip, is it possible to zip a folder recursively except for a given sub-path?
For instance, to zip 'base_folder' and all its sub folders except 'base_folder/sub_folder'.
Is it possible?
And if so, how?
zip -r base_folder.zip base_folder -x base_folder/sub_folder/\* base_folder/another_sub_folder/\*
(I have incorporated the improved information from your experience that you have kindly offered in your comment.)
For more information: man zip
You can use tar with --exclude, and use the -z option to gzip the output tar file.
find the files, exclude the folder, xargs the remaining files to tar or zip or whatever:
find /in/your/dir | grep -v 'name_of_dir_to_be_excluded' | xargs tar jcvf nameOfArchive.tar.bz2

Resources