Linux Tar Command: Create, Extract, and Compress Archives

Tar is a widely used Linux archiving tool—short for “tape archive” that bundles multiple files and directories into a single archive (a tarball) while preserving paths, permissions, ownership, and timestamps, and it can also compress or extract those archives for efficient backup, distribution, and recovery.

To create a compressed tar (tar.gz or tgz) file, run

tar -czvf backup.tgz FOLDER

Replace FOLDER with name of the directory you need to compress. It will create a backup.tgz file with all content of the specified directory.

To extract a tar/tar.gz/tgz file, run

tar xvf file.tar.gz

Uncompress tar.bz2 file

tar -jxvf ncftp-3.2.2-src.tar.bz2

Create a tar file

tar -cvf file.tar FOLDER_NAME

Example

tar -cvf backup.tar public_html

Once backup.tar file is created, you can make it tar.gz with command

gzip backup.tar

Exclude a folder from tar file

To exclude a folder, you can use –exclude option.

tar cvf backup.tar --exclude=public_html/uploads --exclude=public_html/wordpress public_html

List content of a tar file

tar --list --verbose --file=BACKUP.tar.gz

If you want to exclude a folder from the file list, use

tar --list --verbose --file=BACKUP.tar.gz --exclude=home/haridy/Maildir

This will exclude all files that are inside folder “home/haridy/Maildir”. You can use multiple –exclude if required.

Using tar over SSH Session
Split Large file into smaller files
How to view the contents of tar.gz file

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *