Home » Unix command and scripts » how to tar a directory in Linux

how to tar a directory in Linux

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:

cCreate. Writing begins at the beginning of the tar-file, instead of at the end.
rReplace. The named files are written at the end of the tarfile.
tTable of Contents are listed.
uUpdate. 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.
xExtract or restore. The named files are extracted from
the tarfile and written to the directory specified in
the tarfile, relative to the current directory.
XExclude. 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

See also  how to start and stop httpd service

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

4.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

 6. 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/

See also  How to use Putty for SSH

Related articles 
unzip tar.gz file in Linux : Check out this post on how to create ,list and unzip Tar.gz file in Linux, how to use bzip2 compression, how to compress and extract using other options
Linux command for Oracle DBA : This page has useful Unix command for Oracle DBA to help you in your day-to-day activities. Same are applicable for Linux also.How to kill the process
sed command in unix examples : sed command is a Stream Editor – works as a filter processing  input line by line And here are 32 Useful sed command examples in Linux/Unix
basic unix command :Unix Script Tutorial for basic unix command which are used many times . Complete description is given for each of the command
unix shell script for loop example : what is loop statement in Unix shell scripting,what are different type of statement with examples. unix shell script for loop example
awk command in Unix : Awk command in Unix or Linux is a powerful command for processing text. Learn about awk syntax, records, fields, line Separator with examples of awk in unix

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top