tar command
Create and extract .tar, .tar.gz, .tar.bz2 and .tar.xz archives.
- Archives & compression
- Package tar
- 10 options · 10 examples
Syntax
tar [options] [archive] [files…]Provided by the tar package. Run man tar for the full manual.
Common options
| Option | What it does |
|---|---|
-c | Create an archive |
-x | Extract an archive |
-t | List the contents |
-f FILE | Archive file name (put it last in the group) |
-z | gzip (.tar.gz, .tgz) |
-j | bzip2 (.tar.bz2) |
-J | xz (.tar.xz) |
-v | List files as they are processed |
-C DIR | Extract into or archive from DIR |
--exclude=PATTERN | Skip matching files |
Examples
Extract a .tar.gz
tar -xzf archive.tar.gzExtract into a directory
tar -xzf archive.tar.gz -C /opt/appExtract a .tar.xz
tar -xJf linux.tar.xzExtract a .tar.bz2
tar -xjf backup.tar.bz2Create a .tar.gz
tar -czf backup.tar.gz /var/www/htmlCreate with exclusions
tar -czf site.tar.gz --exclude="*.log" --exclude=node_modules project/List the contents
tar -tzf archive.tar.gzExtract one file
tar -xzf archive.tar.gz path/to/file.txtCreate a .tar.xz
tar -cJf data.tar.xz data/Back up with a date in the name
tar -czf "backup-$(date +%F).tar.gz" /etc
No example matches. Check the options table or search the guides.
Tips
- Modern GNU tar detects compression when extracting, so
tar -xf file.tar.gzalso works. - Remember it as e**x**tract **z**ipped **f**ile:
-xzf; **c**reate **z**ipped **f**ile:-czf.
tar guides
- How to Compile and Install .tar.gz or .tar.bz2 Source Package on Ubuntu Linux Commands · Updated Apr 2025
- tar Command in Linux with Practical Examples Linux Commands · Updated Apr 2025
- How to Extract tar.gz File in Linux Linux Commands · Updated Apr 2025
- How to Extract a Tar.bz2 File Linux Commands · Updated Apr 2025
- How To Extract Zip, Gz, Tar, Bz2, 7z, Xz, and Rar Files in Linux Linux Commands · Updated Apr 2025
- How to List Archive File Contents in TAR/TAR.GZ/TAR.BZ2 Linux Commands · Updated Apr 2025
- How to Unpack .xz and tar.xz Files: A Simple Guide Linux Commands · Updated Apr 2025
