split file to specific sizes with tar command - linux

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 -

Related

Extract 3 smallest files from Tar archive in descending order by size [duplicate]

This question already has an answer here:
Extract top 10 biggest files from tar archive Linux
(1 answer)
Closed 3 years ago.
How can I extract from a Tar file in Linux the 3 smallest files in descending order using command line?
You can list the file details, sort by size, pick the top3 files, build the tar x command, and execute to extract the 3 files:
tar tvf foo.tar
|awk '$0=$3"\x99"$NF'
|sort -n
|awk -F'\x99' 'NR<4{s=s" "$2}END{print "tar xvf foo.tar "s}'
|sh
Note:
The above one-liner assumes that all filenames in the tarball don't contain spaces or other special characters
The tarball name foo.tar is hardcoded., you should replace it by your real tarball
you can test the cmd without the last pipe: |sh it will only output the generated tar -x command, if it is fine, you can pipe to |sh to do real extraction.

Join splitted files and uncompress them same command [LINUX]

I ran a command to split a file of 2TB into files of 50GB each.
...| split -b 50G
Now, I need to join and uncompress them, on the same command. Because I don't have enough free disk space to have them both.
To join: cat part-* > big_file.gz
To uncompress: tar -xvf big_file.gz
How could I run those commands above on the same line?
To run several commands in bash use ; as a separator, like:
cat part-* > big_file.gz;tar -xvf big_file.gz
you can also combine them with some bash loop commands like while, for, etc

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

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.

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.

Resources