In our day-to-day life we come across, archived files on the platforms of all kind be it Windows, Mac or Linux. There are several Application program available for all of the platforms to create archive files as well as uncompress them. When it comes to work on Linux Platform, we need to deal with archived files very frequently.
Here in this article we will be discussing archive tools available on standard Linux Distribution, their features, Examples, etc. The article divided into two parts, each part contains five command line archive tools (i.e. total of 10 Best Command Line Archive Tools).
What is Archived file?
An archive file is a compressed file which is composed of one or more than one computer files along with metadata.
Features of Archiving
- Data Compression
- File Concatenation
- Automatic Extraction
- Automatic Installation
- Source Volume and Media Information
- File Spanning
- Directory Structure Information
- Other Metadata (Data About Data)
- Error discovery
Area of Application
- Store Computer Files System along with Metadata.
- Useful in transferring file locally.
- Useful in transferring file over web.
- Software Packaging Application.
The useful archiving application on standard Linux distribution follows:
1. tar Command
tar is the standard UNIX/Linux archiving application tool. In its early stage it used to be a Tape Archiving Program which gradually is developed into General Purpose archiving package which is capable of handling archive files of every kind. tar accepts a lot of archiving filter with options.
- -A : Append tar files to existing archives.
- -c : Create a new archive file.
- -d : Compare archive with Specified filesystem.
- -j : bzip the archive
- -r : append files to existing archives.
- -t : list contents of existing archives.
- -u : Update archive
- -x : Extract file from existing archive.
- -z : gzip the archive
- –delete : Delete files from existing archive.
Create a tar archive file.
# tar -zcvf name_of_tar.tar.gz /path/to/folder
Decompress an tar archive file.
# tar -zxvf Name_of_tar_file.tar.gz
For more detailed examples, read 18 Tar Command Examples in Linux.
shar which stands for Shell archive is a shell script, the execution of which will create the files. shar is a self-extracting archive file which is a legacy utility and needs Unix Bourne Shell to extract the files. shar has an advantage of being plain text however it is potentially dangerous, since it outputs an executable.
- -o : Save output to archive files as specified, in the option.
- -l : Limit the output size, as specified, in the option but do not split it.
- -L : Limit the output size, as specified, in the option and split it.
- -n : Name of Archive to be included in the header of the shar files.
- -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 an shar archive file.
# unshar file_name.shar
3. ar Command
ar is the creation and manipulation utility for archives, mainly used for binary object file libraries. ar stands for archiver which can be used to create archive of any kind for any purpose but has largely been replaced by ‘tar’ and now-a-days it is used only to create and update static library files.
- -d : Delete modules from the archive.
- -m : Move Members in the archive.
- -p : Print specified members of the archive.
- -q : Quick Append.
- -r : Insert file member to archive.
- -s : Add index to archive.
- -a : Add a new file to the existing members of archive.
Create an archive using ‘ar‘ tool with a static library say ‘libmath.a‘ with the objective files ‘substraction’ and ‘division’ as.
# ar cr libmath.a substraction.o division.o
To extract an ‘ar’ archive file.
# ar x libmath.a
cpio stands for Copy in and out. Cpio is a general purpose file archiver for Linux. It is actively used by RedHat Package Manager (RPM) and in the initramfs of Linux Kernel as well as an important archiving tool in Apple Computer’s Installer (pax).
- -0 : Read a list of filenames terminated by a null character instead of a newline.
- -a : Reset Access time.
- -A : Append.
- -b : swap.
- -d : Make Directories.
Create an ‘cpio’ archive file.
# cd tecmint # ls file1.o file2.o file3.o # ls | cpio -ov > /path/to/output_folder/obj.cpio
To extract a cpio archive file.
# cpio -idv < /path/to folder/obj.cpio
gzip is standard and widely used file compression and decompression utility. Gzip allows file concatenation. Compressing the file with gzip, outputs the tarball which is in the format of ‘*.tar.gz‘ or ‘*.tgz‘.
- –stdout : Produce output on standard output.
- –to-stdout : Produce output on standard output.
- –decompress : Decompress File.
- –uncompress : Decompress File.
- -d : Decompress File.
- -f : Force Compression/Decompression.
Create an ‘gzip’ archive file.
# tar -cvzf name_of_archive.tar.gz /path/to/folder
To extract a ‘gzip’ archive file.
# gunzip file_name.tar.gz
The above command must be passed followed with below command.
# tar -xvf file_name.tar
Note: The architecture and functionality of ‘gzip’ makes it difficult to recover corrupted ‘gzipped tar archive’ file. It is advised to make several backups of gzipped Important files, at different Locations.
That’s all for now. We will be discussing other compressing and decompressing applications, available for Linux, in our next article. Till then stay tuned and connected to Tecmint. Don’t forget to provide us with your valuable feedback in the comment section below.