What is a specific command for zipping and compressing multiple files with lzma2 on linux command line? - linux

Can you give an example for compressing files with lzma2?
I searched on Google but I didn't find any examples that I could understand.

You can also use the lzma binary or the xz with the --format=lzma argument.,
If you want to compress a set of files, I would recommend using tar to glue them together and then lzma, as for example:
$ tar --lzma -cf foo.tar.lzma file1 file2 ...
$ file foo.tar.lzma
foo.tar.lzma: LZMA compressed data, streamed

The XZ Utils package is installed in most Linux distributions and is becoming the de facto standard compression format. The tool compresses with LZMA2. Usage is as simple as it gets,
$ xz <file>
To produce file.xz.

This worked:
sudo tar --lzma -cf newname.tar.lzma file1 file2 file3
Replace the newname with the new name you want to give to the file
and replace file1 file2 file3... with the names of the files you want to zip and compress.

Related

Preserve timestamp when compressing files with lz4 on linux

Is there a way to preserve date and time when I compress a file in linux with lz4? The command line looks like this:
jens#xyz $ lz4 file file.lz4
With e.g. gzip the date and time of the compressed file is the same as of the original file. After the command above I could do
jens#xyz $ touch -r file file.lz4
jens#xyz $ rm file
But that seems a little complicated.
Prefer combining lz4 with tar if you wish to preserve file properties :
compress$ tar cf - file | ./lz4 > file.tar.lz4
decompress$ ./lz4 -d file.tar.lz4 | tar xv

print content of more than one file in a zip archive

I have some zip files that are really large and I want to print them without extracting first. I am using zcat and zless to do that and then I redirect the output to a different application. When my zip file contains more than one text file I receive the following error:
zcat tweets.zip >a
gzip: tweets.zip has more than one entry--rest ignored
How can I do what I want with zip files that contain more than one text file?
You can do this to output a file without extracting:
$ unzip -p <zip_file> <file_to_print>
For example:
$ unzip -p MyEar.ear META-INF/MANIFEST.MF
As cur4so mentioned you can also list all files using:
$ unzip -l <zip_file>
Use the -p option of unzip to pipe the output. Multiple files are concatenated. The -c option does the same thing, but includes the file name in front of each file.
If you just want to see a list of files in your zip archive use:
unzip -l tweets.zip
if you want to extract just some file:
unzip tweets.zip file-of-interest-as-it-is-pointed-in-the-archive
if you want something else, could you clarify your question?

How to extract first few lines from a csv file inside a tar file without extracting it in linux?

I have a tar file which has lot of csv files in it.
How to get the first few lines of each csv file without extracting it?
I tried:
$(tar -Oxf $tarfile $file | head -n "$NL") >> cdn.log
But got error saying:
time(http:index: command not found
This is some line in one of the csv files. Similar errors are reported for all csv files...
Any idea??
Using -O you can tell tar to extract a file to standard output instead of to file. So you should be able to first use tar tf <YOUR_FILE> to list the files from archive and filter it using grep to find the CSV files, and then for each file use tar xf <YOUR_FILE> <NAME_OF_CSV> -O | head to get the file's beginning to stdout. This may be a bit ineffective since you unpack the archive as many tiems as there are CSV files, but should work.
You can use perl and its Archive::Tar module. Here a one-liner that extract the first two lines of each one:
perl -MArchive::Tar -E '
for (Archive::Tar->new(shift)->get_files) {
say (join qq|\n|, (split /\n/, $_->get_content, 3)[0..1])
}
' file.tar
It assumes that the tar file only has text files and they are csv. Otherwise you will have to grep the list to filter those you want.

Combine files in one

Currently I am in this directory-
/data/real/test
When I do ls -lt at the command prompt. I get like below something-
REALTIME_235000.dat.gz
REALTIME_234800.dat.gz
REALTIME_234600.dat.gz
REALTIME_234400.dat.gz
REALTIME_234200.dat.gz
How can I consolidate the above five dat.gz files into one dat.gz file in Unix without any data loss. I am new to Unix and I am not sure on this. Can anyone help me on this?
Update:-
I am not sure which is the best way whether I should unzip each of the five file then combine into one? Or
combine all those five dat.gz into one dat.gz?
If it's OK to concatenate files content in random order, then following command will do the trick:
zcat REALTIME*.dat.gz | gzip > out.dat.gz
Update
This should solve order problem:
zcat $(ls -t REALTIME*.dat.gz) | gzip > out.dat.gz
What do you want to happen when you gunzip the result? If you want the five files to reappear, then you need to use something other than the gzip (.gz) format. You would need to either use tar (.tar.gz) or zip (.zip).
If you want the result of the gunzip to be the concatenation of the gunzip of the original files, then you can simply cat (not zcat or gzcat) the files together. gunzip will then decompress them to a single file.
cat [files in whatever order you like] > combined.gz
Then:
gunzip combined.gz
will produce an output that is the concatenation of the gunzip of the original files.
The suggestion to decompress them all and then recompress them as one stream is completely unnecessary.

zcat files in and not in gzip format

I have all my Apache access log files as access.log, access.log.1 access.log.1.gz etc... What I want is to zcat all files in and not in gzip format and pipe them into an X program.
I know I can do: zcat /var/log/apache2/access.log.*.gz | someapp... but that will just work for *.gz and not the first two logs.
Any ideas will be appreciate it
use zcat -f, it will copy uncompressed files as is
For the specific use case of HTTP log server files, consider the zmergelog command (from the mergelog package). It additionally sorts the result of the merge chronologically.

Resources