Tar Command in Linux: Syntax, Options, and Examples

Archiving and unarchiving information in Linux is a vital function that each Linux consumer ought to know for environment friendly file administration. Relating to compressing each information and directories, the “tar command” in Linux can show to be a lifesaver. Whether or not you’re a newbie or a seasoned Linux consumer, having a very good … The post Tar Command in Linux: Syntax, Options, and Examples appeared first on Ferdja.

May 1, 2023 - 02:00
 7
Tar Command in Linux: Syntax, Options, and Examples

Archiving and unarchiving information in Linux is a vital function that each Linux consumer ought to know for environment friendly file administration. Relating to compressing each information and directories, the “tar command” in Linux can show to be a lifesaver. Whether or not you’re a newbie or a seasoned Linux consumer, having a very good know-how of the tar command with its choices might be helpful for numerous duties. On this article, we’ll present you the way to use the tar command in Linux; full with its syntax, choices, and examples.

What’s the tar Command in Linux

Earlier than we speak concerning the tar command, let’s perceive what’s an archive file. It’s a file sort that contains a number of information and directories in a compressed type. This compressed type can then be saved or transferred utilizing little bandwidth on account of its small file dimension. There are numerous completely different file archive codecs resembling .gz, tar.bz, .zip, and so on. If you wish to know the way to deal with zip information, learn our in depth information on the way to zip and unzip information in Linux.

As for the tar command, quick for “Tape Archive”, it’s a Linux utility device that’s used to create and handle archive information of assorted varieties. The tar command is a set of many several types of instruments with which you’ll create and modify archive information. With the tar command, you possibly can create numerous several types of archive information like .tar, .tar.gz, and .tar.bz2 comprising each information and directories. All these file varieties have several types of compression charges and total file sizes.

Tar Command in Linux: Syntax and Choices

The tar command in Linux is fairly easy to make use of and has an easy syntax. Right here’s the fundamental syntax to make use of the tar command:

tar

Within the above syntax, is the identify of the archive file that might be created, specifies the checklist of information and directories to archive. A few of the choices you should use with the tar command are:

Choices Descriptions
-c used to create a brand new archive
-f used to specify the archive filename
-d compares an archive file and, the file system and lists the variations if any
-r provides a brand new file to the archive file
-t lists the contents of an archive file
-u provides solely the up to date information
-x extract information from the archive file
-j extract from the .bz2 file
-z extract from the .gz file
-v present details about every operation going down

Tips on how to create a .tar Archive File in Linux

When information and directories are compressed right into a .tar file, such an archive file is called a tarball. It has a average stage of compression, however positively the next compression stage than .zip. The syntax to compress information and directories right into a tarball is:

tar -cvf .tar

For instance, to compress the listing named “~/Paperwork/take a look at” into an archive file with the identify “test_archive.tar”, you might want to use this command:

tar -cvf test_archive.tar ~/Paperwork/take a look at

archiving files using the tar command

Tips on how to Create a .tar.gz Archive File in Linux

Gzip, one other knowledge compression utility, makes use of the “Deflate Algorithm” to compress information. It has a greater compression stage as in comparison with information which might be compressed right into a tarball. Gzip creates smaller file sizes with the extension .tar.gz, so it’s the mostly used format. However in the case of extracting the information compressed utilizing Gzip, it takes a very long time in comparison with .tar information. To compress information or directories right into a .tar.gz file, use this syntax:

tar -cvzf .tar.gz

Suppose, you wish to create an archive file with the identify “take a look at.tar.gz” of the listing “~/Paperwork/take a look at”, you should use this command:

tar -cvzf take a look at.tar.gz ~/Paperwork/take a look at

compressing files using gzip

Tips on how to Create a .tar.bz2 Archive File in Linux

If you’d like a fair smaller dimension of archive information, use the bzip2 compression format with the tar command. The bzip2 format makes use of the “Burrows–Wheeler Algorithm” that helps to create very small information with the extension .tar.bz. Though you get smaller file sizes, each compressing and decompressing take a very long time in comparison with gzip. The primary drawback of compressing information utilizing bzip2 is that it’s not supported in some stripped-down Linux techniques, in contrast to gzip which is supported on all Linux techniques.

To compress a file/ listing within the bzip2 format utilizing the tar command, you might want to use this syntax:

tar -cvjf .tar.bz2

For instance, to compress your entire ~/Paperwork/take a look at listing right into a “take a look at.tar.bz2”, use this syntax:

tar -cvjf take a look at.tar.bz2 ~/Paperwork/take a look at

compressing files using bzip2 with the tar command

Tips on how to View Archive File Contents Utilizing tar command

So now you understand how to create an archive file, it’s possible you’ll have to view the file contents in an effort to confirm that each one needed information have been archived. To view the contents of an archive file, change the -c flag with -t flag within the syntax above:

tar -tvf

This syntax will work for all of the above three sorts of archive information vis-a-vis .tar, .tar.gz, .tar.bz2. For instance – if you wish to view the contents of the file “test_archive.tar.gz”, use this command:

tar -tvf test_archive.tar.gz

listing the archive file contents

Extract Recordsdata from an Archive File Utilizing tar command

Subsequent, allow us to now see how one can extract all information from an archived file in Linux. To extract information from an archived file or listing, use this command:

tar -xvf

With the above syntax, the information get extracted in the identical listing. For instance – use this command to extract the take a look at.tar.gz file:

tar -xvf take a look at.tar.gz

extracting a gunzip file using the tar command

If you wish to extract the information to a separate listing, you might want to use this syntax:

tar -xvf -C

For instance – if you wish to extract the file ” test_archive.tar.gz” to the “~/Paperwork/take a look at” listing, then use this command:

tar -xvf test_archive.tar.gz -C ~/Paperwork/take a look at

extracting to different directory

Along with extracting all information from an archive file, the tar command additionally offers the added benefit to extract solely chosen information. Relying in your archive file sort, use both of the syntaxes to extract chosen information from the archive file:

tar -xvf

  • Extract from .tar.gz information:

tar -xzvf

tar -jxvf

For instance, if you wish to extract the information “take a look at.py” and “take a look at.png” from the “test_archive.tar.gz” file, use this syntax:

tar -xzvf take a look at.tar.gz house/intel/Paperwork/take a look at/test1/test3.txt house/intel/Paperwork/take a look at/example_1.pdf

extracting two files from an archive file using the tar command

Extract Recordsdata with Matching Sample Utilizing tar Command

Suppose you wish to extract information of just one sort however don’t wish to write the file names one after the other. For such a case, you should use a typical wildcard sample to match the information you wish to extract. The syntax to extract information with a sample utilizing the tar command is:

tar -xvf --wildcards ''

  • Extract from .tar.gz information –

tar -xzvf --wildcards ''

tar -xjvf --wildcards ''

For instance, if you wish to extract all .png information from the “test_archive.tar.bz2” file, use this command:

tar -xjvf test_archive.tar.bz2 --wildcards '*.png'

extracting only selected files inside an archive file
extracting solely chosen information

Add File to Archive File Utilizing the tar Command

There could also be instances if you forgot so as to add a file to the archive file mistakenly. In such a scenario, you possibly can both begin afresh by making the archive file with all of the required information or just use the -r flag to append the file you missed. Most customers will agree that the second technique is extra possible! Use this syntax so as to add a brand new file/listing to a tar archive:

tar -rvf

Let’s say you wish to add the file “test1.png” to “test_archive.tar”, you should use this command:

tar -rvf test_archive.tar Paperwork/take a look at/test1.png

Notice: The above syntax will solely work for .tar information and never for .tar.gz or .tar.bz2 information.

append file using the tar command

Delete File from An Archive File Utilizing tar Command

Identical to you possibly can add a file to an archive file if you happen to missed it one way or the other, likewise you too can delete a file from an archive file with the –delete flag. To delete a file from an archive file, use this syntax:

tar --delete -f

For instance, to delete the file “take a look at.txt” from the archive “test_archive.tar”, use this command:

tar -v --delete house/intel/Paperwork/take a look at/example_1.pdf -f test_archive.tar

delete file inside a tar file

Often Requested Questions

What’s the distinction between zip and tar instructions?

The tar command solely archives the information and directories however doesn’t compress them by default. When mixed with -z or -j flags for gzip or bzip2, the tar can obtain an incredible stage of compression. The zip command, alternatively, can each compress and archive the information/directories.

Can I take advantage of the tar command to archive distant information?

Sure, you should use the tar command together with SSH to archive and switch distant information. For instance, to archive and switch information from a distant server to your native machine, use the command ssh consumer@remote_host "tar -czvf - " | tar -xzvf - -C >.

The post Tar Command in Linux: Syntax, Options, and Examples appeared first on Ferdja.