How to use the tar command in Linux?

In Linux, the tar command is used to compress and decompress files and directories. Here are some common use cases for the tar command:

  1. Create a tar archive file.
  2. Create an archive file called archive.tar and add file1, file2, and dir1 to the archive.
  3. Create a gzip compressed archive file named archive.tar.gz and add file1, file2, and dir1 to the archive.
  4. Create a bzip2 compressed archive file called archive.tar.bz2, and add file1, file2, and dir1 to the archive.
  5. Extract a tar archive file.
  6. Unpack the archive file named archive.tar using the command tar -xf.
  7. Extract the gzip compressed archive file named archive.tar.gz using the command tar -xzf archive.tar.gz.
  8. Extract the bzip2 compressed archive file named archive.tar.bz2 using the command tar -xjf.
  9. View the contents of a tar archive file.
  10. List the files and directories in archive.tar by using the command tar -tf archive.tar.
  11. List the files and directories in archive.tar.gz.
  12. List the files and directories in archive.tar.bz2 by using tar -tjf.
  13. Append a file to an existing tar archive.
  14. Add file3 to the archive file named archive.tar using the command tar -rf archive.tar file3.
  15. Add file3 to a gzip compressed archive file named archive.tar.gz using the command: tar -rzf archive.tar.gz file3.
  16. tar -rjf archive.tar.bz2 file3: Add file3 to the bzip2 compressed archive file named archive.tar.bz2.

Here are just a few common uses of the tar command listed here. There are many other options and uses for the tar command, which you can find detailed instructions on by using the man tar command.

bannerAds