Commands.page Logo

How to Tar Directory Excluding Hidden Files in Ubuntu

This article provides a quick method for creating a compressed archive of a directory on Ubuntu while ignoring all hidden files. You will learn the exact tar command syntax needed to filter out configuration files and system data that start with a dot, resulting in a cleaner backup package.

The Tar Command Structure

To create a gzip-compressed archive while excluding hidden files, use the tar utility with the --exclude flag. Hidden files in Linux are identified by a leading dot (.). The following command structure achieves this:

tar --exclude='.*' -czvf archive_name.tar.gz /path/to/directory

Understanding the Flags

Each option in the command serves a specific function to ensure the archive is created correctly:

Practical Example

If you want to back up a folder named projects located in your home directory without including hidden files like .gitignore or .config, run the following command in your terminal:

tar --exclude='.*' -czvf projects_backup.tar.gz ~/projects

This generates a file named projects_backup.tar.gz containing only the visible files within the projects directory and its subdirectories.

Verifying the Archive

After creating the archive, you can list its contents to confirm that no hidden files were included. Use the -t flag to list the contents without extracting them:

tar -tzvf projects_backup.tar.gz

Review the output to ensure no filenames beginning with a dot appear in the list. This confirms your exclusion pattern worked correctly.