Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cuylerstuwe/ebe7ee34d3a5371f8aee515b6d47090f to your computer and use it in GitHub Desktop.
Save cuylerstuwe/ebe7ee34d3a5371f8aee515b6d47090f to your computer and use it in GitHub Desktop.
Remove All Zip Files With Matching Directory Names

Shell Script: "Remove All Zip Files With Matching Directory Names"

Problem:

Recently, I've used my Egghead Downloader userscript to download lots of lessons as ZIP files. I've done this from time to time, but haven't always gotten around to actually unzipping them. And sometimes, because the network device that serves them is relatively slow, I haven't stuck around for the unzipping process to finish in order to actually deleting the remaining ZIP files after extracting to folders with the same name.

Solution:

This is the quick cleanup script I wrote to solve the issue -- It will delete any zip file under the current directory whose name matches a directory name.

find . -type d -print0 | xargs -0 printf -- '%s.zip\0' | xargs -0 rm

NOTE: This script is potentially dangerous if used outside of its original purpose, and the rm portion especially should not be run without confirming first (e.g., via echo) that the correct items are piping through the process.

NOTE 2: This also is not necessarily the most optimal solution to the problem. It's just the first solution I made which worked well enough to get the job done. If you have a more efficient solution, feel free to share.

Bonus Problem:

In the past, I'd downloaded lessons not remembering whether I'd downloaded/extracted them in the past, and then extracted them with 7-Zip. 7-Zip created folders with a (1) or (2) suffix to denote these, depending on how many extra copies I'd made. I wanted a way to get rid of these dupulicates, so I made a quick shell script for this.

Bonus Solution:

This is the quick shell script I wrote to get rid of these extra folders:

ls | grep -P '\(\d\)$' --null | tr '\n' '\0' | xargs -0 rm

NOTE: Again, this is not necessarily an optimal solution -- It's just something that works for my needs oand might work for someone with similar needs, with a little tweaking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment