I'm a total bash and SE beginner and in need some help, please. I have more than 2000 folders (their names are incrementals of 1000) on a Debian server, which I need to zip to separate store-only archives with the corresponding incremental names in the folder 1 level-up the hierarchy from the source. Then I need source files folders deleted after each archive is created.
So, below code works for what I need but my current idea is to repeat the same line over 2000 times. I'd like to know if there is a better way to automate this process into just a few extra lines with the use of a loop and I guess a sub-folder variable?
Any help appreciated, please. Thank you very much!
#!/bin/bash
cd private/completed/0/ && zip -r -0 ../0.zip ./* && rm -R 0/ && cd -
cd private/completed/1000/ && zip -r -0 ../1000.zip ./* && rm -R ../1000 && cd -
cd private/completed/2000/ && zip -r -0 ../2000.zip ./* && rm -R ../2000 && cd -
cd private/completed/3000/ && zip -r -0 ../3000.zip ./* && rm -R ../3000 && cd -
cd private/completed/4000/ && zip -r -0 ../4000.zip ./* && rm -R ../4000 && cd -
....
cd private/completed/2800000/ && zip -r -0 ../2800000.zip ./* && rm -R ../2800000 && cd -
UPDATE: Thank you very much for the helpful link, Kusalananda (command to zip multiple directories into individual zip files). I used the code from that related answer and slightly modified it to make it work exactly how I need it. I added the resulting code below:
#!/bin/bash
cd private/completed/
for i in {0..2800000..1000}; do echo "$i" ; cd "$i" ; zip -q -r -0 ../"${i%/}.zip" . && cd .. && rm -R "$i" ; done
{}
. remove ` enclosing each line. – Ahmad Ismail Sep 27 '20 at 06:471
,2
, etc., i.e. with sequence numbers between the ones with step 1000? – Kusalananda Sep 27 '20 at 07:321
folder, or1500
etc.? – Kusalananda Sep 27 '20 at 07:36private/completed
regardless of the folder's name". – Kusalananda Sep 27 '20 at 07:38private/completed
directory). – Kusalananda Sep 27 '20 at 07:44