Get specific file from .tar file within a Bat file - windows

I want to extract a specific file named WholeImage.jpg from a .tar file in Windows.
I got it working in one specific case, in which I specify the exact filelocation in the .tar file:
tar -xf %~1 --strip-components 5 InspectionProjectList/InspectionProject0718/InspectionProgram001/Sensing/Board0003/WholeImage.jpg
The --strip-components 5 is to get rid of the unnecessary folders while extracting the wanted file.
However the filepath for the WholeImage.jpg changes from tar file to tar file.
The parts that change are noted here witd a dollar sign:
InspectionProjectList/InspectionProject$$$$/InspectionProgram$$$/Sensing/Board$$$$/WholeImage.jpg

Related

Unzip only files that start with a certain word

I have a zip archive (let's call it archive) and let's say I want to go through some directories and finally extract ONLY the files that start with the word 'word'. Some thing similar to:
archive.zip/dir1/dir2/word***.csv
What is the command that could do this without having to extract the whole file (very big file)?
I tried this command line:
unzip -p archive.zip dir1/dir2/word***1.csv >destination
But this only extracts one file not all files that start with 'word'
You should do
unzip -p archive.zip dir1/dir2/word*1.csv >>destination.csv
The > truncates file destination.csv to zero length giving you the impression that only one file was unzipped, while >> creates the file if not present, otherwise appends to it which is the required behavior.
Reference : Check I/O redirection

How do I use grep command to search in .bz2.gz.bz2 file?

Basically I have .bz2.gz.bz2 file which on extraction gives a .bz2.gz file and on again extraction gives .bz2 file. In this .bz2 file, is my txt file which I want to search on using grep command. I have searched for this but I got bzgrep command which will only search in bz2 file and not the corresponding .gz.bz2 file and give me no results.
Is there a command in unix system which will recursively search in a zipped archive for zipped archive and return results only when it finds the txt file inside it?
P.S: the txt file may be deep in the archive to level 10 max. I want the command to recursively find the txt file and search for the required string. And there will be no other than an archive inside the archive until the txt file level.
I'm not sure I fully understand but maybe this will help:
for i in /path/to/each/*.tar.bz2; do
tar -xvjf "$i" -C /path/to/save/in
rm $i
done
extract all `tar.bz2` and save them in directory then remove the `.bz2`
Thnx for sharing your question.
There are a couple of strange things with it though:
It makes no sense to have a .bz2.gz.bz2 file, so have you created this file yourself? If so, I'd advise you to reconsider doing so in that manner.
Also, you mention there is a .bz2 that would apparently contain different archives, but a .bz2 can only contain one single file by design. So if it contains archives it is probably a .tar.bz2 file in which the tar-file holds the actual archives.
In answer to your question, why can't you write a simple shell script that will unpack your .bz2.gz.bz2 into a .bz2.gz and then into a .bz2 file and then execute your bzgrep command on that file?
I do not understand where it is exactly that you seem to get stuck..

tar extracted archive removes version

I have a zipped archive version 0.0.1: myarch_0.0.1.tar.gz
When I extract it with tar, everything is unzipped and extracted in a myarch folder, stripping the version number.
ls
myarch_0.0.1.tar.gz
tar -zxvf myarch_0.0.1.tar.gz
ls
myarch/ myarch_0.0.1.tar.gz*
I want the extracted folder to be named: myarch_0.0.1/
How do I keep my version number stuck to the extracted folder name?
The name of an archive file, and the name of the files inside, have nothing to do with each other in general. If you want extracted directories to have a certain name, with a version number, then you have to create the archive with so named directories.
In this example, the extracted content is a directory named myarch, instead of your desired myarch_0.0.1. You can rename the directory and recreate the archive:
mv myarch myarch_0.0.1
tar zcf myarch_0.0.1.tar.gz myarch_0.0.1
That's it. When you untar this new archive, you will get a directory named myarch_0.0.1, simply because that's what you put inside. Even if you rename this file to mickeymouse.tar.gz, when you untar it, you will still get a directory named myarch_0.0.1, simply because that's what's inside the archive. Nothing to do with the filename of the archive.

TAR'ing files in different directories

Never coded in Shell before, need to TAR 7 files, 5 are located in one directory, two are located in another. Sounds simple, right? Should be. I'm guessing that we assign variables to each file, then TAR them up. How does this syntax work?
tar -zcvf -T <file_name> abc.tar.gz
where <file_name> contains list of file to be included in .tar.gz

in shell script,how to get file name after untar the file

i am having tar file "test.tar.gz'.now i want to get the name "test' after untar the "test.tar.gz'. In shell script ,how to get this.
To strip an extension from a file name, use basename:
basename test.tar.gz .tar.gz
prints test
But that is just the base name of the archive. It's not always the name of any file or directory which the tar creates. The tar archive can contain any file names. If you need those, use tar tf to list the content of the archive.
Use something like:
file="test.tar.gz"
tarfilename=${file%.tar.gz}
read up about it man bash search for variable expansion.

Resources