0

My folder structure is:

YEAR - MONTH - DAY - SUBFOLDERS

I want to compress each subfolder in the [DAY] directories. The solution from here which is:

for i in */; do zip -r "${i%/}.zip" "$i"; done

works fine as long as I'm doing it from a [DAY] folder. But if I change it to:

for i in */*/; do zip -r "${i%/}.zip" "$i"; done

and run from a [MONTH] folder it still works, but the problem is that the .zip now contains an additional parent folder for each additional '/*' in the command when I unpack.

How can I run the command from a [YEAR] folder with '/* /* /*' while making sure that the .zip only contains the lowest level subfolder when I unpack?

3
  • How is this related to Apple? This is probably a better fit on Unix/Linux.
    – Allan
    Jul 30, 2020 at 5:35
  • @Allan Shell scripting is on-topic here :-)
    – nohillside
    Jul 30, 2020 at 5:36
  • 1
    True, but there are subtle differences in commands if this is a Linux box. Just look at grep. That's why I didn't flag to close but asked for clarification.
    – Allan
    Jul 30, 2020 at 5:37

1 Answer 1

1

In your case you can combine two loops to accomplish this

cd YEAR
for month in */; do
    (cd "$month";  for i in */; do zip -r "${i%/}.zip" "$i"; done)
done

PS: If MONTH folders can be empty you need to check for this in the inner loop.

1
  • But...+1 anyway...'cause it's a damm good answer.
    – Allan
    Jul 30, 2020 at 5:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .