tar¶
Operation Flags¶
These tell the command what main action to take. You generally use only one of these at a time.
- -c (–create): Creates a new archive file.
- -x (–extract): Extracts the contents of an existing archive.
- -t (–list): Lists the contents of an archive without extracting them (useful for inspecting a file before unpacking it).
- -r (–append): Appends files to the end of an existing archive.
- -u (–update): Appends files only if they are newer than the copy in the archive.
General Options¶
These modify how the operation is performed or provide feedback.
- -v (–verbose): Displays the progress in the terminal, showing each file as it is processed. Without this, the command runs silently.
- -f (–file): Specifies the filename of the archive. This is mandatory for almost all operations involving a specific file.
- -p (–preserve-permissions): Retains the original file permissions (users, groups, access modes) of the archived data.
- -C – Change to a specified directory before performing the operation (useful when extracting archives to a target location).
Compression Flags¶
These tar flags automatically pass the archive through a compression algorithm.
- -z: Compress using gzip (fast, widely compatible). Creates .tar.gz.
- -j: Compress using bzip2 (better compression, slower). Creates .tar.bz2.
- -J: Compress using xz (high compression, very slow). Creates .tar.xz.
Compressing a folder¶
tar -czf target_file.tar.gz source_folder
Compressing files¶
tar -czf target_file.tar.gz source_file1 source_file2 source_file3
Extracting to current directory¶
tar -xf source_file.tar.gz
Extracting to specific directory¶
tar -xf source_file.tar.gz -C /path/to/destination
Linux tar Command Guide: How to Use It | Contabo Blog