Linux Tar Command: Complete Guide & Examples
Tar is a commonly used file packaging and compression tool in the Linux system. It allows multiple files or directories to be packaged into a single file and can also compress the packaged files. Here is a detailed tutorial on the tar packaging command.
- Pack up files or directories:
- Create a tar archive with the specified file or directory.
- For example, to create a file named backup.tar containing all files and directories in the current directory:
- Create a backup file called “backup.tar” containing all files in the current directory.
- View the contents of the packaged file:
- List the contents of the tar file using the command tar -tvf
. - For example, to view the contents of the backup.tar file:
- List the contents of the backup.tar file
- Unzip the compressed file.
- extract the files from the compressed file named
- For example, to decompress the backup.tar file:
- Extract the contents of the backup.tar file.
- Package and compress using gzip:
- Create a compressed file named
containing the specified files or directories. - For example, to compress all files and directories in the current directory into a tar.gz file named backup.tar.gz:
- Create a compressed backup file called backup.tar.gz including all files and folders.
- Extract and decompress tar.gz file.
- Unpack the file by using the command: tar -xzvf
- For example, to unpack and decompress the backup.tar.gz file:
- Extract the contents of backup.tar.gz
- Package and compress with bzip2:
- Create a .tar.bz2 file with the specified name that includes the specified files or directories.
- For example, to pack all files and directories in the current directory and compress them with bzip2, creating a file named backup.tar.bz2:
- Create a compressed tar archive called backup.tar.bz2 of all files in the current directory.
- Unzip and decompress the tar.bz2 file.
- Unpack the compressed file by running the following command: tar -xjvf
- For example, to decompress and extract the backup.tar.bz2 file:
- Extract the contents of the backup.tar.bz2 file.
Here is a detailed tutorial on the tar packaging command. When using the tar command, please choose the appropriate options and parameters according to your actual needs.