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-folderCommand Breakdown
Understanding the flags ensures you can modify the command for future use:
--exclude='node_modules': Tells tar to ignore any file or folder matching this name.-c: Creates a new archive.-z: Compresses the archive using gzip.-v: Verbose mode, showing files as they are added.-f: Specifies the filename of the archive.
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.gzScan 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.