You asked if it's possible: yes, this is possible if you mount the zip as a filesystem (or, of course, if you unzip the archive, which I'm assuming you're explicitly not willing to do from Some Good Reason).
See Fuse-Zip for a tool that will do this. You could then do something like:
$ mkdir foo ; fuse-zip foo.zip foo
$ foo/running-my-script-in-foozip.sh
...
$ fusermount -u foo ; rmdir foo
Note that this is going to requires fuse, which in-turn requires a kernel module that you may or may not have. But you asked if it was possible, not if it was convenient.
In BASH you can use the trailing slash (I think it should work in any POSIX shell):
rm -R -- */
Note the --
which separates options from arguments and allows one to remove entries starting with a hyphen - otherwise after expansion by the shell the entry name would be interpreted as an option by rm
(the same holds for many other command line utilities).
Add the -f
option if you don't want to be prompted for confirmation when deleting non-writeable files.
Note that by default, hidden directories (those whose name starts with .
) will be left alone.
An important caveat: the expansion of */
will also include symlinks that eventually resolve to files of type directory. And depending on the rm
implementation, rm -R -- thelink/
will either just delete the symlink, or (in most of them) delete the content of the linked directory recursively but not that directory itself nor the symlink.
If using zsh
, a better approach would be to use a glob qualifier to select files of type directory only:
rm -R -- *(/) # or *(D/) to include hidden ones
or:
rm -R -- *(-/)
to include symlinks to directories (but because, this time, the expansion doesn't have trailing /
s, it's the symlink only that is removed with all rm
implementations).
With bash
, AT&T ksh
, yash
or zsh
you can do:
set -- */
rm -R -- "${@%/}"
to strip the trailing /
.
Best Answer
You can use this loop in
bash
:i
is the name of the loop variable.*/
means every subdirectory of the current directory, and will include a trailing slash in those names. Make sure youcd
to the right place before executing this."$i"
simply names that directory, including trailing slash. The quotation marks ensure that whitespace in the directory name won't cause trouble.${i%/}
is like$i
but with the trailing slash removed, so you can use that to construct the name of the zip file.If you want to see how this works, include an
echo
before thezip
and you will see the commands printed instead of executed.Parallel execution
To run them in parallel you can use
&
:We use
wait
to tell the shell to wait for all background tasks to finish before exiting.Beware that if you have too many folders in your current directory, then you may overwhelm your computer as this code does not limit the number of parallel tasks.