How can I unzip files in Linux when other files are present - linux

I have files such as these
a1.tif
a2.tif
a1.zip
sa.zip
ff.zip
wqqq.zip
I want to unzip only the zip files and ignore the .tif files
Also ignore those .zip files that have already been decompressed.
I am getting this
unzip /*zip
unzip: cannot find or open /*zip, /*zip.zip or /*zip.ZIP.

You are searching for anything in the root directory that ends with "zip". Use *.zip for any file in the current directory ending in ".zip", or more explicitly use ./*.zip, ./ being the current directory.
That said, if you have more than one match it will pass all of them to the unzip command with the first being interpreted as the archive file and the remaining matches as files to extract from the archive. Avoid this by using find to pass each match to unzip.
find . -name '*.zip' -exec unzip {} \;

Related

Shell - create zip files in directories without including subdirectories

I have a structure like this:
file1.jpg
file2.png
subfolder1
subfolder2
file3.jpg
...
now I would like to create a zip file via shell only of the files present in the root of the directory without taking into consideration the subdirectories and the files contained within them.
How can I do? I hope I have been clear enough
You can try something like this by using a find.
It will only find files that are in the current folder and zip it to a file called filename.zip
find . -maxdepth 1 -type f -exec zip filename.zip '{}' \;

How to zip a folder, but minus the files using zip command line

I am creating a zip file of my application tree, minus folders that have temporary files. For now I exclude the folders using -x option and manually created them with unzip. Is there a way with zip to exclude the files but include the folder (i.e. it would be an empty folder in the zip file?)
I am using
zip -r zipfile.zip . -x appsessions/\* workfolder/\*
but of course it excludes the folders and files in them. I would like to keep appsessions/ and workfolder/ in the zip file, but empty.
Give a try to this:
find . -type d -print | zip name.zip -#
Also check this answer: https://stackoverflow.com/a/13234936/1135424

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

tar/gzip excluding certain files

I have a directory with many sub-directories. In some of those sub-directories I have files with *.asc extension and some with *.xdr.
I want to create a SINGLE tarball/gzip file which maintains the directory structure but excludes all files with the *.xdr extension.
How can I do this?
I did something like find . -depth -name *.asc -exec gzip -r9 {} + but this gzips every *.asc file individually which is not what I want to do.
You need to use the --exclude option:
tar -zc -f test.tar.gz --exclude='*.xdr' *
gzip will always handle files individually. If you want a bundled archive you will have to tar the files first and then gzip the result, hence you will end up with a .tar.gz file or .tgz for short.
To get a better control over what you are doing, you can first find the files using the command you already posted (with -print instead of the gzip command) and put them into a file, then use this file (=filelist.txt) to instruct tar with what to archive
tar -T filelist.txt -c -v -f myarchive.tar

Script to extract zip files in seperate folders to their own folders

I have hundreds of folders each containing a zip file. I would like to extract each zip file to where they are located. Is there a simple trick or script to do this?
EDIT:
Each folder is under the same parent folder. So the hierarchy is as the following:
PARENT FOLDER
-SubFolder1
--somefile.zip
-Subfolder2
--somefile.zip
...
-SubfolderN
--somefile.zip
Under unix you could use something like
find <dir> -iname '*.zip' -execdir unzip {} \;
The program find traverses <dir> recursively and on every .zip file it finds it will change to that files directory and executes unzip on it.
Windows version:
for /r "C:\Some\Directory" %f in (*.zip) do unzip "%f" -d "%~dpf"
Warning: Completely untested.
References:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx
http://www.info-zip.org/mans/unzip.html
I think with 7-zip it would be
for /r "C:\Some\Directory" %f in (*.zip) do 7z x -o "%~dpf" "%f"
but that's even untesteder.

Resources