Quick Summary: Use the
--excludeflag followed by the target directory name right before your file arguments when running thetarcommand to omit specific folders from your compressed archive.
Introduction
When backing up a website or archiving project files via the command line, your directory often contains heavy or sensitive folders that you simply do not need to bundle up. Using the standard tar zcvf mywebsite.tar.gz * command grabs everything, but you can easily tailor your backup by dropping unwanted directories.
How do you exclude directories from a tar archive?
To leave out a specific folder, pass the --exclude flag before your wildcard or file selection:
Bash
tar --exclude='directory_to_exclude' -zcvf mywebsite.tar.gz *
Explanation of the Command
--exclude='...': Tells the tar utility to skip any file or folder matching the specified name or path during compression.-zcvf: Compresses the archive using gzip (z), creates a new archive (c), displays verbose output (v), and specifies the filename (f).
Common Reasons to Exclude Directories
When managing server backups or code migrations, you will often want to omit certain parts of your file tree. Here are three common scenarios:
- You might have a media folder that you don’t need to archive: Large asset directories containing user uploads, images, or videos can bloat backup sizes unnecessarily, especially if those assets are already synced elsewhere.
- You might just want to back up the code for the website: Keeping your archives strictly focused on source code ensures fast, lightweight deployments without non-essential assets.
- You might have a config folder you want to exclude in your backups: Environment files or local configuration directories containing sensitive credentials should often be handled securely outside of standard code archives.
Step-by-Step Configuration Guide
- Navigate to your web root: Open your terminal and change directory to the root of the project you want to archive.
Bashcd /var/www/mywebsite - Run the tar command with multiple exclusions: If you need to omit more than one directory (such as a media folder and a config folder), chain the
--excludeflags together.
Bashtar --exclude='media' --exclude='config' -zcvf mywebsite.tar.gz * - Verify the archive contents: Confirm that your excluded directories were successfully left out of the compressed file.
Bashtar -ztf mywebsite.tar.gz | head -n 20
Frequently Asked Questions (FAQ)
Can I exclude a directory nested inside a subdirectory?
Yes, you can provide a relative path to the flag, such as --exclude='public/media', to target specific nested folders without excluding folders with the same name elsewhere.
Does the order of the flags matter?
Yes. The --exclude flag must be placed before the archive filename and the source files (*) you are bundling.
Conclusion
Excluding unwanted directories keeps your backups lean, fast, and secure. By leveraging multiple --exclude parameters, you can fine-tune your archiving workflow to include only what matters.
Would you like to write an automated shell script to run these backups on a regular cron schedule?
Check out this post about creating a backup using tar in a bash script.
![]()
One Comment “How to Exclude Directories When Creating Tar Archives in Linux.”