TAR COMMAND EXPLAINED
tar command
The tar program is used to create, maintain, modify, and extract files that are archived in the tar format.
The tar command on Linux is often used to create .tar.gz or .tgz archive files, also called “tarballs.”
"tar" stands for tape archive. It is an archiving file format.
tar was originally developed in the early days of Unix for the purpose of backing up files to tape-based storage devices.
How to create tar file
To create tar file run following command
$tar -cvf test.tar custom_folder
in above command
· tar:- command
· c:- option to creates a new .tar archive file
· v:- option to display a list of the files that are included in the archive
· f:- option to specifytype of the archive file.You should always use -f option as the final option in sequence otherwise, the system will become confused as to the desired name for the new file and will use the next option in the sequence as the name.
· test.tar:- archived file name
· custom_folder:- folder which is going to archived
How to compress a tar file
tar command does not have compress or decompress features on its own. tar command is combined with external compression utility.
Option
|
Compression utility
|
-j
|
bzip2
|
-z
|
gzip
|
-Z
|
compress
|
When you use any of above switch to combine compress utility with tar, compression utility compress the new archive file as soon as it has been created.
How to extract tar file
Following steps should be taken before extracting a tar file.
1. Check is tar file compressed or not? You can easily determine it by looking at the filename extension.
2. If the tar file has been compressed, it must first be decompressed using the appropriate decompression program.
3. Move tar file in an empty directory. Create new if you do not have one. It would prevent the reconstituted files from cluttering up the current directory and overwriting any files or directories with same names that are in it.
4. It is also advisable to check sufficient space available before unpacking tar file.
· Use -x option to extract
· Use -v option to display the list of files during the unpack process
· Use -f option to specify file name
Same options can be used to have the compression programs automatically decompress tar files prior to extraction. For example to uncompress bz2 file use -j switch or to uncompress gzip file use -z switch.
In following example we would decompress and extract the tar file compressed in above example
make a folder and move all three tar files
Extract tar file
Extract tar file compressed with bz2
Extract tar file compress with gzip
Linux tar command examples
So far in this article we have covered basic of tar with example. Now we will take some advance example of tar command. These examples of tar command helpful for beginner in linux. So let check how Linux system administrator use tar command with example
How to list files of tar tile
Some time you may want to check which files tar file contains without extracting tar file. Use - t option to list the content of tar file without extracting it. -t option works well with compressed tar files as well.
How to remove files after creating tar file
By default, tar creates an archive of copies of the original files and/or directories, and the originals are retained. To remove files / directories after creating tar file use --remove-files option.
How to extract single file form tar file
You can extract specific file form tar archive. File name is required for it. You can check file name from -t option as we did in above example. You need to specify full path of file.
To extract single file form tar archive
$tar -xvf [Archived tar file name] [name of file ]
If tar file compressed with bz2 use -j option while extracting
$tar -xvjf [Archived tar file name] [name of file ]
If tar file compressed with gzip use -z option while extracting
$tar -xvzf [Archived tar file name] [name of file ]
How to extract multiple files from tar archive
In above example we extracted single file but you can extract multiple files from tar archive. You can specify multiple files separately
To extract single file form tar archive
$tar -xvf [Archived tar file name] ["name of file" ] ["name of file" ]
If tar file compressed with bz2 use -j option while extracting
$tar -xvjf [Archived tar file name] ["name of file" ] ["name of file" ] ["name of file" ]
If tar file compressed with gzip use -z option while extracting
$tar -xvzf [Archived tar file name] ["name of file" ] ["name of file" ] ["name of file" ]
How to extract group files from tar file
You can use wildcard to extract group of files from tar file. Same as above if tar file is compressed you need to use same options [-j for bz2, -z for gzip ] as well.
How to add new files in existing tar file
-r option is used to append the existing tar archive.
You can only append tar file if it is not compressed. You cannot append tar file if you have used -j , -z or -Z options during the creation process.
How to exclude files from tar
While creating tar archive some time you need to exclude certain files or directories. With --exclude option you can specify the file or directory
You can use multiple exclude options for tar like
$ tar --exclude='file1' --exclude='directory/file2' --exclude='patter*'