1

First of all, I've read numerous post on here regarding this topic, but none have helped so far, e.g.

Zipping without creating parent folder Unix zipping sub directory not including parent directory

Here's what I want. I have the following folder structure:

- root
-- subroot
--- subsubroot
---- file1.foo
---- file2.foo
---- file3.foo
---- file4.foo

And the result of the zipped file should be, when unzipped

file1.foo
file2.foo
file3.foo
file4.foo

The crux is that the file*.foo are just packages, thus basically being folders itself, so I tried using the -j option in conjunction with my recursive option (-r), but then I just get a flat hierarchy of all the files.

My current shell command looks like this:

find root/subroot/subsubroot -name '*.foo' | grep -v somestring | xargs -I % zip -r foos.zip %

However, the result of that will be exactly like the original structure with all the parent folder.

I don't necessarily have to use the zip command, but the resulting file has to be a zip.

1 Answers1

0

Your command is nearly correct, although you shouldn't need to use the -I option.

Directory Structure:

.
└── root
    └── subroot
        └── subsubroot
            ├── foo1
            │   └── foo1.pkg
            ├── foo2
            │   └── foo2.pkg
            ├── foo3
            │   └── foo3.pkg
            ├── fooA.pkg
            ├── fooB.pkg
            ├── fooC.pkg
            └── nope.pkg

Command:

find root/subroot/subsubroot -name '*.pkg' | grep -v "nope" | xargs zip -jr foo 

Resulting foo.zip:

fooA.pkg
fooB.pkg
fooC.pkg
foo1.pkg
foo2.pkg
foo2.pkg

There is one caveat.

If you have this type of directory structure you'll get an error:

.
└── root
    └── subroot
        └── subsubroot
            ├── foo1
            │   └── foo1.pkg
            ├── foo2
            │   └── foo2.pkg
            ├── foo3
            │   └── fooC.pkg
            ├── fooA.pkg
            ├── fooB.pkg
            ├── fooC.pkg
            └── nope.pkg 

Error:

zip warning:   first full name: root/subroot/subsubroot/foo3/fooC.pkg
               second full name: root/subroot/subsubroot/fooC.pkg
               name in zip file repeated: fooC.pkg
               this may be a result of using -j

You'll need to be mindful that files/packages in sub-directories do not have the same filenames otherwise you'll run into problems as shown by fooC.pkg. It's assumed this is not the case, since you are attempting to create a flat archive.

l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • Ah, I forgot to mention that I have tried that as well. And unfortunately I do face the same problem, because each of the packages contain a file with the same name, and the packages are traversed as well – I suppose due to the recursive option. However, I don't actually want it to traverse any further after it has found the `*.pkg` file. –  May 16 '18 at 20:16
  • You could use `maxdepth` with find perhaps. The zip tool should not traverse inside a .pkg file though, as I duplicated the same one and just used a different filename without issue. I’m not sure what type of package you are actually referring to, but if it’s just a directory then that’s likely the issue. – l'L'l May 16 '18 at 20:33
  • It's a `dSYM`, so basically just a container. I'll try with `maxdepth`. **EDIT**: `maxdepth` didn't change anything. –  May 16 '18 at 21:01
  • I eventually opted in to just run: `cd root/subroot/subsubroot/ && zip -r outputFile.zip *.dSYM && mv outputFile.zip ../../../ && cd ../../..` Ugly as ever, but at least it works. –  May 17 '18 at 07:27