Dump the code

TAR: tape archive in Linux

Created 8 months ago
Posted By admin
3min read
Tar, short for "tape archive," is a command-line utility commonly used in Unix or Linux for archiving and compressing files and directories. It allows users to bundle multiple files and directories into a single archive file, which can be easily transmitted, stored, or shared. Originally designed for tape storage, tar is now widely used for various purposes, including backup, distribution, and system administration tasks.

Creating an Archive:
   
To create a new archive (a compressed or uncompressed collection of files and directories), you use the 'tar' command with the '-c' option to create, the '-f' option to specify the archive file name, and then list the files and directories you want to include in the archive.

Example

tar -cvf archive.tar file1.txt file2.txt directory/

This command creates a new archive file named 'archive.tar' and includes 'file1.txt', 'file2.txt', and everything inside the 'directory/' in the archive.

Extracting Files from an Archive:
   
To extract files from an existing archive, you use the 'tar' command with the '-x' option (for extract), the '-f' option to specify the archive file name, and possibly the '-v' option for verbose output.

Example:

tar -xvf archive.tar
 
This command extracts the contents of the 'archive.tar' file.

Creating a Compressed Archive (Optional):

You can compress the archive using gzip (-z) or bzip2 (-j). This is optional, and you can choose to do this based on your preference.

Example (gzip):

tar -czvf archive.tar.gz file1.txt file2.txt directory/

This command creates a compressed archive named 'archive.tar.gz'.

Extracting Files from a Compressed Archive (Optional):
If you compressed the archive, you need to add the '-z' option when extracting.

Example (gzip):

tar -xzvf archive.tar.gz
 
This command extracts the contents of the compressed archive 'archive.tar.gz'.

Listing Contents of an Archive:

You can list the contents of an archive without extracting it using the '-t' option.

Example:

tar -tvf archive.tar

This command shows you a list of files and directories stored in the 'archive.tar' file.

Conclusion:

Tar plays a crucial role in simplifying the management and transfer of data in Unix-like systems, providing an efficient means of bundling and compressing files for storage or distribution. Its versatility and simplicity make it a fundamental tool for system administrators, developers, and users working in command-line environments.
Topics

Mastering Nginx

27 articles

Bash script

2 articles

Crontab

2 articles