Preserve timestamp when compressing files with lz4 on linux - 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

Related

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

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.

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.

split file to specific sizes with tar command

There is an iso file with 5.5 GB in size. I want to split that with tar command each part should be 500M. What is the command for that?
It's not the job of tar to split files. Use split to split the tar file.
You can use split:
tar [your params] |split -b 500m - output_prefix
...Or use zip (or any other compressor):
tar [your params] | zip -s 500m output.zip -

Linux: Adding named files to a zip archive, from a pipe

Is it possible to use something like:
command.exe | zip >> archive.zip
command2.exe | zip >> archive.zip
...and end up with two named files inside one zip archive.
This way, if at all possible, would be neater than having temp files.
Create two named pipes in a new dir (with mkfifo), pipe the output of the commands to these two pipes and then zip the dir.
mkdir tmp
mkfifo tmp/1.out
mkfifo tmp/2.out
command1.exe > tmp/1.out
command2.exe > tmp/2.out
zip -FI -r tmp.zip tmp/
EDIT: Added the FI flag to zip, which does make this possible. The only caveat is that you need zip 3.0 for this to work. Tar:ing FIFO:s is not implemented (according to tar devs) because you need the file size in advance in order to write it to the TAR header.
Use fuse, fuze-zip rather.

Resources