Commands.page Logo

How to Archive a Directory to tar.gz in Ubuntu Linux

This article provides a concise explanation of the command required to archive and compress a directory into a tar.gz file on Ubuntu. It covers the specific syntax, flag explanations, and a practical example to ensure you can create compressed archives quickly via the terminal.

To archive a directory into a tar.gz file, use the tar command with specific flags enabled for creation and gzip compression. The standard command structure is as follows:

tar -czvf archive_name.tar.gz /path/to/directory

In this command, -c tells tar to create a new archive, -z compresses the data using gzip, -v enables verbose mode to show progress, and -f specifies the filename of the archive. Replace archive_name.tar.gz with your desired file name and /path/to/directory with the actual path of the folder you wish to archive.

For example, to compress a folder named projects located in your home directory into a file called backup.tar.gz, you would run:

tar -czvf backup.tar.gz ~/projects

Once the command executes, the terminal will list the files being added, and the compressed archive will appear in your current working directory.