Shell – How to compress all files from several subfolders

command linecompressionshell-scriptzip

Raw input:

    ➜  test tree
    .
    ├── f1.md
    ├── f2.md
    ├── f3.md
    ├── f4.txt
    ├── f5.csv
    ├── f6.doc
    ├── s1
    │   ├── code
    │   └── data
    │       ├── f1.md
    │       ├── f2.md
    │       ├── f3.md
    │       ├── f4.txt
    │       ├── f5.csv
    │       └── f6.doc
    ├── s2
    │   ├── code
    │   └── data
    │       ├── f1.md
    │       ├── f2.md
    │       ├── f3.md
    │       ├── f4.txt
    │       ├── f5.csv
    │       └── f6.doc
    ├── s3
    │   ├── code
    │   └── data
    │       ├── f1.md
    │       ├── f2.md
    │       ├── f3.md
    │       ├── f4.txt
    │       ├── f5.csv
    │       └── f6.doc
    └── s4
        ├── code
        └── data
            ├── f1.md
            ├── f2.md
            ├── f3.md
            ├── f4.txt
            ├── f5.csv
            └── f6.doc

    12 directories, 30 files

Expected output

➜  test tree
.
├── f1.md
├── f2.md
├── f3.md
├── f4.txt
├── f5.csv
├── f6.doc
├── s1
│   ├── code
│   └── data
│       ├── Archive.zip
│       ├── f1.md
│       ├── f2.md
│       ├── f3.md
│       ├── f4.txt
│       ├── f5.csv
│       └── f6.doc
├── s2
│   ├── code
│   └── data
│       ├── Archive.zip
│       ├── f1.md
│       ├── f2.md
│       ├── f3.md
│       ├── f4.txt
│       ├── f5.csv
│       └── f6.doc
├── s3
│   ├── code
│   └── data
│       ├── f1.md
│       ├── f2.md
│       ├── f3.md
│       ├── f4.txt
│       ├── f5.csv
│       └── f6.doc
└── s4
    ├── code
    └── data
        ├── Archive.zip
        ├── f1.md
        ├── f2.md
        ├── f3.md
        ├── f4.txt
        ├── f5.csv
        └── f6.doc

12 directories, 33 files

I want files from subfolders(s1/s2/s4) to compress.

I tried to use command-line zip -r Archive.zip ./* in each subfolder( s1/s2/s4) . It's inconvenient because I have to enter same commands for three times.

How do I using a command once or writing a script to achieve this? I'm on OSX(10.12.6).

Best Answer

Something like this script can do the work:

for i in `find /path -type d -name data`;
do
if [ "$i" = "s3/data" ]
 then continue
 else cd "$i" && zip Archive.zip *
fi
done

P.S. Replace /path with starting point of your directory tree

Related Question