tar
The tar command in Linux is a highly versatile tool used for file archiving and compression. The name tar stands for "Tape Archive", a throwback to its original purpose for writing data to tape drives. It allows multiple files and directories to be bundled together into a single archive file (commonly known as a tarball), and optionally compress it for space efficiency. Here are the key aspects and functionalities of the tar command:
Creating Archives: To create a tarball,
taris used with the-c(create) option, along with-fto specify the filename of the archive. For example,tar -cf archive.tar file1 file2 dir1will create an archive file namedarchive.tarcontainingfile1,file2, anddir1.Viewing Archive Contents: To view the contents of a tarball without extracting it, use the
-t(list) option. For example,tar -tf archive.tarlists the contents ofarchive.tar.Extracting Archives: To extract the contents of a tarball, use the
-x(extract) option. For example,tar -xf archive.tarwill extract the contents ofarchive.tarinto the current working directory.Compression:
tarcan be combined with compression methods like gzip or bzip2. To create a compressed tarball, you use additional options:-zfor gzip (resulting in.tar.gzor.tgzfiles) or-jfor bzip2 (resulting in.tar.bz2files). For example,tar -czf archive.tar.gz directorywill create a gzipped tarball ofdirectory.Decompressing and Extracting: To extract a compressed tarball, you include the same compression option. For example,
tar -xzf archive.tar.gzwill extract the contents of a gzipped tar archive.Appending Files to an Archive: The
-roption allows you to append files to an existing tarball. Note that this doesn’t work with compressed archives.Updating an Archive: The
-uoption is used to update files within an archive that have changed since the archive was created.Preserving Permissions:
tarpreserves file permissions and ownership in the archive, which makes it a popular choice for backups and transferring files between systems.Piping and Redirection:
tarcan be used with piping and redirection in Linux. For example, you can pipe the output oftartosshfor remote backups or use it with other commands for advanced file processing tasks.
Last updated