how to tar a directory in Linux

Last updated on January 13th, 2019 at 02:36 pm

In continuation of our series of Unix tutorial ,i am here presenting article on how to tar a directory in Linux.Topics included in this are how to tar a directory in Linux, tar compress directory command, tar gzip command,how to extract and list files from tar file ,GNU tar command

tar – create tape archives and add or extract files.A more common use for tar is to simply combine a few files into a single file, for easy storage and distribution. The tar command archives and extracts files to and from a single file called a tarfile.

tar command example

-I include-file ==> Opens include-file containing a list of files, one per line, and treats it as if each file appeared separately on the command line.

Function Letters
The function portion of the key is specified by one of the
following letters:

c Create. Writing begins at the beginning of the tar-file, instead of at the end.
r Replace. The named files are written at the end of the tarfile.
t Table of Contents are listed.
u Update. The named files are written at the end of the tarfile if they are not already in the tarfile, or if
they have been modified since last written to that
tarfile.
x Extract or restore. The named files are extracted from
the tarfile and written to the directory specified in
the tarfile, relative to the current directory.
X Exclude. Use the exclude-file argument as a file con-
taining a list of relative path names for files (or
directories) to be excluded from the tarfile when
using the functions c, x, or t.

Practical tar command example

1.how to tar a directory in Linux

Using the tar Command to Create an Archive of Your Home Directory.

$tar -cvf  /home/user_dir.tar .

how to tar a directory in Linux
2. Lists the contents of the tar file created.

$tar -tvf /home/user_dir.tar

List content of tar files in Linux
3. To extract files from tar file:

$tar -xvf user_dir.tar
  1. To tar files as per include options:
$tar -cvf user_dir.tar -I list_of_files

tar command in linux with -l option

5. Extract a single file from tar

tar -xvf user_dir.tar /home/oracle/network/admin/tnsnames.ora

extract single file from tar file in Linux

  1.  Extract a single directory from tar
tar -xvf user_dir.tar /home/oracle/network/admin/

7.Extract group of files from tar  using regular expression

tar -xvf oracle_hom.tar –wildcards ‘*.ora’

8. Adding a file or directory to an existing archive using option -r

tar rvf host_dir.tar new.txttar rvf host_dir.tar new/

add new file to the tar archive

Few important things to note down for tar command line

1) The relative path is important while extracting file from tar

Example

tar -cvf user_dir.tar /home/oracle/jdk

It create a tar file of/home/oracle/jdk and its all inside directory in the current directly where the command is executed

Now if you plan to untar this file.
Then It will create file in the same relative path
i.e
/home/oracle/jdk/file1
/home/oracle/jdk/file2

Now if you take the tar backup like this

cd /home/oracle/

tar -cvf user_dir.tar jdk

It create a tar file of jdk and its all inside directory in the current directly where the command is executed
Now if you plan to untar this file.
Then It will create file in the same relative path
i.e
jdk/file1
jdk/file2

So basically you can create jdk directory in any folder by this approach while by first approach,it will always created in /home/oracle/

Related articles 

How to create ,List and unzip Tar.gz file in Linux

Useful Linux command for Oracle DBA

Awesome sed command in unix examples

Unix Script Tutorial : basic unix command

Writing loop statement in Unix shell scripting with examples

21 awk command examples in Unix/Linux

Leave a Reply