Working With Tar Compressed Archives in Linux

In Linux, the tar command is commonly used for archiving and extracting files and directories. Tar archives can be compressed to save space using various compression algorithms like gzip (*.tar.gz) or bzip2 (*.tar.bz2). This guide will cover how to create, extract, and work with tar compressed archives in Linux.

Compressing Files and Directories with Tar

1. Create a Tar Archive

To create a tar archive of a folder, use the tar -cvpf command:

1
tar -cvpf file.tar folderToCompress

2. Create a Gzipped Tar Archive

To create a gzipped tar archive (commonly referred to as a .tar.gz file), use the -z option along with tar -czvpf:

1
tar -czvpf file.tar.gz folderToCompress

3. Create a Gzipped Tar Archive Without Parent Directory

To create a gzipped tar archive without including the parent directory in the archive, use the -C option:

1
tar -C folderPath -czvpf file.tar.gz selectedDir

Extracting Tar Archives

1. Extract a Tar Archive

To extract the contents of a tar archive, use the tar -xvpf command:

1
tar -xvpf file.tar

2. Extract a Gzipped Tar Archive

To extract the contents of a gzipped tar archive, use tar -xzvpf:

1
tar -xzvpf file.tar.gz

3. Extract with Specific Folder

To extract the contents of a gzipped tar archive into a specific folder, use the -C option:

1
tar -C ~/example.com -xzvpf file.tgz docker-composes/example.com

Listing Contents of a Tar Archive

To list the contents of a tar archive, use the tar -tvf command:

1
tar -tvf file.tar.gz

These commands should cover most of your needs for creating, extracting, and listing the contents of tar compressed archives in Linux. Make sure to replace file.tar, folderToCompress, folderPath, selectedDir, and other placeholders with your actual file and directory names as needed.

0%