Linux tar examples: bundle multiple files & directories
Yes, the Linux tar command bundles multiple files and directories into a single archive file, and it can also compress that archive. You use tar with a set of options, the archive filename, and the list of items to include; the result is one file that contains all of them, ready to store, transfer, or back up.
What tar actually does
The name tar stands for tape archive, a tool originally designed to write data to sequential storage like magnetic tape. Today it is used almost entirely for creating and extracting archive files on disk. An archive is a single file that contains other files and directories, plus metadata such as permissions, ownership, and timestamps. By itself, tar does not compress anything; it only concatenates the contents. Compression is added by combining tar with a compression program such as gzip, bzip2, or xz.
The most common invocation is tar -czvf archive.tar.gz /path/to/files. The options tell tar to create an archive (-c), compress it with gzip (-z), be verbose and list the files being added (-v), and use the filename that follows (-f). The resulting file is often called a tarball.
Basic examples: creating archives
Here are the everyday commands you will use to bundle files and directories:
- Create an uncompressed archive:
tar -cf archive.tar file1.txt file2.txt dir1/ - Create a gzip-compressed archive:
tar -czf archive.tar.gz file1.txt dir1/ - Create a bzip2-compressed archive:
tar -cjf archive.tar.bz2 file1.txt dir1/ - Create an xz-compressed archive:
tar -cJf archive.tar.xz file1.txt dir1/
Notice that the compression option letter changes: -z for gzip, -j for bzip2, -J for xz. You can also use the modern syntax without the dash, e.g. tar czf archive.tar.gz dir1/, which works on most Linux systems.
Extracting archives
To unpack an archive, replace -c (create) with -x (extract). The compression option is usually the same as when creating, but tar can often detect the compression automatically if you omit it. For example:
- Extract a gzip archive:
tar -xzf archive.tar.gz - Extract a bzip2 archive:
tar -xjf archive.tar.bz2 - Extract an xz archive:
tar -xJf archive.tar.xz - Extract to a specific directory:
tar -xzf archive.tar.gz -C /target/directory
The -C option changes to the specified directory before extracting, which is useful when you want to unpack into a particular location. Without it, files are extracted into the current working directory, which can overwrite existing files with the same names.
Listing archive contents without extracting
Before extracting, you often want to see what is inside. Use -t (list) instead of -x:
tar -tzf archive.tar.gz
This prints the names of all files and directories in the archive. You can also use tar -tvzf archive.tar.gz to get a detailed listing with permissions, sizes, and modification times, similar to ls -l.
Common options and their meaning
Understanding the core options helps you adapt commands to your needs:
-c— create a new archive-x— extract files from an archive-t— list the contents of an archive-f archive— specify the archive file name (must be followed by the name)-v— verbose; show the files being processed-z— filter through gzip-j— filter through bzip2-J— filter through xz-C dir— change to directory before performing the operation--exclude=pattern— exclude files matching a pattern
You can combine short options, so -czvf is equivalent to -c -z -v -f. The -f option must be followed by the archive name, so it is usually placed last among the options.
How to choose the right compression
Compression reduces file size but takes time and CPU. The trade-off is:
- gzip — fast, widely compatible, good compression. Best for everyday use.
- bzip2 — slower than gzip but usually compresses better, especially for text files.
- xz — the slowest and most CPU-intensive, but typically gives the smallest archives. Good for long-term storage or distribution.
If you are not sure, use gzip. It is the most portable and the default on virtually every Linux distribution. For very large directories or logs, xz can save significant space, but the time cost may not be worth it for frequent backups.
Common pitfalls and how to avoid them
Several mistakes trip up new tar users:
- Forgetting the
-foption: If you omit it, tar writes to standard output or reads from standard input, not from a file. Always include-fwith the archive name. - Using absolute paths: If you archive
/home/user/dir, tar stores the path ashome/user/dir(without the leading slash). When extracting, it recreates that path relative to the current directory. This is intentional for safety, but it can confuse users expecting the original absolute path. - Overwriting files on extraction: By default, tar overwrites existing files with the same names. Use
--keep-old-filesto avoid that, or extract to a fresh directory with-C. - Not excluding unnecessary files: When archiving a directory, you may inadvertently include large temporary files or logs. Use
--exclude='*.log'or--exclude=dir/cacheto keep the archive lean. - Forgetting to use
sudowhen needed: If you archive system directories like/etcor/var, you may need root privileges to read all files. Run the command withsudo.
Frequently Asked Questions
Does tar compress files by default?
No. The basic tar command only bundles files; it does not compress them. To compress, you must add a compression option like -z (gzip), -j (bzip2), or -J (xz). The resulting file will have a corresponding extension such as .tar.gz or .tar.xz.
How do I exclude a directory when creating a tar archive?
Use the --exclude option. For example, tar -czf backup.tar.gz /home/user --exclude=/home/user/cache will archive the home directory but skip the cache folder. You can repeat the option to exclude multiple patterns.
What is the difference between .tar.gz and .tgz?
They are the same format. The .tgz extension is just a shortened version of .tar.gz for systems with filename length limits. Both are gzip-compressed tar archives, and you can extract them with the same command: tar -xzf file.tgz.
How do I extract a single file from a large archive?
You can specify the file name after the archive name: tar -xzf archive.tar.gz path/to/file.txt. This extracts only that file. To find the exact path, first list the contents with tar -tzf archive.tar.gz.
Can I update an existing tar archive?
Yes, with the -r (append) option: tar -rf archive.tar newfile.txt adds a file to the end of an uncompressed archive. However, this does not work with compressed archives (e.g., .tar.gz) because the compression stream cannot be appended to. For compressed archives, you would need to recreate the archive.
Why does tar preserve file permissions and ownership?
tar stores metadata for each file, including Unix permissions, user and group IDs, and timestamps. When extracting as root, it restores these exactly. When extracting as a regular user, ownership is set to the current user, but permissions are preserved. This makes tar useful for backups and system migration.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.