tar is the most widely used compression/decompression tool in Linux. The name tar is short for ‘Tape Archive’, and it was originally created for tape backups. However, it is now the most common tool used to bundle and compress files.
Essential Basic Options You Should Know
tar commands are broadly divided into action specification options and action modifiers. All tar commands use a combination of these two types of options.
Action Specification Options
- c: Create a new archive (file)
- x: Extract an archive
- t: List the contents of an archive
- r: Append files to an archive
- u: Update files in an archive
Action Modifier Options
- f: Specify the file name (almost always used)
- v: Print the progress of processing
- z: Use gzip compression (.tar.gz)
- j: Use bzip2 compression (.tar.bz2)
- J: Use xz compression (.tar.xz)
Frequently Used Options
Compression
|
|
Decompression
|
|
Listing Files
|
|
Options That Are Really Useful to Know
Path Related
- -C : Run from a different directory
- -P : Keep absolute paths
- –strip-components=N : Remove the top N directories when extracting
File Selection
- –exclude : Exclude specific files/directories
- –exclude-from : Read a list of files to exclude from a file
- –wildcards : Use wildcard patterns
Attribute Preservation
- -p : Preserve file permissions
- –same-owner : Preserve ownership information
- –numeric-owner : Preserve UID/GID
Conclusion
tar is used for a variety of purposes in addition to file compression. In system backups, the -p option is important because it preserves file permissions and ownership information. When distributing software, xz compression is mainly used because it has a high compression ratio.
In the case of web applications with complex directory structures, it is important to exclude unnecessary files such as logs and caches when backing up. Such files can be excluded with the –exclude option.