How to get the list of files (ls command) from bz2 archive? - linux

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.

Related

How to untar `.tgz` directory, and gzip one of the extracted files in memory?

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"

GZip an entire directory

i used the following:
gzip -9 -c -r <some_directory> > directory.gz
how do i decompress this directory ?
I have tried
gunzip directory.gz
i am just left with a single file and not a directory structure.
As others have already mentioned, gzip is a file compression tool and not an archival tool. It cannot work with directories. When you run it with -r, it will find all files in a directory hierarchy and compress them, i.e. replacing path/to/file with path/to/file.gz. When you pass -c the gzip output is written to stdout instead of creating files. You have effectively created one big file which contains several gzip-compressed files.
Now, you could look for the gzip file header/magic number, which is 1f8b and then reconstruct your files manually.
The sensible thing to do now is to create backups (if you haven't already). Backups always help (especially with problems such as yours). Create a backup of your directory.gz file now. Then read on.
Fortunately, there's an easier way than manually reconstructing all files: using binwalk, a forensics utility which can be used to extract files from within other files. I tried it with a test file, which was created the same way as yours. Running binwalk -e file.gz will create a folder with all extracted files. It even manages to reconstruct the original file names. The hierarchy of the directories is probably lost. But at least you have your file contents and their names back. Good luck!
Remember: backups are essential.
(For completeness' sake: What you probably intended to run: tar czf directory.tar.gz directory and then tar xf directory.tar.gz)
gzip will compress 1+ files, though not meant to function like an archive utility. The posted cmd-line would yield N compressed file images concatenated to stdout, redirected to the named output file; unfortunately stuff like filenames and any dirs would not be recorded. A pair like this should work:
(create)
tar -czvf dir.tar.gz <some-dir>
(extract)
tar -xzvf dir.tar.gz

Viewing the files contained in a .gz archive using python

I am looking for a simple way to view the contents (file names) of a .gz archive. I saw that there has been a discussion already on how to do this when using .zip archive. So basically I am looking for the analogue of ZipFile.namelist(), but for .gz archives.
.gz files are not, in and of themselves, archives. A .gz file contains a single compressed file. When you decompress foo.bar.gz, you get the file foo.bar. So there's your one name, in the name of the .gz file itself.
You might have a .tar.gz file, which is a TAR (tape archive) file containing a representation of a directory of files, that is then compressed as a single file by gzip. In that case, you would need to use Python's tarfile to read the file and list the contents.

Use tar to archive the contents of your home directory in linux

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.

Linux - how to rename files within a .tgz file without extracting contents and applying tar again?

I have a tar file called test.tgz , inside it are the following files:
tool.foo
atest.you
btest.you
ctest.you
t.you
I want to rename the files inside test.tgz to be:
0.foo
0.you
1.you
2.you
3.you
Without the use of extracting the files and repacking them. How could I accomplish this?
Even though you can't rename the files in the tar archive, you can rename them with a sed expression on the fly while they are being extracted. The option to tar is--transform [sed-expression].
You do need to extract the files before you rename them. When files are in a tgz, they are protected from change.

Resources