0

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
9
  • select the code block and then click {}. remove ` enclosing each line. Sep 27, 2020 at 6:47
  • Are there folders with names like 1, 2, etc., i.e. with sequence numbers between the ones with step 1000?
    – Kusalananda
    Sep 27, 2020 at 7:32
  • The folders are "0", "1000", "2000" with a step of 1000 and they go all the way up to "28000000".
    – Global
    Sep 27, 2020 at 7:33
  • 1
    Ok, so the issue could be formulated as "create a Zip file for each folder under private/completed regardless of the folder's name".
    – Kusalananda
    Sep 27, 2020 at 7:38
  • 1
    Does this answer your question? unix.stackexchange.com/questions/68489/… (with the "single directory" being your private/completed directory).
    – Kusalananda
    Sep 27, 2020 at 7:44

2 Answers 2

0

You can use the for loop; like so; for example : seq start step end

for  i in `seq 1000 1000 2000000`;do echo $i; done;

You can use your cp command and use $i for directory name and foldername

1
  • Thank you for help, Sandeep. Sorry, I'm really green in bash. I just happen to have to work with a Debian server since yesterday and need to solve an everyday automation problem. So, I kind of really need a ready-to-paste code :-)
    – Global
    Sep 27, 2020 at 7:35
0

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

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