Joe Cole Joe Cole - 1 year ago 99
Linux Question

Zip Junk Paths Option Not Working

I have a bit of an odd one that I can't seem to figure out.

Here's the setup. I am downloading a bunch of files given to a script via array. The files are not necessarily in the same folder, so I don't think just using cd to change directory and then add the files I want is going to work.

I have added -j to my shell command in order to not include the directories, and it worked... kinda. Now I have all the files in the top level of the zip, but it's also included the directories and duplicate versions of the files in the appropriate directories.

Any thoughts?

Edit: code added.

I have an array of files like this:

Other images
Other folder\Another subfolder\Why so many folders\

So what I'd like is to have all the images in the root of the zip, but what I'm getting is both directories and junked directories in one zip:

--Other images
--Other folder
--Another subfolder
--Why so many folders

My code is quite simple, I'm using -j to strip out the directory and passing an array of files to zip. If I remove the -j, it only includes a single copy of the files, although in the folders, so I know the issue is not my array doubling up on the files.

zip -jq zip array_of_files

Solved. Kind of.

For some reason when I placed the -j at the end of the command line, it works as expected. Unsure why that would matter, but it fixed it. Here's what worked.

zip zipname array_of_files -jq

Answer Source

You can use a combination of find and zip together. Run the above command at the same level as the individual folders.

find . -type f -name "*.jpg" -print | zip -jq -@

The find command lists the jpg files from he current path and with the -@ option in zip is to read from stdin which the previous command produces. And the -jq flag for junk paths.

With the above command, I was able to achieve the structure as you intended. Use unzip -l to list the files under the archive without extraction.

$ unzip -l
  Length     Date   Time    Name
 --------    ----   ----    ----
        0  11-26-16 13:08   image1.jpg
        0  11-26-16 13:09   image2.jpg
        0  11-26-16 13:09   image3.jpg
        0  11-26-16 13:09   image4.jpg
 --------                   -------
        0                   4 files

The above simulation of mine is for your original input, when you had 4 images in total.