Introduction
This article will walk you through manually compressing a directory using the tar
command on the Linux command line. This is useful for backups, transfers, or archiving purposes.
Procedure
1. Identify the Directory Path
Before compressing, determine the full path of the directory you want to compress. You can use the pwd
command to display your current working directory:
pwd
Example output:
/home/username/myproject
2. Compress the Directory
Use the following command to create a compressed .tar.gz
archive:
tar -zcvf directory.tar.gz /path/to/directory
Explanation of the options:
-z
– Compress the archive usinggzip
.-c
– Create a new archive.-v
– Verbose output (lists the files being processed).-f
– Specifies the filename of the archive to create.
Example:
tar -zcvf myproject.tar.gz /home/username/myproject
This will create a compressed archive named myproject.tar.gz
in your current working directory.
Notes
- Ensure you have write permission in the directory where you’re creating the archive.
- The archive includes the full path unless you
cd
into the directory’s parent first:
cd /home/username
tar -zcvf myproject.tar.gz myproject
This results in a cleaner archive without full path references.
Conclusion
Using tar
is a quick and effective way to compress directories manually. It’s widely supported and efficient for both personal use and scripting.