Home »
Linux
Archiving Files Using Linux Command Line
By IncludeHelp Last updated : October 20, 2024
As we already understand what Compression (Compression techniques in Linux) is? We shall learn about Archives. We prefer compression as it is convenient to send file compressed through a network but sometimes it is not a smart way to compress the multiple files separately but as a single entity, we can sure compress multiple files in a single line but in the core, all files are compressed independently.
What is Archiving in Linux?
Archiving is the solution to this problem. The UNIX utility to archive files is tar, that is TApeaRchive.
Modes of TAR
Tar has 3 modes those are:
- Create: which is a new archive from a group of files
- Extract: means getting one or more files from the archive
- The list: which shows the content of the archive without extracting it.
Our operations and command for archiving includes above three modes and will discuss throughout this article.
A tar file is also called a tarball, now let's dive into the practical thing.
Example 1: Create Mode
Explanation
Creating a Tarball(at least) requires 2 options, here we used 'c' and 'f' :
- c: create an archive file
- f: tells tar to expect a file name as next argument.
In line 1, we created the tar file with .tar extension and used a wildcard for all the files we have selected (multiple file name or both can be used).
tar command consists of:
tar [options] tarname filename1 filename2 ...
The first option of tar always is its mode, here it was created ('c') then 'f' where we pass file names after the name ofthe tar file.
In line 2, the size of includehelp.tar is slightly more than the files it contains, but it can be compressed either explicitly compressing it with gzip(or bzip2) or with tar itself.
Example 2: Compressing
Explanation
In the above example, we used 3 option format of tar, for compression with achieving.
In line 2, again the first option is to create mode, second one 'z' is for gzip (we can use 'j' for bzip2), and third for file argument. Format of compress tar with gzip is .tar.gz or .tgz(for bzip2, format will be .tar.bz or tbz).
Example 3: List mode
In the above sample, the command used is:
tar –tzvf filename
Where, –t option is used for list files in archive and z for gzip (j for bzip2) and v is verbose that is whatever is happing will shown on screen and f for given filename includehelp.tar.gz (or .tbz).
Note:
Although the position of the option doesn't matter in Linux command line in case of tar command, we always need to keep –f option at the end because after f it expects a filename.
Recommended Articles: