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

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.

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 '{}' \;

Copy or move all files in a directory regardles of folder depth or number

Lets say i have a folder named Pictures and I want to move or copy all files out of this folder.
However I also want to move and harvest all of the files who are in sub folders so:
Pictures/1.png
Pictures/yolo/2.png
Pictures/yolo/swag/sand/3.png
Pictures/extra/fire/4.png
I want to move or copy all these files to another folder like results so I get:
results/1.png
results/2.png
results/3.png
results/4.png
Only I have no idea in advance what sub folders will be in the Pictures folder.
How can I accomplish this in bash/shell scripts ?
I also appreciate making it file type neutral so any files are harvested from their directories (not only .png like in my example) and I have no idea what the file name will be (I only used 1...4 because i did not have any idea how to name them).
You can do it like this:
find /absolute/path/to/Pictures -type f -name '*.png' -exec mv -i {} /absolute/path/to/results \;
Another option is to use xargs
find /absolute/path/to/Pictures -name '*.png' | xargs -I files mv files /absolute/path/to/results
You can simply copy all files and subdirectories along with their contents using cp's recursive option:
cp -pr <source_path>/* <destination_path>/
But, moving them recursively is a bit tricky, you will need to create tar files of the subdirectories and move them and then untar the tar files in destination path. As this is a complex process, as a workaround, you can copy the files/directories recursively and then delete the files from original path.
cp -pr <source_path>/* <destination_path>/ && rm -rf <source_path>/*

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

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 {} \;

Rsync make flat copy

I'm trying to write a script that copy all the files of one dir (with subdirs) to the root of another dir.
So Imagine I have this file structure:
/
pic.JPG
PIC5.JPG
FOLDER
pic2.JPG
pic3.JPG
FOLDER2
pic4.JPG
I want all the .JPG files from that directory and copy them over to another destination. But I don't want the directory structure, just the files.
This is what I've got:
"sudo rsync -aq --include '*/' --include '*.JPG' --exclude '*\' /source/picturesRoot/ /destination/flatView/
But it also copies the directories :(
I found this link on stackoverflow:
rsync : Recursively sync all files while ignoring the directory structure
I looked at the solution and didn't see much difference with my command, apart from the * and . in the path. I tried it but it didn't work.
I hope somebody can help me, thanks.
This answer cannot work for you because your pictures are not at the same level in directories. There is no option in rsync to skip the creation of directory structure. In the link you gave, it's working because the user explicitly select source files with *.
You can try something with find and rsync. Find will find files and rsync copy them.
Here is a solution :
find /source/picturesRoot -type f -name "*.JPG" -exec rsync -a {} /destination/flatView/ \;
Be careful, if two files have the same name just one will be in destination directory.

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

Resources