View several lines from a file in a tar.gz file - bash

I have a very large .tar file that contains several .gz files. I would like to view a few lines in any of the individual files without untarring. I can view the files using:
tar -tzf TarFile # doesn't actually end in .tar
I get:
TarFile/
FileA.gz
FileB.gz
FileC.gz
FileD.gz
I would like to view just a few lines from any of the individual files. Normally I would use:
zless MyFile
Is there a way to combine the two commands so I can view a few lines from any of the individual files?

tar -xOf TarFile FileB.gz | zless
Explanation:
tar
-x
-O extract to standard output
-f Tarfile
FileB.gz the file in the tar archive to extract
| zless pipe the extracted file data to zless
This will be expensive to do more than once as it requires tar to scan the archive each time you run the command. If the tar archive is large (and the file you want is early in the tarball) you might also benefit from using --occurrence=1 on that command line to get tar to stop processing the tar file immediately when it finds a file that matches the file you told it to extract.

Related

How to tail the last nth lines of a file that is located in a compressed tar file

Ubuntu 16.04
Bash 4.4.20
I am wanting to tail the last 30 lines of a SQL file located in a tar.gz file. I noticed that the SQL backups were failing due to a locked file. Instead of me untarring the entire compressed file, I'd like to simply tail the SQL backup and then grep for certain phrases.
This StackOverflown Thread explains that this is not possible with zipped files. Does this also apply to tar files that are created like so:
tar czf name.tar.gz foldername
If this has been explained in another thread, please delete this thread and I'll keep searching.
You can use tar's -O option to print content of file then using | (pipe), you can apply basic unix operations, like tail:
tar zxvf name.tar.gz <file_to_read> -O | tail
Sample command, to read last two lines of a file A1.txt
tar zxvf B.tar.gz B/A1.txt -O | tail -2
Here B is a directory on which I have ran tar command, it contains many files along with file A1.txt

Extracting few files without scanning whole tar.gz

I would like to extract few files from one directory in tar.gz. The problem is that in the location I find a lot of files and the process of unpacking the whole folder is inefficient. I tried to extract them individually, but before extracting a particular file it scans the entire package. Is there a way to skip a scan and extract directly?
script for extracting a single file one by one:
filesList=( "file1" "file2" "file3" )
filesPath="path/to/files/inside/targz/"
for i in "${filesList[#]}"
do
tar -xvf compressed.tar.gz $filesPath$i
done
To extract a specific file from tar you can use:
tar -zxvf <tar_name> <file_to_untar>
Not sure to unserdstand what is the scan (maybe in another part of the script). The for loop can be avoided and tar called once.
tar -xvf compressed.tar.gz "${filesList[#]/#/$filesPath}"
or process the list first
filesList=( "file1" "file2" "file3" )
filesPath="path/to/files/inside/targz/"
filesPathList=()
for i in "${filesList[#]}"
do
filesPathList+=("$filesPath$i")
done
tar -xvf compressed.tar.gz "${filesPathList[#]}"
I've just seen it's a tar.gz, so inflated (gzip -cd or zcat) inside the loop. Which explain the poor performance, another option can be to gunzip first.

combine multiple gzip compressed files in a tar file

I have a really large tar file containing many gzip files. I would like to combine all the gzip files into one gzip file and place in another directory. To view the contents of the tar file I use:
tar -zxvf Big.tar
which gives:
Big/FileA.gz
Big/FileB.gz
Big/FileC.gz
Big/FileD.gz
To normally combine multiple gzip files I would move to that directory and use:
cat FileA.gz FileB.gz FileC.gz FileD.gz > BigFile.gz
However the BigFile.gz would remain in that directory. I'm just not sure how to do this within a tar file and how to have the combined file be written to another directory.
With GNU tar, you can do:
tar -Oxf Big.tar --wildcards 'Big/*.gz' > /tmp/your_file.gz
With OS X tar, you have to list the files individually:
tar -Oxf Big.tar Big/FileA.gz Big/FileB.gz > /tmp/your_file.gz
The salient feature in both is -O, which writes the files to stdout.
Here's an example transcript on a GNU system:
$ pwd
/home/me
$ tar tf Big.tar
Big/
Big/foo.txt.gz
Big/bar.txt.gz
$ tar -Oxf Big.tar --wildcards 'Big/*.gz' > /tmp/your_file.gz
$ zcat /tmp/your_file.gz
This is the contents of foo.txt
This is the contents of bar.txt
Using tar and assuming folder Big has FileA.gz, FileB.gz, etc.:
tar -czv -f /path/to/final/BigFile.gz -C Big .

HP-UX - How can I read a text file from tar archive without extracting it?

I have a tar archive which contains several text files. I would like to write a script to display (stdout) the content of a file without extracting it to the current directory.
Actually I would like to do the same as:
tar tf myArchive.tar folder/someFile.txt
cat folder/someFile.txt
rm -R folder
but without the rm...
I tried this way but it didn't work:
tar tf myArchive.tar folder/someFile.txt | cat
Thanks
Use x to extract, with f from archive file. Then add also option -O to direct extracted files to standard output.
tar xf myArchive.tar folder/someFile.txt -O

Automated unzipping of files

I have a folder full of zipped files (about 200). I would like to transform this into a folder consisting only of unzipped files. What would be the easiest and quickest way to do this?
Please note that I would like to remove the zipped file from the folder once it us unzipped.
Also, I'm on a Mac.
Thanks!
You can do something like:
for file in `ls *.zip`; do unzip -f $file; rm $file; done
We are looping through all the zip files in the directory, unzipping it and then deleting it.
Note that the -f option of zip will overwrite any file without prompting if it finds a duplicate.
You need to run the above one-line command on the command line from the directory that has the all the zip files. That one line is equivalent to:
for file in `ls *.zip` # ls *.zip gets the list of all zip file..iterate through that list one by one.
do # for each file in the list do the following:
unzip -f $file # unzip the file.
rm $file # delete it.
done
I found this answer which is a simple one liner to gunzip all .gz compressed files within a folder.
Basically you cd to the folder and then run
gunzip *.gz
If you want to only unzip files with a certain prefix you put that before the *
gunzip example*.gz
Easy as cake!

Resources