tar Bundle and unbundle files into archives
Packs many files and folders into one archive file, and unpacks them again. Add compression and you get the familiar .tar.gz or .tgz file. You use it for backups, for sending a folder as one file, and for unpacking downloads.
Synopsis
tar -c [-z] -f archive [files ...]
tar -x [-z] -f archive [-C directory]
tar -t [-z] -f archive
Common options
| Option or form | What it does |
|---|---|
-c | Create a new archive from the files and folders you name |
-x | Extract files from an archive |
-t | List the contents of an archive without extracting anything |
-f archive | The archive file to write or read. It must come right before the file name, so write -czf backup.tgz, not -cfz backup.tgz |
-v | Verbose: print each file as it is processed |
-z | Compress with gzip when creating, or decompress gzip when reading. Use it for .tar.gz and .tgz files |
-C directory | Change to this directory first. When extracting, files go there instead of the current folder |
--exclude pattern | Skip files and folders whose names match the pattern. Quote the pattern so the shell does not expand it |
Examples
tar -czf backup.tgz docs && tar -tzf backup.tgz
tar -czf backup.tgz docs
tar -czf backup.tgz docs && mkdir restore && tar -xzf backup.tgz -C restore && ls restore
docs
tar -czf photos.tgz photos && mkdir out && tar -xf photos.tgz -C out && ls out/photos
a.jpg
b.jpg
tar --exclude='*.jpg' -czf site.tgz docs photos && tar -tzf site.tgz
tar -czf html.tgz -C /var/www html
Notes
- The leading dash is optional in the first word of options, so tar czf backup.tgz docs and tar -czf backup.tgz docs do the same thing. You will see both. The dash form is clearer, and it is the one used here.
- On macOS (bsdtar), tar -xf backup.tgz works without -z, because tar detects gzip compression itself when reading. -z is still needed when creating a compressed archive. Older or minimal GNU tar versions may need -z, so keeping it is safe everywhere.
- tar extracts into the current directory, and an archive without a top-level folder scatters files all over it. Look first with tar -tf, or extract into a new empty folder with -C.
- tar removes a leading / from names when it creates an archive (it prints "Removing leading '/' from member names"), so an archive of /etc/hosts holds etc/hosts. Archives from other people may contain absolute paths or .. entries. Check them with -t before extracting.
- Extraction overwrites files that already exist with the same name, with no prompt. Do not extract over a folder you want to keep.
See also
Last reviewed 2026-10-07.