tar Command in Linux / Unix
tar
is an acronym for Tape Archive. tar command is used to Manipulates archives in Linux/Unix. System administrators uses tar command frequently to rip a bunch of files or directories into highly compressed archive which are called tarball
or tar
, bzip
and gzip
in Linux/Unix system.
tar Syntex
tar [OPTION...] [FILE]...
Or
tar required Flags
tar {-r|-t|-c|-x|-u}
tar optional Flags
tar {one of the required Flags} [ -d ][-B] [ -F ] [ -E ] [ -i ] [-h ] [ -l ] [ -m ] [ -o ] [ -p ] [ -w] [ -s ] [ -U ] [ -v ] [-Number] [-b Blocks] [-f Archive]
Examples
Create tar Archive File by Compressing an Directory or a Single File
The terminal command below will create a .tar
file called sample_dir.tar
with a directory /home/codebind/sample_dir or sample_dir in present working directory.
codebind@codebind:~$ tar -cvf sample_dir.tar sample_dir sample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output codebind@codebind:~$ ls sample_dir sample_dir.tar
Here’s what those flags (-cvf) actually mean
-c, --create
– create a new archive
-x, --extract, --get
– extract files from an archive
-f, --file ARCHIVE
– use archive file or device ARCHIVE
Create tar.gz
or tgz
Archive File by Compressing an Directory or a Single File
The terminal command below will create a .tar.gz
file called sample_dir.tar.gz
with a directory /home/codebind/sample_dir or sample_dir in present working directory.
Notice that we have added extra flag -z
to the command.Here’s what the flag -z
actually mean
-z, --gzip, --gunzip --ungzip
– Compress the archive with gzip
codebind@codebind:~$ tar -cvzf sample_dir.tar.gz sample_dirsample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output codebind@codebind:~$ ls sample_dir sample_dir.tar.gz
The command bellow will create a .tgz file. One this to notice is tar.gz and tgz both are similar.
codebind@codebind:~$ tar -cvzf sample_dir.tgz sample_dirsample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output codebind@codebind:~$ ls sample_dir sample_dir.tgz
Compressing Multiple Directories or Files at Once
Let’s say, For example we want to compress the sample_dir
directory, the java_test
directory, and the abc.py
file to a tar file called sample_dir.tar.gz.
Run the following command to achieve the goal above.
codebind@codebind:~$ tar -cvzf sample_dir.tar.gz sample_dir java_test abc.py sample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output java_test/ java_test/HelloCV.java abc.py codebind@codebind:~$ ls sample_dir java_test abc.py sample_dir.tar.gz
Create .bzip2
Archive File by Compressing an Directory or a Single File
codebind@codebind:~$ tar -cjvf sample_dir.tar.bz2 sample_dir sample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output codebind@codebind:~$
Notice that we have added extra flag -f
to the command.Here’s what the flag -f
actually mean
-f, --file ARCHIVE
– use archive file or device ARCHIVE
Extract .tar
Archive File
We can extract or untar the compressed file using the tar command. The command below will extract the contents of sample_dir.tar to the present directory.
codebind@codebind:~$ tar -xvf sample_dir.tar sample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output codebind@codebind:~$
The following command will extract or Untar files in specified Directory i.e. /home/codebind/dir_name in this case.
codebind@codebind:~$ tar -xvf sample_dir.tar -C /home/codebind/dir_name sample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output codebind@codebind:~$
we have added extra flag -C
to the command.Here’s what the flag -C
actually mean
-C, --directory DIR
– change to directory DIR
Leave a Reply