0

Rawinput:

➜  datatest tree
.
├── a
│   ├── README.md
│   ├── code
│   └── data
│       └── apple.csv
├── archive.sh
├── f
│   ├── README.md
│   ├── code
│   └── data
│       ├── Archive.zip
│       ├── a.csv
│       ├── b.xlsx
│       └── c.xlsx
└── toolbox
    └── tool.py

7 directories, 9 files

Output:

➜  datatest tree
.
├── a
│   ├── README.md
│   ├── code
│   └── data
│       ├── Archive.zip
│       └── apple.csv
├── archive.sh
├── f
│   ├── README.md
│   ├── code
│   └── data
│       ├── Archive.zip
│       ├── a.csv
│       ├── b.xlsx
│       └── c.xlsx
└── toolbox
    └── tool.py

7 directories, 10 files

In each /data subfolder, files should be compressed to Archive.zip except it already exists Archive.zip(like f folder).

Trying:

I have to check if there is no Archive.zip in any /data subfolder by using tree command-line, then zip -r Archive.zip ./* , which is inconvenient.

Hope:

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

3

Something like this maybe?

find . -type d -name data \
    \! -exec test -f {}/Archive.zip ';' \
    -execdir zip -rj data/Archive.zip data ';'

This would locate each data directory (first line).

The \! -exec test -f {}/Archive.zip ';' would filter out any data directory that does not contain any file called Archive.zip.

This line may be replaced by \! -execdir test -f data/Archive.zip ';'.

The last -execdir would execute the given zip command from within the parent directory of the data directory. This would create data/Archive.zip containing the files in data (with no path attached to the archived filenames).

This is similar to my answer to your previous question, but with the test for existance of data/Archive.zip inserted.

  • Minor improvement: it might be cleaner to replace both -exec and -execdir with a single command, -execdir 'sh -c "[ -f data/Archive.zip ] || zip -rj data/Archive.zip data" sh'. – Satō Katsura Aug 27 '17 at 10:03
  • @SatōKatsura That would probably be possible. If it's cleaner or not is debatable :-) – Kusalananda Aug 27 '17 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.