Linux provides various utilities to compress and combine files, with the tar command being one of the most commonly used. tar stands for "tape archive", and is a versatile utility for creating and manipulating compressed archive files. It is a powerful tool that can be used for a variety of tasks, from creating backups of important files to compressing multiple files into a single archive.
The basic syntax of the tar command is as follows:
tar [options] archive_name files_or_directories
Here, options are the various flags that can be used to modify the behavior of the command, such as -c for creating a new archive, -x for extracting files from an archive, and -z for compressing or decompressing an archive using gzip.
archive_name is the name of the archive file that will be created or operated on, and files_or_directories is a list of one or more files or directories that will be included in the archive.
For example, to create a new tarball of a directory called my_folder, you could use the following command:
tar -czvf my_folder.tar.gz my_folder/
Here, the -c flag indicates that a new archive should be created, the -z flag specifies that the archive should be compressed with gzip, the -v flag enables verbose output so that the user can see which files are being added to the archive, and the f flag specifies the name of the archive file.
To extract the contents of the tarball, you can use the following command:
tar -xzvf my_folder.tar.gz
Here, the -x flag indicates that the files should be extracted from the archive, and the other flags have the same meanings as before.
Overall, the tar command is a versatile and powerful tool that is essential for managing and manipulating archive files in Linux.
Simple Examples
# Create archive.tar from files foo and bar.
tar -cf archive.tar foo bar
# List all files in archive.tar verbosely.
tar -tvf archive.tar
# Extract all files from archive.tar.
tar -xf archive.tar
# Compress directory baz into archive.tar.gz
tar -czvf archive.tar.gz baz/
-c, --create create a new archive
-t, --list list the contents of an archive
-x, --extract, --get extract files from an archive
-v, --verbose verbosely list files processed
-f, --file=ARCHIVE use archive file or device ARCHIVE
-z, --gzip, --ungzip use the gzip compression format
Difference between TAR and ZIP files
The tar file is a file format in itself designed for tape archives. This format can then be compressed using (for example) gzip or bzip2 compression formats. When you extract a compressed tar file, you effectively uncompress it, then extract the original files from the uncompressed tar file.
The zip tool is a completely different thing. It takes a bunch of files and combines them into a single compressed file. With totally different algorithms.
The tar tool is just combining several files into a single file without any compression.
The gzip tool is just compressing a single file.
If you want to have both, you just combine both tools resulting in a .tar.gz file.
For more info:
Visit the man page for tar on your shell:
man tar
Top comments (0)