tar's Examples

Creating an uncompressed tar Archive using option -cvf : This command creates a tar file called file.tar which is the Archive of all .c files in current directory. 

$ tar cvf file.tar *.c

Extracting files from Archive using option -xvf : This command extracts files from Archives. 

$ tar xvf file.tar

gzip compression on the tar Archive, using option -z : This command creates a tar file called file.tar.gz which is the Archive of .c files. 

$ tar cvzf file.tar.gz *.c

Extracting a gzip tar Archive *.tar.gz using option -xvzf : This command extracts files from tar archived file.tar.gz files.  

$ tar xvzf file.tar.gz

Creating compressed tar archive file in Linux using option -j : This command compresses and creates archive file less than the size of the gzip. Both compress and decompress takes more time then gzip.  

$ tar cvfj file.tar.tbz example.cpp

Untar single tar file or specified directory in Linux : This command will Untar a file in current directory or in a specified directory using -C option. 

$ tar xvfj file.tar 
or 
$ tar xvfj file.tar -C path of file in directory

Untar multiple .tar, .tar.gz, .tar.tbz file in Linux : This command will extract or untar multiple files from the tar, tar.gz and tar.bz2 archive file. For example the above command will extract “fileA” “fileB” from the archive files.  

$ tar xvf file.tar "fileA" "fileB" 
or 
$ tar zxvf file1.tar.gz "fileA" "fileB"
or 
$ tar jxvf file2.tar.tbz "fileA" "fileB"

Check size of existing tar, tar.gz, tar.tbz file in Linux : The above command will display the size of archive file in Kilobytes(KB).  

$ tar czf file.tar | wc -c
or 
$ tar czf file1.tar.gz | wc -c
or 
$ tar czf file2.tar.tbz | wc -c

Update existing tar file in Linux  

$ tar rvf file.tar *.c

list the contents and specify the tarfile using option -tf : This command will list the entire list of archived file. We can also list for specific content in a tarfile  

$ tar tf file.tar

Applying pipe to through ‘grep command’ to find what we are looking for : This command will list only for the mentioned text or image in grep from archived file.  

$ tar tvf file.tar | grep "text to find" 
or
$ tar tvf file.tar | grep "filename.file extension"

We can pass a file name as an argument to search a tarfile : This command views the archived files along with their details.  

$ tar tvf file.tar filename

Viewing the Archive using option -tvf  

$ tar tvf file.tar

To search for an image in .png format : This will extract only files with the extension .png from the archive file.tar. The –wildcards option tells tar to interpret wildcards in the name of the files 

$ tar tvf file.tar --wildcards '*.png'

To combine multiple files into a single archive file (for example, my_files.tar), use the following command (replace file1 and file2 with the names of the files you want to combine)

tar -cvf my_files.tar file1 file2

To combine all the files in a directory into a single archive file (for example, my_files.tar), use the following command (replace /path/to/my/directory with the absolute path to the directory containing the files you want to combine).

tar -cvf my_files.tar /path/to/my/directory

To use tar and gzip to combine multiple files into a compressed archive file (for example, my_files.tar.gz), use the following command (replace file1 and file2 with the names of the files you want to combine).

tar -cvzf my_files.tar.gz file1 file2

To use tar and gzip to combine all the files in a directory into a compressed archive file (for example, my_files.tar.gz), use the following command (replace /path/to/my/directory with the absolute path to the directory containing the files you want to combine).

tar -cvzf my_files.tar.gz /path/to/my/directory

If your system does not use GNU tar, but nonetheless has gzip, you can create a compressed tar archive file (for example my_files.tar.gz with the following command (replace file1 and file2 with the names of the files you want to combine)

tar -cvf - file1 file2 | gzip > my_files.tar.gz

If gzip isn't available on your system, you can use the compress utility to create a compressed archive (for example, my_files.tar.Z); for example (replace file1 and file2 with the names of the files you want to combine).

tar -cvf - file1 file2 | compress > my_files.tar.Z

To extract the contents of a tar archive file compressed with compress (for example, my_files.tar.Z), use the following command

uncompress -c my_files.tar.Z | tar -xvf -

If you are not using GNU tar and need to extract the contents of a tar archive file compressed with gzip (for example, my_files.tar.gz), use the following command

gunzip -c my_files.tar.gz | tar -xvf -

DevOpsSchool
Typically replies within an hour

DevOpsSchool
Hi there 👋

How can I help you?
×
Chat with Us