tar Command Builder
Create, extract and list tar archives without looking up the flags.
About this tool
Choose whether to create, extract or list an archive, pick the compression (gzip, bzip2, xz or zstd), add files and exclusions, and copy the tar command with every flag explained.
How to use it
- Choose Create, Extract or List contents.
- Pick the compression; the archive name gets the matching extension.
- For a new archive, list the files and folders to include and any patterns to exclude.
- Copy the command and read what each flag does.
Questions
- How do I extract a .tar.gz file?
- Run tar -xzvf file.tar.gz. Add -C folder to extract somewhere else. Modern GNU tar and the tar on macOS detect the compression themselves, so tar -xvf file.tar.gz works too.
- Which compression should I choose?
- gzip is the safe default that opens everywhere. zstd is usually both faster and smaller, if every machine that opens the file has a recent tar. xz gives the smallest files but is the slowest.
- Why does -f have to be last in -czvf?
- f takes the next word as the archive name. In -czfv, tar would treat v as the file name. Keep f at the end of the group of letters.
- What does --strip-components do?
- It removes leading folders from every path while extracting. Archives often contain a top folder like project-1.2/; --strip-components=1 puts its contents straight into the destination.
- How do I see what is inside without extracting?
- Use List contents, which gives tar -tvf archive.tar.gz. It prints every file with its size and date and changes nothing.
- How do I add files to an existing tar.gz?
- You can’t append to a compressed archive. tar -rf adds files only to an uncompressed .tar. For a .tar.gz, either create the archive again with the extra files, or decompress it with gunzip, run tar -rf backup.tar newfile, and compress it again with gzip.
- Why does tar say “Not found in archive”?
- The path you asked for doesn’t match a member name exactly. Run tar -tzf archive.tar.gz and copy the path from its output: archives made with tar -czf x.tar.gz . store every name with a leading ./, and many have a top folder. Patterns also need --wildcards in GNU tar.
- How do I stop macOS adding ._ files to a tar archive?
- The tar on macOS (bsdtar) stores extended attributes as extra ._ files, which show up as clutter on Linux. Create the archive with --no-mac-metadata, or set COPYFILE_DISABLE=1 before the command: COPYFILE_DISABLE=1 tar -czf backup.tar.gz project. On Linux and with GNU tar, nothing changes.
How to create a tar.gz archive of a folder
Run tar -czvf backup.tar.gz src docs package.json to pack the listed files and folders. Paths are stored as you type them, so use relative paths. To store a folder without the path above it, change directory first with -C: tar -czf site.tar.gz -C /var/www html. GNU tar and bsdtar remove a leading / from absolute paths, so the archive can’t overwrite system files when extracted.
To leave things out, add --exclude=node_modules or --exclude="*.log", and quote patterns so the shell doesn’t expand them. A pattern without a slash matches that name at any depth. In GNU tar, put the --exclude options before the file list, because they only apply to files listed after them. Write the archive outside the folder you are packing, so tar doesn’t try to pack the archive into itself.
tar.gz vs zip: which archive format to use?
tar keeps Unix permissions, owners and symlinks, which matters for scripts and deployments. It compresses all files as one stream, so many small similar files shrink better than in zip. The cost is no random access: listing or extracting one file means reading through the archive. zip compresses each file separately and opens natively on Windows, though Windows 10 and later also ship tar.
.tgz is just a shorter name for .tar.gz, with exactly the same content. .tar.bz2 and .tar.xz trade speed for smaller files, and .tar.zst is fast and small but needs GNU tar 1.31 or newer, or a recent bsdtar, with the --zstd flag. Both GNU tar and bsdtar also accept -a, which picks the compression from the archive’s file extension when creating.
How to extract a single file from a tar archive
Name the file after the archive: tar -xzf backup.tar.gz src/config.yml. The path must match exactly what tar -tzf backup.tar.gz prints, including any leading ./ or top folder. GNU tar needs --wildcards to use patterns like "*.yml", while bsdtar on macOS accepts them directly. Add -O to print the file to the terminal instead of writing it: tar -xzOf backup.tar.gz src/config.yml.
Before extracting an archive from someone else, list it. Some archives have no top folder and spill hundreds of files into the current folder, and existing files with the same names are overwritten without asking. Extract into a new, empty folder instead: mkdir out, then tar -xzf archive.tar.gz -C out. To check a download first, compare its SHA-256 with the Hash Generator or sha256sum.
Something not working, or a result looks wrong? Report a problem with this tool