Commands.page Logo

How to Archive Directory Excluding Node Modules in Ubuntu

This guide explains how to create a compressed archive of a project directory on Ubuntu while excluding the heavy node_modules folder. You will learn the specific tar command flags required to skip unnecessary files, saving space and speeding up the backup process. Follow the steps below to efficiently package your code without the dependency bloat.

The Tar Command

To create a gzip-compressed archive while ignoring the node_modules directory, use the tar utility with the --exclude flag. Open your terminal and navigate to the parent directory of the folder you wish to archive.

Run the following command:

tar --exclude='node_modules' -czvf project-backup.tar.gz ./project-folder

Command Breakdown

Understanding the flags ensures you can modify the command for future use:

Verifying the Archive

After creation, you can verify that the node_modules folder was not included by listing the contents of the archive. Run the following command:

tar -tzvf project-backup.tar.gz

Scan the output list. If the command was successful, you will not see any paths containing node_modules. This confirms your archive is clean and ready for transfer or storage.