In our day-to-day life, we often come across archived files on various platforms, whether it’s Windows, Mac, or Linux. Several applications are available on all these platforms to create and extract archived files. When it comes to working on the Linux platform, dealing with archived files is a frequent task.
In this article, we will explore archive tools available on standard Linux distributions, along with their features, usage, and examples.
The article is divided into two parts, with each part covering five command-line archive tools (a total of 10 best command-line archive tools).
1. tar Command
tar is the standard UNIX/Linux archiving tool, which was initially designed as a Tape Archiving Program, it has evolved into a general-purpose archiving utility that can handle archive files of all kinds.
tar supports various archiving filters with multiple options.
-A
: Append tar files to existing archives.-c
: Create a new archive file.-d
: Compare the archive with the specified filesystem.-j
: Compress using bzip.-r
: Append files to existing archives.-t
: List contents of existing archives.-u
: Update an existing archive.-x
: Extract files from an existing archive.-z
: Compress using gzip.--delete
: Delete files from an existing archive.
Create a tar archive file:
tar -zcvf name_of_tar.tar.gz /path/to/folder
Extract a tar archive file:
tar -zxvf name_of_tar_file.tar.gz
For more detailed examples, read Tar Command Examples in Linux.
2. shar Command
shar, short for Shell Archive, is a shell script that, when executed, creates the original files. It is a self-extracting archive format that is now considered a legacy tool and requires the Unix Bourne Shell to extract the files.
shar has the advantage of being in plain text format, but it can be potentially dangerous since it outputs an executable.
shar Options:
-o
: Save output to archive files as specified.-l
: Limit the output size but do not split it.-L
: Limit the output size and split it.-n
: Name of the archive to be included in the header.-a
: Allow automatic generation of headers.
Note: The -o
option is required if the -l
or -L
option is used, and the -n
option is required if the -a
option is used.
Create a shar archive file:
shar file_name.extension > filename.shar
Extract a shar archive file:
unshar file_name.shar
3. ar Command
ar (short for archiver) is a utility for creating and manipulating archives, primarily used for binary object file libraries. It can create archives of any kind but has largely been replaced by tar
. Nowadays, it is primarily used for creating and updating static library files.
ar Options:
-d
: Delete modules from the archive.-m
: Move members to the archive.-p
: Print specified members of the archive.-q
: Quick append.-r
: Insert file members to archive.-s
: Add an index to the archive.-a
: Add a new file to the existing archive members.
Create an archive using ar:
ar cr libmath.a subtraction.o division.o
Extract an ar archive file:
ar x libmath.a
4. cpio Command
cpio (Copy In and Out) is a general-purpose archiving tool in Linux, which is commonly used by the Red Hat Package Manager (RPM) and in the initramfs of the Linux kernel. cpio is also an important archiving tool in Apple’s Installer (pax).
cpio Options:
-0
: Read a list of filenames terminated by a null character instead of a new line.-a
: Reset access time.-A
: Append to an archive.-b
: Swap bytes.-d
: Create directories as needed.
Create a cpio archive file:
ls | cpio -ov > /path/to/output_folder/obj.cpio
Extract a cpio archive file:
cpio -idv < /path/to/folder/obj.cpio
5. gzip Command
gzip is a widely used compression and decompression utility, which compresses files and produces tarballs in the format *.tar.gz
or *.tgz.
gzip also supports file concatenation.
gzip Options:
--stdout
or--to-stdout
: Produce output on standard output.--decompress
or--uncompress
: Decompress a file.-d
: Decompress a file.-f
: Force compression or decompression.
Create a gzip archive:
tar -cvzf name_of_archive.tar.gz /path/to/folder
Extract a gzip archive:
gunzip file_name.tar.gz
After decompression, extract the tar archive using:
tar -xvf file_name.tar
Note: Due to the architecture and functionality of gzip, it is difficult to recover corrupted .gz
archive files. It is recommended to create multiple backups of critical files at different locations.
Conclusion
That’s all for now. In the next article, we will cover other compression and decompression utilities available for Linux. Stay tuned and connected to Tecmint. Don’t forget to leave your valuable feedback in the comments section below.
Was there ever a “Part 2” to this article?
@Dragonmouth,
Thanks for your interest!
Yes, we’ve published Part 2, where we cover 5 more advanced archive tools for Linux, including
bzip2
,xz
,zip
,7zip
, and more.You can check it out here: https://www.tecmint.com/linux-archive-tools/.
Let me know if you find it helpful or have any questions!
Do not know how will the data center to backup data, but in my tests that have been mainly do backups of files on NTFS partitions I have come to the conclusion that whenever data is lost, there are files that are not copied by permission error I have not found how to solve this problem.
My favorite tools are Rsync to back up files without compressing them. Ever I made a tar 50GB and then failed on some point after trying to unzip.
And Tar uncompressed files to store long term and/or for divide them into DVDs.
I definitely like to avoid compression to the maximum, or limit the size of compressed files according to my need is for now, the maximum size of a DVD.
What are the most efficient ways to divide files into volumes ?, eg DVDs?
These are my favorite uses of tar
$ tar -cvf dvd-001.tar -ML 4480M –totals=SIGQUIT -C source
I failed to understand understood the script indicated in the documentation for an automatic Tar multivolume.
@Jesus,
Thanks for sharing your real life backup scenarios with us, to be fact I never use tar command, when my data is larger than 15GB, because it takes lots of time in compressing the data or moving from one location to another. So, I always use rsync tool and recommend anyone who want to make a larger backups and it’s easier to make a mirrors of folders in local or remote. If you still want to split the files, better user split command as shown:
Read more about split command man pages..
Researching the multi volume tar, but I do not work for me
http://www.gnu.org/software/tar/manual/html_node/Multi_002dVolume-Archives.html
$ vim new-volume.sh
#! /bin/bash
# For this script it’s advisable to use a shell, such as Bash,
# that supports a TAR_FD value greater than 9.
echo Preparing volume $TAR_VOLUME of $TAR_ARCHIVE.
name=`expr $TAR_ARCHIVE : ‘\(.*\)-.*’`
case $TAR_SUBCOMMAND in
-c) ;;
-d|-x|-t) test -r ${name:-$TAR_ARCHIVE}-$TAR_VOLUME || exit 1
;;
*) exit 1
esac
echo ${name:-$TAR_ARCHIVE}-$TAR_VOLUME >&$TAR_FD
$ chmod 755 new-volume.sh
$ tar -c -L650M -f cd-disk.tar -F new-volume Videos
tar: new-volume: Cannot exec: No such file or directory
tar: Error is not recoverable: exiting now
tar: ‘new-volume’ command failed
tar: Error is not recoverable: exiting now
This creates the first tar file named cd-disk.tar 650 MB correctly, but then throws an error and not continuous creaando the multivolume.
Some clue?, Thanks.
Why are you using this – >
# gunzip file_name.tar.gz
Can you not directly use this ->
# tar -zxvf file_name.tar.gz
sir999
tar has a lot of option and is quite confusing too, at times. gunzip was a straight command. Moreover we mean to show the use of gunzip there.
Hope you understand.
Agreed – but that wasn’t the typo that I first saw.
Take a look at the first sub heading – What is Archieved file? Exactly how that one got past the Spell Check I’m not sure.
My pet peeve are typos:
“shar has an advantage of being **plane** text ”
I am sure you meant “plain text”, not “plane text”. I imagine “plane text” would be the magazines in the seat backs one jets.
It was funny though, and very typical of the state of internet writing.
Good article though.
Steve