How to Archive and Extract Data on Red Hat Enterprise Linux 8

 Archive and Extract Data on RHEL 8 By use of Command “tar”

Archive is a collection of data that are file and directories. By the help of archiving, you can collect a data in a single file. In archiving of data, the compression is use to reduce the size of data. In Window system, the archive files with compression contain the extension .rar, .zip, etc. In Linux system, the archive files with compression contain the extensions .tar.gz or tgz, .tar.bz2, .tar.xz, etc.

Mostly archive is used to store large amount of data and compression is apply to reduce its size. Archive is use to protect data from damages and lost, if original data delete for any reason the archive data is last hope on that case. Archiving is a convenient way to collect the multiple files and multiple directories into single file, the files can be any format. In an archive file, you can read the content easily without restore the complete content. For sending an email archive file is very useful to send multiple files into single file.

In Linux tar command is use to archive the multiple files into single file and compress this file that have three methods.

  1. gzip
  2. bzip2
  3. xz

gzip: The gzip is the fastest compression of tar file but this is the oldest method. The extension of gzip compression is use on tar files are filename.tar.gz or filename.tgz. The attribute use for gzip compression is “z”.

bzip2: The bzip2 compression is usually lead to smaller archive files. The extension of bzip2 compression on tar file is filename.tar.bz2. The attribute use for bzip2 compression is “j”.

xz: The xz compression method is new and it is provide a best compression method then other. The extension of xz compression is use on tar files is filename.tar.xz. The attribute use for xz compression is “J”.

Below mention, tar options are very useful to create and extract the archive file.

c – Option “c” is use to create a new archive file.

x – Option “x” is use to extract the existing archive file.

t – Option “t” is use to list the content of an archive file.

v – Option “v” is verbose that is show which files get archived or extracted.

f – Option “f” is use for file name; this option is use when the archive file is create or extract.

p – Option “p” is use to preserve the permission of files and directories, by use of option “p” extracting archive file will preserve same permission as original means subtracting umask is not apply.

z – Option “z” is use for gzip compression (.tar.gz).

j – Option “j” is use for bzip2 compression (.tar.bz2). bzip2 provide a better compression ratio as compare to gzip.

J – Option “J” is use for xz compression (.tar.xz). xz provide a better compression ratio as compare to bzip2.

To create archive file of multiple files by use of command “tar”, type the below mention command. owais1, owais2 and owais3 are text files; you can create these files by command “touch”. Owais.tar is an archive file.

touch owais1 owais2 owais3

how to create tar archive

tar cf owais.tar owais1 owais2 owais3

how to create tar archive

You can create a multiple files and multiple directories by use of single command. For demo purpose, I am creating three text files and three directories on single directory. Type the below mention command.

mkdir moiz
cd moiz
mkdir moiz1 moiz2 moiz3
touch moiz1 moiz2 moiz3

how use tar on rhel 8

Create and Extract tar file:

To create a tar file of entire directory that have multiple files and directories on it, type the below mention command. In this command option “c” is use to create an archive and option f is use for file name.

tar cf moiz.tar moiz

how use tar on rhel 8

To Extract the archive tar file on the same location where archive tar file is available, type the below mention command.

tar xf moiz.tar

how use tar on rhel 8

When you extract an archive file, umask is subtracted from the permission of archive content. To preserve the permission of an archive file use option “p”.

tar xpf moiz.tar

how use tar on rhel 7

To verify the permissions inside the directory, go to the directory “moiz” by type the command “cd” then type command “ll” to list the content inside the directory.

cd moiz
ll

how use tar on rhel 7

To list the content of archive tar file, type the below mention command. Option “t” is use to list the content of an archive file.

tar tf moiz.tar

how use tar on rhel 7

Create and Extract tar file with gz compression:

To create an archive “tar” file with gz compression, type the below mention command. gz compression is fastest compression but it is oldest.

tar czf moiz.tar.gz moiz

how use tar on rhel 7

To extract the gz compression tar file, type the below mention command.

tar xzf moiz.tar.gz

how use tar on red hat enterprise linux 8

To extract the gz compression tar file and preserve the permission, type the below mention command.

tar xpzf moiz.tar.gz

how to archive and extract data on linux by use tar

To extract the gz compression tar file with option “v” for verbose that is show which files get archived or extracted and option “p” preserve the permission, type the below mention command.

tar xvpzf moiz.tar.gz

how to archive and extract data on red hat enterprise linux 8 by use tar

Create and Extract tar file with bzip2 compression:

By default bzip2 compression package is not install, to install bzip2 compression package type the below mention command.

yum install bzip2.x86_64

how create file using tar

Create a directory “owais” by use of command “mkdir”, then go to the directory “owais” by command “cd” and create text files by use of command “touch”.

mkdir owais
cd owais
touch owais1 owais2 owais3

how create file using tar

To create an archive “tar” file with bzip2 compression, type the below mention command. bzip2 compression better then gz compression.

tar cjf owais.tar.bz2 owais

To list the directory and its files, type the below mention command.

ls -lrt owais.tar.bz2 owais

To list only the directory, type the below mention command.

ls -lrd owais.tar.bz2 owais

how create file using tar

To extract the bzip2 compress file, type the below mention command.

tar xjf owais.tar.bz2

how to archive file using tar on rhel 8

To extract the bzip2 compress file with permission type the below mention command.

tar xpjf owais.tar.bz2

To verify the permission of extracted file type the below mention command.

ls -lrt owais owais.tar.bz2

how to archive file using tar

Create and Extract tar file with xz compression:

To check xz compression package is install, type the below mention command.

yum list xz

how to archive file using tar on centos 7

To create an archive “tar” file with xz compression, type the below mention command. xz compression better then bzip2 compression. Option “J” is use for xz compression.

tar cJf owais.tar.xz owais

how to archive file using tar on centos 7

To extract an archive “tar” file that have xz compression and use option “p” to preserve the permission, type the below mention command.

tar xpJf owais.tar.xz

how to archive file using tar on centos 8

Extract File to a Directory:

This is a safe method to extract tar file on directory. First, create a directory “moiz” by use of command “mkdir” the go to the directory by command “cd” and extract tar file on the directory.

mkdir moiz
cd moiz
tar xpJf /root/owais.tar.xz

how create and extract tar file

Thanks for Read this Article

Comments