Shell script Q?

I have expanded zip files for many different downloads from Thingiverse and similar.

I want to just tidy up my storage and delete either the expanded folder or archive file for the ones I’ve extracted, but I haven’t done them all.

So my logic is if a filename matches the next filename with .zip extension, delete one or the other.

Here are a couple of examples. I have hundreds of these in my Downloads folder alone…
drwx------@ Simple_Sugar_Bowl_with_spoon_1848999
-rw-r–r–@ Simple_Sugar_Bowl_with_spoon_1848999.zip
drwx------@ Skull Bowl widened - 6181744
-rw-r–r–@ Skull Bowl widened - 6181744.zip

So this is what I’ve been playing with:
aname=“xxx”; ls | while read bname; do if [ “$bname” -eq “$aname.zip” ] ; then rm -r $bname ; fi ; done

Pretty simple, but I keep getting syntax errors that I can’t nail down. The native shell on Mac OS is zsh, but I’ve also tried csh and bsh which I know really well from a past life in the *IX world.

9 Likes

I don’t know anything about shell scripts, but I’ll bet you could accomplish your goal using Mac Automator.

9 Likes

You can probably do it in chatgpt in 20 seconds.

10 Likes

Heathens, :rofl:

9 Likes

Thusly

Here’s a safe version in zsh or bash:

do_delete=false

Usage: ./script.sh --delete

if [ “$1” = “–delete” ]; then
do_delete=true
fi

for zip in *.zip; do
base=“${zip%.zip}”
if [ -d “$base” ]; then
if $do_delete; then
rm “$zip”
else
echo “Would delete: $zip”
fi
fi
done

Default: Just prints Would delete: filename.zip
With --delete: Actually deletes the .zip files.

8 Likes

Work of the devil, I say!! :grinning:

Got to eat. I’ll try it later, or tomorrow. Thanks…

9 Likes

Okay, but I think Automator is less heathenish than chatgpt. But I’d still probably go with the easy way out.

7 Likes

I did it by pasting the contents of @eflyguy’s post, then adding “ can you help me finish this project? I’d also like to add a flag that executes the deletes. If the flag is not present it simply outputs what it would do if the flag were present, as a safety measure”

It took about 1 second. It’s a new world.

As with any AI code, look at it carefully and make sure you know what’s doing but skimming it, it looks pretty good. This kind of simple coding question is a piece of cake for LLM’s.

11 Likes

AI (alien invasion)

They’re coming. We have a telescope and we can see their ships headed our way. They will be here soon. We just don’t know if they will be friendly or hostile.

If chat-gpt every creates a book titled “To Serve Man”, you better double-check the table of contents.

12 Likes

That doesn’t work. Formatting is wrong, quotes are not standard, all kinds of issues with it. No matter, just wanted to report back. It’s not a huge priority so I’ll share what I end up with.

Regarding automator, it’s pretty much useless here as well. I know it, I’ve used it, it’s great at what it’s designed for.

Back when I used to do this for a living, I would just write C code to solve problems like this. I’ve played with perl but it came long after I got out of that game. I know shell can do it, I just need to figure out the syntax.

6 Likes

Here we go… easy peasy once I figured out the new syntax for zsh…

pfile=“abc123”
for file in *; do
if [[ $pfile.zip = $file ]]; then rm “$file” ; fi
pfile=$file
done

Cleaned up about 200 redundant zip files and retrieved ~3GB of storage…

14 Likes

Humans: 1, GPT: 0 :slight_smile:

12 Likes

In addition, you get that seratonin boost for figuring something out.

6 Likes

you left out getting to high five yourself :slight_smile:

3 Likes

For the inner geek - I then took it to the next level, and scripted out a way to look inside the .zip archives I had not expanded into folders for .svg, .stl or .dxf files, and moved them over to the same folder as well…

for zip in *.zip ; do
unzip -l “$zip” | grep -E “.STL|.stl|.svg|.SVG|.dxf|.DXF” > NULL
if test $? = 0 ; then mv “$zip” design\ files ; fi
done

(just realized - the forum removed the back slashes in front of every file extension in the grep command)

No pretty women came calling, however… :roll_eyes:

4 Likes

Yup. Discourse uses it as the escape character, so you have to escape the escape so to speak.:man_shrugging:

5 Likes

Interesting that it didn’t with “design files” on the next line, though.

3 Likes

Whoops, I should have said that Discourse escapes leading backslashes. :wink:

3 Likes