I have a folder tree which contains multiple .zip folders (approx 600) each containing four files. I wish to extract just one file from each folder and copy it out to a separate folder. To complicate things, the file has the same name in each folder.Is there a script i can run to do this? Any help appreciated.
You can try the following:
mkdir -p "DESTDIR" for i in *.zip ; do unzip "$i" "THEFILE" -d "DESTDIR" mv "DESTDIR/THEFILE" "DESTDIR/$(basename "$i" .zip)_THEFILE" done
DESTDIR with the destination path. And
THEFILE with the filename of the
the file in each .zip file which you wan to extract.
The files are renamed according to the filename of the zip file.
$ unzip FolderName.zip '*/file1.gz' Archive: FolderName.zip extracting: FolderName/b/c/file1.gz extracting: FolderName/b/e/file1.gz extracting: FolderName/b/f/file1.gz
This despite the fact that
FolderName.zip also contains files named things other than
file1.gz. If you can match the desired file with a glob pattern,
unzip can extract it.
I get the impression from your other commentary that you need to concatenate all of these extracted files. Something like this will do what you want, assuming the top-level folder containing the
unzip output is called
FolderName, and the target file (
file1.gz) is at the same folder depth in all subfolders:
$ gzcat FolderName/*/*/file1.gz > one-big-uncompressed-log-file
That is, it skips over two subfolder levels to pull out the
file1.gz files, then uncompresses and concatenates them with the common
gzcat program, found most everywhere you find