I have a large tar archive with many xml files in it. a couple of xml files in this archive are corrupt. How can I extract the good files without the program exiting?
There is a tar file within the gz
tar zxf myFile.gz
tar: Unexpected EOF in archive
tar: rmtlseek not stopped at a record boundary
tar: Error is not recoverable: exiting now
It looks, from the filename, as though you're trying to unpack something that isn't a tar archive. Usually a tar file would have a .tar extension, and if it had been then compressed with gzip, it would be .tar.gz or .tgz.
The command you're running, with the z option to tar, tries to undo gzip compression first, and then untar the resulting archive. But from the .gz extension, it rather looks as though you've got a gzipped file rather than a gzipped tar archive.
The best thing to do is to examine the file to find out what sort of file it is:
file myFile.gz
That will tell you whether it's gzipped or whatever. If it's gzipped, then run
gunzip myFile.gz
That will leave you with myFile without the extension; you can then use
file myFile
to probe it to determine whether it's a tar archive or something else.
mv myFile.gz myFile.tar.gz
gunzip myfile.tar.gz
tar xf myFile.tar
You might try bzip2recover to try recover the bondaries first!
bzip2recover file.bzip
eg from manual:
In case of damage to one member of a .gz file, other members can still be recovered (if the
file: Corrupt input. Use zcat to recover some data.
usage: zcat file > recover
Related
TL;DR
How can I untar a file .tgz, and then selectively gzip the output?
My extracted dir has a few text files and a .nii file. I'd like to gzip the later.
More details
First method would be to just do sequentially. However I'm dealing with a huge dataset (10k+ tar archives) stored on a BeeGFS file system and I was told it would be better to do it in memory instead in two steps, since BeeGFS doesn't like handling big directories like this.
Sequential method:
for tarfile in ${rootdir}/*.tgz; do
tarpath="${tarfile%.tgz}"
tar zxvf ${tarfile} # (1) untar directory
gzip ${tarpath}/*.nii # (2) gzip the .nii file
done
Is there a way to combine (1) and (2)? Or do you have any other tips on how to do this process effectively?
Thanks!
You can extract a single file from the archive (If you know the filename), and have tar write it to standard output instead of to a file with -O, and then compress that stream and redirect it to a file. Something like
tar xzOf "$tarfile" "$tarpath/foo.nii" | gzip -c > "$tarpath/foo.nii.gz"
You can then extract everything else in the archive with tar xzf "$tarfile" --exclude "*.nii"
What is the Unix bash command to get the list of files (like ls) from archive file of type .bz2 (without unzipping the archive)?
First bzip2, gzip, etc compress only one file. So probably you have compressed tar file. To list the files you need command like:
tar tjvf file.bz2
This command uncompress the archive and test the content of tar.
Note that bzip2 compresses each file, and a simple .bz2 file always contains a single file of the same name with the ".bz2" part stripped off. When using bzip2 to compress a file, there is no option to specify a different name, the original name is used and .bz2 appended. So there are no files, only 1 file. If that file is a tar archive, it can contain many files, and the whole contents of the .tar.bz2 file can be listed with "tar tf file.tar.bz2" without unpacking the archive.
I am trying to archive the contents of my home directory using tar and then compress the tar file with gzip. I know you can uncompress and unarchive the .tar.gz file using cat, tar and gzip. But , I don't know how to compress and archive.
Hey there here is a link for your question. a full guide
https://www.howtogeek.com/248780/how-to-compress-and-extract-files-using-the-tar-command-on-linux/
tar -czvf name-of-archive.tar.gz /path/to/directory-or-file
Here’s what those switches actually mean:
-c: Create an archive.
-z: Compress the archive with gzip.
-v: Display progress in the terminal while creating the archive, also known as “verbose” mode. The v is always optional in these commands, but it’s helpful.
-f: Allows you to specify the filename of the archive.
Is there any possible way to compress a directory with GZip, BZip, BZip2, xz format. I'm building a command line tool(using bash) which I need these options to be included.
A command like
tar czf output.tar.gz yourdir/
should work.
c means that tar will create an archive
z means that the output will be compressed (using gzip)
the output filename is after f
at the end, you can specify any number of directories/files (space-separated)
To answer the "why" part of your question, it is because of the Unix philosophy of having many small tools that do their job well that you can string together, as opposed to one big tool that doesn't do anything well and is hard to make better. Your examples are a perfect illustration of this philosophy, where you have several compression tools to choose from, and it is easy to add a new compression tool to your tool box. The archiving part, turning a directory of files into a byte stream, is a different task that is its own tool that can be combined with any of those or any future compression tools.
The body of your question then asks "how". You use a pipe with tar, cpio, or pax. tar is the most common. You then name the file accordingly so the consumer of the file can tell what it is from the name. E.g. ending with .tar.gz. Like this:
tar cf - somedirectory | gzip > somedirectory.tar.gz
or
tar cf - somedirectory | xz > somedirectory.tar.xz
These tar up the directory into a byte stream, which is then piped to a compressor. The output of the compressor is then written to the file containing the compressed directory contents.
To decompress:
gzip -dc somedirectory.tar.gz | tar xf -
Here it is done in the reverse order to first decompress the file and feed the output of that to tar to extract the files and recreate the directory structure. The - means to put the archive to stdout or get the archive from stdin.
Having said all that stuff about how much better it is to have small tools that do their job well, this application of tar is so incredibly common that it is built into the tar options. So you can instead:
tar czf somedirectory.tar.gz somedirectory
tar cJf somedirectory.tar.xz somedirectory
tar will run the gzip or xz executables and pipe the data through them itself.
(J is a recent gnutar addition, so your tar may not have it.)
I was in needed of backing up a .img file, basically what I had to do was to compress the file and copy it to another location. On the begining this file was a +800GB file, so I ran this command:
tar -cvf file.img file.tar
Of course that I didn't see the problem of the command till this was promped
tar: fle.tar: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
Now I ran the command properly after that error
tar -cvf file.tar file.img
And this time succeeded... The problem came when I realized the original +800GB file was now a 12KB file.
I don't know If I damaged the file or if it was compressed, If so, how do I get back the original size of the file?
I am using a linux SLES 11
When you ran
tar -cvf file.img file.tar
you overwrote file.img, creating a tar file with no contents, even though the tar commmand appeared to fail. So when you swapped the parameters around, your large image file was gone. Sorry but I think you've lost the file.
"man tar" will help you
-c = create new tar file
-x = extract tar file
and compression options are -z -Z -j (-z -Z -y on BSD & MAC)