1

Here's what I would like to achieve: I have some directories, and subdirectories in them (which may have sub-subdirectories). In each subdirectory I want a zip file with contents of that particular directory.

find nicely finds my subdirectories:

find . -mindepth 2 -maxdepth 2 -type d

but I have a hard time completing the

-exec zip ... \;

Would you be able to help me with that? I tried many combinations, can't figure it out. :(

For clarification, here's a tree.

├── DIR1
│   ├── FILE1
|   ├── FILE2
│   └── FILE3
├── FILE4
├── DIR2
│   ├── DIR3
│   │   ├── SUBDIR1
│   │   │   ├── SUBFILE1
│   │   │   ├── SUBSUBDIR1
│   │   │   │   ├── SUBSUBFILE1
│   │   │   │   └── SUBSUBFILE2
│   │   │   └── SUBSUBDIR1
│   │   │       ├── SUBSUBFILE3
│   │   │       └── SUBSUBFILE4
│   │   ├── FILE4
│   │   ├── FILE5
│   │   ├── FILE6
│   │   └── Zip file named DIR3.zip with FILE4, FILE5, FILE6

1 Answer 1

2

Using -execdir instead of -exec runs the command from the directory find is currently traversing, which makes this problem a little easier:

find . -mindepth 2 -maxdepth 2 -type d -execdir zip -r {}.zip {} ';'

Note that the string {} is interpreted by find as the current filename, and is relative when -execdir is used.

1
  • Hey, works great. Thanks! Tried something akin to that, with -execname, not sure why it did not work outf.
    – user11426
    Commented Nov 30, 2015 at 9:01

You must log in to answer this question.

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