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
Global
  • 1
  • select the code block and then click {}. remove ` enclosing each line. – Ahmad Ismail Sep 27 '20 at 06:47
  • Are there folders with names like 1, 2, etc., i.e. with sequence numbers between the ones with step 1000? – Kusalananda Sep 27 '20 at 07:32
  • The folders are "0", "1000", "2000" with a step of 1000 and they go all the way up to "28000000". – Global Sep 27 '20 at 07:33
  • So there is no 1 folder, or 1500 etc.? – Kusalananda Sep 27 '20 at 07:36
  • No, no such folders. – Global Sep 27 '20 at 07:37
  • 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 '20 at 07:38
  • Yes, as long as each zip filename is the same as corresponding source folder name. Plus deleting the source folder. – Global Sep 27 '20 at 07:42
  • 1
    Does this answer your question? https://unix.stackexchange.com/questions/68489/command-to-zip-multiple-directories-into-individual-zip-files (with the "single directory" being your private/completed directory). – Kusalananda Sep 27 '20 at 07:44
  • Thank you for the link, Kusalananda. It was very helpful. I used the code from a related answer you linked to and slightly modified it to make it work exactly how I need it. – Global Sep 27 '20 at 09:58

2 Answers2

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

Sandeep Kothari
  • 427
  • 2
  • 6
  • 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 '20 at 07: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
Global
  • 1