When working with the command line in Linux, you may come across situations where you need to compress or archive a directory. The tar command is commonly used for this purpose. It allows you to create a single file that contains multiple files and folders. However, there may be instances when you want to exclude certain folders from the archive. In this article, we will learn how to use the tar command to create a compressed archive of a directory while excluding specific folders.
Understanding the Tar Command
The tar command is used to create, view, and extract archives. It is often used in conjunction with compression utilities like gzip or bzip2 to create compressed archives. The basic syntax of the tar command is as follows:
tar [options] [archive_name] [files/directories]
Here, options are additional flags or parameters that modify the behavior of the tar command, archive_name is the name of the resulting archive file, and files/directories are the files and directories you want to include in the archive.
Excluding Folders with the --exclude Option
The --exclude option allows you to specify patterns of files or directories that you want to exclude from the archive. The pattern can be a simple string or a regular expression. To exclude a specific folder, you need to provide the full path to that folder.
For example, let's say you have a directory named my_directory and you want to create a compressed archive of it while excluding a folder named my_directory/exclude_folder. You can use the following command:
tar --exclude='my_directory/exclude_folder' -czvf archive.tar.gz my_directory
Let's break down the command:
--exclude='my_directory/exclude_folder': This option tellstarto exclude the foldermy_directory/exclude_folderfrom the archive.-czvf: These options specify that we want to create a compressed archive (-c), use gzip compression (-z), display the progress (-v), and specify the name of the archive file (-f).archive.tar.gz: This is the name of the resulting archive file.my_directory: This is the directory we want to archive.
By running this command, tar will create a compressed archive file named archive.tar.gz that contains all the files and folders from my_directory, excluding the exclude_folder.
Excluding Multiple Folders
If you need to exclude multiple folders, you can use the --exclude option multiple times. Each exclusion pattern should be separated by a space.
For example, let's say you want to exclude both my_directory/exclude_folder1 and my_directory/exclude_folder2. The command would look like this:
tar --exclude='my_directory/exclude_folder1' --exclude='my_directory/exclude_folder2' -czvf archive.tar.gz my_directory
Here, we added two --exclude options, each specifying a different folder to exclude.
Excluding Folders Using Wildcards
You can also use wildcards to exclude multiple folders that match a certain pattern. The wildcard character * represents any number of characters.
For example, let's say you want to exclude all folders that start with the word exclude. You can use the following command:
tar --exclude='my_directory/exclude*' -czvf archive.tar.gz my_directory
This command will exclude all folders in my_directory that start with exclude.
The tar command is a powerful tool for creating compressed archives of directories. By using the --exclude option, you can easily exclude specific folders from the archive. Whether you need to exclude a single folder, multiple folders, or folders that match a certain pattern, the tar command provides the flexibility to tailor your archives to your specific needs.
| Reference | Link |
|---|---|
| GNU Tar Manual | https://www.gnu.org/software/tar/manual/tar.html |
| Tar Command in Linux with Examples | https://www.geeksforgeeks.org/tar-command-linux-examples/ |