How to Compress Directory Excluding .git Folders in Ubuntu
This article provides a straightforward method for compressing project directories on Ubuntu while automatically excluding .git folders. By removing version control data, you reduce archive size and protect sensitive repository history. The following steps detail the exact command-line syntax needed to achieve this using the standard tar utility.
Use the Tar Command with Exclude Flag
The most efficient way to compress a directory while ignoring
specific subfolders is using the tar command with the
--exclude option. This built-in utility allows you to
define patterns for files or directories that should not be included in
the final archive.
Open your terminal and navigate to the parent directory containing the folder you wish to compress. Run the following command:
tar --exclude='.git' -czvf project-archive.tar.gz /path/to/your/directoryUnderstanding the Command Flags
Each flag in the command serves a specific function to ensure the
process runs correctly. The --exclude='.git' parameter
tells tar to skip any directory named .git. The -c flag
creates a new archive, while -z compresses it using gzip.
The -v flag enables verbose output so you can see which
files are being added, and -f specifies the filename of the
resulting archive.
Verifying the Archive
After creating the archive, you can verify that the .git folders were successfully omitted. List the contents of the tar file using the following command:
tar -tzvf project-archive.tar.gzScan the output for any references to .git. If none appear, your directory has been compressed successfully without any version control data. You can now share or store the lightweight archive securely.