AMP AMP

Best CommandLine Archive Tools for Linux

Best Command Line Archive Tools for Linux

We encounter archived files on the platforms like Windows, Mac or Linux everyday. There are many application programs available for different platforms to create archive files and also uncompress them. In Linux Platform, we need to deal with archived files very often. This tutorial explains about archive tools available on standard Linux Distribution about their features and examples.

Archived file

An archive file is a compressed file that is composed of one or more computer files with metadata.

Features of Archiving

  • File Concatenation
  • Data Compression
  • Error discovery
  • Automatic Installation
  • Encryption
  • Checksum
  • Source Volume and Media Information
  • Automatic Extraction
  • File Spanning
  • Other Metadata
  • Directory Structure Information

Area of Application

  • Software Packaging Application.
  • Store Computer Files System along with Metadata.
  • Useful in transferring file over web.
  • Useful in transferring file locally.

Some important archiving application on standard Linux distribution are as follows:

Tar Command

Tar command is the standard UNIX/Linux archiving application tool, which was earlier used to be a Tape Archiving Program and gradually developed into General Purpose archiving package. It is capable of handling archive files of any kind.


tar options
-c : Create a new archive file.
-A : Append tar files to existing archives.
-j : bzip the archive
-d : Compare archive with Specified filesystem.
-t : list contents of existing archives.
-r : append files to existing archives.
-x : Extract file from existing archive.
-u : Update archive.
--delete : Delete files from existing archive.
-z : gzip the archive

tar Examples
Create a tar archive file

root@linuxhelp:~# ls
Desktop    Downloads         Music     Public     test
Documents  examples.desktop  Pictures  Templates  Videos
root@linuxhelp:~# tar -zcvf test.tar.gz test
test/
test/file2
test/file3
test/file1
root@linuxhelp:~# ls
Desktop    Downloads         Music     Public     test         Videos
Documents  examples.desktop  Pictures  Templates  test.tar.gz

To extract tar archive file

root@linuxhelp:~# ls
Desktop    Downloads         Music     Public     test.tar.gz
Documents  examples.desktop  Pictures  Templates  Videos
root@linuxhelp:~# tar -zxvf test.tar.gz 
test/
test/file2
test/file3
test/file1
root@linuxhelp:~# ls
Desktop    Downloads         Music     Public     test         Videos
Documents  examples.desktop  Pictures  Templates  test.tar.gz

ar Command

ar command is a manipulation utility for archives and is widely used for binary object file libraries. The ar stands for archive that can be used to create archive but has been replaced by tar and now it is used only to create and update static library files.


ar options
-m : Move Members in the archive.
-d : Delete modules from the archive.
-q : Quick Append.
-p : Print specified members of the archive.
-s : Add index to archive.
-r : Insert file member to archive.
-a : Add a new file to the existing members of archive.

ar Examples
Create an archive using ar with a static library such as libmath.a along with objective files subtraction and division

To create ar archive file

root@linuxhelp:~# ls
Desktop  doc2       Downloads         Music     Public     Videos
doc1     Documents  examples.desktop  Pictures  Templates
root@linuxhelp:~# ar cr doc.a doc1 doc2
root@linuxhelp:~# ls
Desktop  doc2   Documents  examples.desktop  Pictures  Templates
doc1     doc.a  Downloads  Music             Public    Videos

To extract ar archive file

root@linuxhelp:~# ls
Desktop     Documents  examples.desktop  Pictures  Templates
document.a  Downloads  Music             Public    Videos
root@linuxhelp:~# ar x document.a 
root@linuxhelp:~# ls
Desktop  doc2        Documents  examples.desktop  Pictures  Templates
doc1     document.a  Downloads  Music             Public    Videos

cpio

cpio stands for Copy in and out, which is a general purpose file archive for Linux. It is widely used by RedHat Package Manager and in the initramfs of Linux Kernel. It is used as an vital tool in Apple Computer’ s Installer.

cpio options
-b : swap
-0 : Read a list of file names terminated by a null character instead of a newline
-d : Make Directories
-A : Append
-a : Reset Access time

cpio Examples

Create a cpio archive file

root@linuxhelp:~# ls
Desktop    Downloads         file   Pictures  Templates
Documents  examples.desktop  Music  Public    Videos
root@linuxhelp:~# file cpio -v >  sample.cpio
root@linuxhelp:~# ls
Desktop    Downloads         file   Pictures  sample.cpio  Videos
Documents  examples.desktop  Music  Public    Templates

shar Command

shar stands for Shell archive. It is a shell script and its execution results in creating files. It is a self-extracting archive file that is a legacy utility and requires Unix Bourne Shell to extract the files. It has a main advantage of being plain text but it turns out to be potentially dangerous, because it outputs an executable files.

shar options
-a : Allow automatic generation of headers
-l : Limit the output size, but do not split it
-o : Save output to archive files
-n : Name of Archive to be included in the header of the shar files
-L : Limit the output size and split it

Important: The -o option is needed if the -l or -L option is used and the -n option is needed if the -a option is used.

shar Examples

Create a shar archive file

root@linuxhelp:~# ls
Desktop    Downloads         Music     Public  Templates
Documents  examples.desktop  Pictures  sample  Videos
root@linuxhelp:~# shar sample >  sample.shar
root@linuxhelp:~# ls
Desktop    Downloads         Music     Public  sample.shar  Videos
Documents  examples.desktop  Pictures  sample  Templates

Extract a shar archive file

root@linuxhelp:~# ls
Desktop    Downloads         file.shar  Pictures  sample     Videos
Documents  examples.desktop  Music      Public    Templates
root@linuxhelp:~# unshar file.shar 
file.shar:
x - created lock directory _sh12889.
x - SKIPPING sample (file already exists)
x - removed lock directory _sh12889.
root@linuxhelp:~# ls
Desktop    Downloads         file.shar  Pictures  sample     Videos
Documents  examples.desktop  Music      Public    Templates

rar and unrar Command

rar is used for creating an archive files in the rar format. This format is mostly used in windows.
unrar is used for extracting any archive which is in rar format.

rar options

a - Add files to archive.
d - Delete files from archive.
e - Extract files to current directory.
t - Test archive files.
x - Extract files with full path.

unrar options

e - Extract files to current directory.
l - List archive content.
t - Test archive files.
x - Extract files with full path.

To create a rar archive

root@linuxhelp:~# ls
Desktop    Downloads         Music     Public     text
Documents  examples.desktop  Pictures  Templates  Videos
root@linuxhelp:~# rar a text.rar text/

RAR 5.30 beta 2   Copyright (c) 1993-2015 Alexander Roshal   4 Aug 2015
Trial version             Type RAR -? for help

Evaluation copy. Please register.

Creating archive text.rar

Adding    text/file2.txt                                              OK 
Adding    text/file1.txt                                              OK 
Done
root@linuxhelp:~# ls
Desktop    Downloads         Music     Public     text      Videos
Documents  examples.desktop  Pictures  Templates  text.rar

To extract a rar archive

root@linuxhelp:~# ls
Desktop    Downloads         Music     Public     text.rar
Documents  examples.desktop  Pictures  Templates  Videos
root@linuxhelp:~# unrar x text.rar 

UNRAR 5.30 beta 2 freeware      Copyright (c) 1993-2015 Alexander Roshal

Extracting from text.rar

Creating    text                                                      OK
Extracting  text/file2.txt                                            OK 
Extracting  text/file1.txt                                            OK 
All OK
root@linuxhelp:~# ls
Desktop    Downloads         Music     Public     text      Videos
Documents  examples.desktop  Pictures  Templates  text.rar
FAQ
Q
how can we check the compression ratio?
A
A compression ratio of the compressed file(s) can be verified using the '-l' option
#gzip -l archieve.gz
Q
Can I Extract the archive file?
A
It is possible. Use this command
#tar xvf archive.tar
Q
How can I create an archive file?
A
By follow this command to create an archive file
#tar --create --verbose --file=archive.tar file1 file2 file3
Q
What is Archived file?
A
An archive file is a compressed file which is composed of one or more than one computer files along with metadata.
Q
How to create a share archive file?
A
Use the following command to create a share file
# shar file_name.extension > filename.shar