Are you getting tar'd yet?

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,525
Reaction score
3,287
Credits
31,524

Using tar in Linux​

The tar command in Linux is used to create, maintain, modify, and extract files from a tar archive. While Linux doesn't require filename extensions, it's a common convention to use the .tar suffix for tar files for convenience. Compressed tar archives usually end in a .tar.gz, .tar.bz2, or .tar.xz suffix to make it easier to know how the file is compressed.

Creating a Tar File​

Example 1: Creating a Tar File with a Couple of Files in the Same Directory​

To create a tar file with just a couple of files in the same directory, use the following command:
Code:
 tar -cvf archive.tar file1.txt file2.txt
This command creates a tar file named archive.tar containing file1.txt and file2.txt.

Example 2: Using Wildcards to Archive a Specific File Type​

To archive all files of a specific type (e.g., .txt files) in a directory, use:
Code:
 tar -cvf textfiles.tar *.txt
This command creates a tar file named textfiles.tar containing all .txt files in the current directory.

Example 3: Including Everything Beneath a Directory​

To include everything beneath a directory (e.g., mydir), use:
Code:
 tar -cvf mydir.tar mydir/
This command creates a tar file named mydir.tar containing all files and subdirectories within mydir.

Extracting a Tar File Archive​

To extract a tar file archive, use the following command:
Code:
 tar -xvf archive.tar
This command extracts the contents of archive.tar into the current directory.

Handling Soft Links and Hard Links​

Soft links (symbolic links) and hard links can cause problems when creating tar files. Soft links point to the original file, and if the original file is moved or deleted, the link becomes broken. Hard links, on the other hand, point directly to the data on the disk, and multiple hard links to the same file share the same data blocks.

When creating a tar archive, you can use the -h option to follow symbolic links and archive the files they point to:
Code:
 tar -cvhf archive.tar symlink

Reasons to Create a Tar File​

  • Backup: Tar files are commonly used for backing up data.
  • Distribution: Tar files can be used to distribute multiple files as a single package.
  • Compression: Tar files can be compressed to save space.
  • Historical Context: In the early days of Linux, before the advent of .deb and .rpm packages, tar.gz files were the common way to install certain programs and applications.

Compressing and Decompressing Tar Files​

Compression Methods​

  • gzip: Compresses faster but may not achieve the smallest file size.
  • bzip2: Compresses slower but achieves a smaller file size.
  • xz: Compresses even slower but achieves the smallest file size.

Example Commands​

To create a compressed tar file using gzip:
Code:
 tar -cvzf archive.tar.gz mydir/

To extract a gzip-compressed tar file:
Code:
 tar -xvzf archive.tar.gz

To create a compressed tar file using bzip2:
Code:
 tar -cvjf archive.tar.bz2 mydir/

To extract a bzip2-compressed tar file:
Code:
 tar -xvjf archive.tar.bz2

To create a compressed tar file using xz:
Code:
 tar -cvJf archive.tar.xz mydir/

To extract an xz-compressed tar file:
Code:
 tar -xvJf archive.tar.xz

Compression Efficiency​

Text files usually compress very well because they contain repetitive patterns. Binary files, such as MP3s and JPEGs, typically do not compress as well because they are already in a compressed format.

Conclusion​

Using tar in Linux is a powerful way to manage file archives. Whether you're backing up data, distributing files, or saving space through compression, tar provides a versatile solution. Remember to consider the type of files you're compressing and choose the appropriate compression method for your needs. Compressed tar archives usually end in a .tar.gz, .tar.bz2, or .tar.xz suffix to make it easier to know how the file is compressed. In the early days of Linux, tar.gz files were the common way to install certain programs and applications before the advent of .deb and .rpm packages.
 


Never liked TAR, and that is because:
  • compression is not a standard feature
  • it lacks compatibility with Windows
 
It's weird in one way, but the reality is that to control Unix and Linux machines, there is only one Operating System that is being used by corporate standards, and that is Windows. I'm West-Europe based, it's not UK.
 
I've always used tar files reason being is that they run and update without installing them.
 
I run PorteuX and make modules that require the entire directory structure from / down to the file or folder.

Part of the command used includes tar.

I used to make the directory structure manually until I tried this part in my context menu .desktop file:

[Desktop Action 6]

Name=ROOT-DIR-FROM-HERE-2-ONE-DIR-UP

Exec=file=%f; tar cf %f.tgz %f && tar -xf %f.tgz && rm %f.tgz


It makes the process much easier.

Vektor
 
Never liked TAR, and that is because:
  • compression is not a standard feature
It's not meant to provide compression as-is. The point of a tarball is just to put everything in a single file. From there you can opt to compress it, if compression gains you anything. Your videos and photos will hardly be dented even with xz set to -7 and up because your phone/camera has high efficiency codecs. And sometimes you don't need/want them compressed, just organised.

This is more historic, but: in ye olde days, copying many files across disks or a network was much slower than a single file (within reason, there is an X-axis peak). Today we have features like delta copying, much higher concurrency (CPU and storage), more bandwidth, etc. so tar'ind something is not as gainful as it once was in that arena, but a a whole, it's still pretty useful and for a large number of files, it still speeds things up.

I know some software offer zero-compression, by with tar it is the default, expected behaviour, which is good.

(So far as Windows compatibility, Windows has apps (mentioned) and there's WSL, and IIRC BusyBox was ported to natively run on Windows.)

At the end of the day, it's a personal choice. Not much point saying this is better than that because it's situation-dependent.
 
At the end of the day, it's a personal choice. Not much point saying this is better than that because it's situation-dependent.
Amen.
 
It's not meant to provide compression as-is. The point of a tarball is just to put everything in a single file. From there you can opt to compress it, if compression gains you anything. Your videos and photos will hardly be dented even with xz set to -7 and up because your phone/camera has high efficiency codecs. And sometimes you don't need/want them compressed, just organised.

This is more historic, but: in ye olde days, copying many files across disks or a network was much slower than a single file (within reason, there is an X-axis peak). Today we have features like delta copying, much higher concurrency (CPU and storage), more bandwidth, etc. so tar'ind something is not as gainful as it once was in that arena, but a a whole, it's still pretty useful and for a large number of files, it still speeds things up.

I know some software offer zero-compression, by with tar it is the default, expected behaviour, which is good.

(So far as Windows compatibility, Windows has apps (mentioned) and there's WSL, and IIRC BusyBox was ported to natively run on Windows.)

At the end of the day, it's a personal choice. Not much point saying this is better than that because it's situation-dependent.

Fact of the matter is that compression will gain space in 99% of all such files created, the 1% you talk about does exist but it's a shame they made that the default choice. In Zip (Info-Zip) the default is to compress and option to not compress.
 
Fact of the matter is that compression will gain space in 99% of all such files created, the 1% you talk about does exist but it's a shame they made that the default choice. In Zip (Info-Zip) the default is to compress and option to not compress.
I hear your frustration, and nobody is trying to make you us tar, I'm just pointing out that tar is not a file compression utility so it should not compress things by default. As I said:
It's not meant to provide compression as-is. The point of a tarball is just to put everything in a single file.

...In the same way an image viewer should not crop your pictures by default. Both can be extended to provide extra features, but ultimately that is not the function of said tool. With the tools you mention, those are file compression software, that is their intended function, so they should compress by default.


PS: I do have to correct what you said about files: you won't gain space with "99%" of all files (and "gains" should be reasonably interpreted)...
Almost any video file with a reasonable codec (such as h265/x265/HEVC), with any halfway decent compression setting, will get you either no compression (<=1%) or you will end up with a larger file size (>=1%) when you try to compress it with a file compression tool as it is already compressed by the media encoder...
If you meant "99% file-types" that's a grey area because of how one defines a file-type (vs format, vs functions, etc.) and the total file-types that are on average, too small to compress (smaller than the FS block size or smaller than the dictionary, etc.)
 
tar is not a file compression utility

I probably should have done compression as a separate article, as it really has very little to do with tar. I included it because many times tar files are compressed, and sometimes people want to compress their archives.
 
Last edited:


Members online


Top