~/shell-script/38-shell-script-extract-single-file-bunch-jar-files.md

Shell script to extract single file from a bunch of jar files

1 min readNovember 24, 2017by Gorav Singal#unzip-script#scripting
// TL;DR

Use a shell script with unzip command and a for loop to extract a specific file from each jar in a directory, organizing output into per-jar folders.

Shell script to extract single file from a bunch of jar files

Problem Statement

I have a bunch of jar files lying in a folder. Lets name it /Users/labuser/jars

Each jar file has a manifest file in META_INF/MANIFEST.MF
I have to read each MANIFEST.MF file and get some information from it. So, instead of extracting whole jar, one by one is a tedius thing.

What I want

I want to extract that file in a folder whose name can be as of jar file.

Unzip command to extract a jar file

Following command can be used to unzip a complete jar file:

unzip <path of jar file>

If you want to unzip files to a particular folder

unzip <path of jar file> -d <path of target folder>

Note: if the target folder does not exists, it will be created

If you want to unzip only particular files

unzip <path of jar file> <path of file inside jar file>

If you want to unzip only particular files in a particular folder

unzip <path of jar file> <path of file inside jar file> -d <target folder>

Conclusion

That is all. Combine these, and you are done.

Shell script I used to solve this

for myfile in `ls /Users/labuser/jars` ; do unzip ../$myfile META-INF/MANIFEST.MF -d $myfile; done

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Curl - Your friend for Rest APIs/Calls - Basic Commands

Curl - Your friend for Rest APIs/Calls - Basic Commands

Curl is a wonderful tool for initiate REST APIs or calls. Or, you can literally…

Drupal Helpful codes for database queries

Drupal Helpful codes for database queries

Being a drupal user from last around 5 years, I used to know small codes for…

Docker image for Drupal 7, and Php extension MongoDB installed.

Docker image for Drupal 7, and Php extension MongoDB installed.

You have drupal 7 image from docker hub, and want to connect tomongo db via php…

Docker Push&#58; How to push your docker image to your organization in hub.docker.com

Docker Push&#58; How to push your docker image to your organization in hub.docker.com

Tag the image, by seeing its image id, from docker images command docker tag 04d…

How to get Youtube Video Thumbnail Images

How to get Youtube Video Thumbnail Images

If your youtube video looks like:https://www.youtube.com/watch?v=g0kFl7sBdDQ…

Jira Rest APIs common usages

Jira Rest APIs common usages

This article shows some of common usages of JIRA rest apis. Note: This article…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…