0

I am unzipping the .zip file. I'm able to do so but my requirement to extract only the parent folder data ignoring any sub directory and their contents.

Below is the name format of zip file.

filename.csv.zip

commands I have used unzip -j /zip_filepath "*" -d /final_directory.

I tried with above commands but above commands extracting all the complete zip contents which are the part of zip file.

Please find below the file structure of zip file.

  • -rw-r--r-- abc.csv
  • drwxrwxr_x __MACOSX/
  • -rw-r--r-- __MACOSX/_abc.csv

Second zip file structure: *-rw-r--r-- xyz.csv

I want to extract only abc.csv and xyz.csv files from the zip files.


Output of: unzip -Z -1 abc.zip

__MACOSX/
__MACOSX/_abc.csv
abc.csv

Output:unzip -Z -1 /user/sam/abc.zip -d /user/sam/2019-05-22/

caution: not extracting -d ignored

    abc.csv
    __MACOSX
    __MACOSX/._abc.csv

The output here should be abc.csv file only

Output of: unzip -Z -1 xyz.zip

xyz.csv

Output:unzip -Z -1 /user/sam/xyz.zip -d /user/sam/2019-05-22/

caution: not extracting -d ignored
xyz.csv
  • I've edited your question, cause if I understood you correctly, the solution I've gave to you, works for both cases... Check if the output from the commands I added after the dividing line in your question, matches the output you get in your system for your files... If not, make the proper edits, and paste the exact output from those commands in your question, and then check that is rendering properly, so we know that we are in the same page. – matsib.dev May 22 at 11:22
  • For now, my edit is pending for review: This edit will be visible only to you until it is peer reviewed... If you don't see it, wait for a while, and check again later. – matsib.dev May 22 at 11:24
  • @matsib.dev, I have edited my question with the result of above commands. The above commands are not able to extract anything in both the files – sam May 22 at 12:33
  • the commands I've added to your question ( unzip -Z -1 abc.zip and unzip -Z -1 xyz.zip ) are just for listing, not for extracting. The idea of those command, is that can give us a way to know that we're talking about the same directory structure... When using those commands, remove the -d /user/sam/2019-05-22/... on the other hand, the command for extraction is the one on my answer: unzip filename.csv.zip -x '*/*' -d /final_directory, and I think is the correct command for both situations, if the structure is what you post. If is not working... Are you getting some specific error? – matsib.dev May 22 at 12:51
  • double check that the rendered question is perfectly matching your terminal output, and,... if the structure its the same, but the command I'v gave you does not work, explain yourself in the answer... what is wrong? what is exactly happening when you are not getting your expected result? Are there any errors? – matsib.dev May 22 at 12:54
2

You can use -x option, with '*/*' as argument, like this:

unzip filename.csv.zip -x '*/*' -d /final_directory

If you want to suppress the Warning caution: excluded filename not matched: */* for cases like the one with your xyz.zip file, you can use:

unzip filename.csv.zip -x '*/*' -d /final_directory 2> /dev/null

Note: bare in mind that this will suppress all of stderr output. It would be ideal to instruct unzip command to not show that kind of warning (only that kind),... I took a quick look at its manual, and I did not see any option for that. So, in your case, if is not possible to instruct unzip to just suppress that kind of warning, I think is better to suppress nothing at all, cause that warning it's making no harm, but if you discard all errors, you could miss some important information.

  • Thank you it's working for the current scenario but it's failing when I'm unzipping only one level directory. I was looking some generic command that's will work in both case 2 level directory and 1 level directory. – sam May 22 at 6:15
  • What do you mean by 'unzipping only one level directory'? Please update your question, and add what is your goal on each case, giving examples of both zip internal directory structure, and what you want to extract, exactly, in both cases. – matsib.dev May 22 at 8:04
  • I have updated my question. I want a generic command that work for both the zip files. – sam May 22 at 10:32

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.