The tar command in Linux is used to create, manipulate, and extract files from tape archives (tarballs). It is commonly used to create backup archives, compress multiple files, and transfer large amounts of data efficiently.
The basic syntax of the tar command is as follows:
tar [options] [archive_file] [file(s) or directory]Here’s a table explaining the main parameters and options of the tar command:
| Parameter/Option | Description |
|---|---|
| archive_file | Specifies the name of the archive file to be created or extracted. If not provided, it operates on standard input/output. |
| -c, –create | Create a new archive. |
| -x, –extract | Extract files from an archive. |
| -t, –list | List the contents of an archive. |
| -v, –verbose | Enable verbose mode, showing the processed files. |
| -f, –file=ARCHIVE | Use the specified file as the archive. |
| -z, –gzip | Filter the archive through gzip for compression (use with .tar.gz or .tgz extension). |
| -j, –bzip2 | Filter the archive through bzip2 for compression (use with .tar.bz2 or .tbz extension). |
| -r, –append | Append files to the end of an archive. |
| -u, –update | Only append files that are newer than the version in the archive. |
| -d, –diff | Compare the contents of an archive with the file system. |
| -C, –directory=DIR | Change to the specified directory before performing any operations. |
| –help | Display help and exit. |
| –version | Output version information and exit. |
Now, let’s see some examples of how to use the tar command:
- Create a tar archive from a directory:
tar -cvf archive.tar directory/This will create a tar archive named archive.tar containing the contents of the directory/ directory.
- Extract files from a tar archive:
tar -xvf archive.tarThis will extract the contents of archive.tar to the current directory.
- Create a gzip-compressed tar archive:
tar -czvf archive.tar.gz directory/This will create a gzip-compressed tar archive named archive.tar.gz from the contents of the directory/ directory.
- Extract files from a gzip-compressed tar archive:
tar -xzvf archive.tar.gzThis will extract the contents of the gzip-compressed tar archive archive.tar.gz to the current directory.
- Append files to an existing tar archive:
tar -rvf archive.tar additional_fileThis will append additional_file to the existing archive.tar.
- List the contents of a tar archive:
tar -tvf archive.tarThis will list the contents of archive.tar without extracting them.
The tar command is a powerful tool for creating, extracting, and managing archive files, making it a crucial utility for file backups and data transfer. When used in combination with compression utilities like gzip or bzip2, it can efficiently store and transfer large amounts of data.