I need to write a script that copies all .zip files in the subdirectories of ~100 folders. No clue how to write a loop that goes into each folder, searches for a .zip file, and copies it and extracts it to a unique location.
I imagine something like
cp -f /home/folder1/*.zip /home/NameOfFolderOne
but then how do i get it to copy to a unique folder name, which is the title of the zip file?
The zip files are all in subdirectories, i.e. in /home/folder1/folder1A, /home/folder1/folder1B, etc., with one zip file per folder in the directory /home/folder1/___/. I need to unzip each zip file and put it into a different directory, which is titled just /home/filename, not /home/zipdir/
It currently returns an error saying file exists
Also, when I unzip, how do I get it to unzip it to the correct folder?
Maybe this is too confusing. All I want is to unzip the contents of each 'filename.zip' file in those subdirectories into its own folder called /home/'filename'
Is there an unzip to {create directory} command I can run in a loop?
You can cd to newly created directory and unzip the file.
Instead of that, you have a ready-made command for that.
Have a look at the man page of unzip.
[-d exdir]
An optional directory to which to extract files. By default, all files and subdirectories are recreated in the current directory;
the -d option allows extraction in an arbitrary directory (always assuming one has permission to write to the directory). This option
Ok. so zip files are not at the single location. Also you dont want copy the actual file but the extract the file?
probably you should try:
for file in $(find /home/folder1 -name "*.zip" -type f 2>/dev/null)
do
zipdir=${file##*\/}
zipdir=${zipdir%.*}
zipdir=/home/${zipdir}
mkdir $zipdir || echo "unable to create $zipdir"
unzip $file -d $zipdir || echo "unzip failed for $file"
done
Check for unzip command syntax and test it first, as i never used that command.
---------- Post updated at 01:12 AM ---------- Previous update was at 01:06 AM ----------
I am assuming that your filename doesn't contains spaces.
Problem now is it outputs a 1000 entries of:
error: cannot create /cygdrive/'extractedfile.whatever'
where 'extractedfile.whatever' is the name of each file in the compressed *.zip file.
I need to make it go to /cygdrive/c/UNZIPPED/'zipfilename'
(where UNZIPPED is just some random master folder to keep these all in)
And I need the folders to be named whatever their zip file is named, not each individual compressed file within the zip file.
I've played around with the code but can't seem to get by this, otherwise it works great and the unzip -d works fine. Thanks for your help so far!