To exclude certain files or directories when creating a tarball (a compressed archive file) using the tar
command, you can use the --exclude
option. This option allows you to specify patterns or names of files or directories that should be excluded from the tarball.
For example, to create a tarball of the current directory, excluding all files with the .tmp
extension and the temp
directory, you can use the following command:
tar -cf my_tarball.tar --exclude "*.tmp" --exclude "temp" .
This will create a tarball called my_tarball.tar
that includes all files and directories in the current directory, except for files with the .tmp
extension and the temp
directory.
You can use the --exclude
option multiple times to exclude multiple patterns or names. For example:
tar -cf my_tarball.tar --exclude "*.tmp" --exclude "temp" --exclude "*.bak" .
This will create a tarball that excludes files with the .tmp
and .bak
extensions, as well as the temp
directory.
You can also use the --exclude-from
option to specify a file containing a list of patterns or names to exclude. For example:
tar -cf my_tarball.tar --exclude-from exclude.txt .
This will create a tarball that excludes any files or directories specified in the exclude.txt
file. The file should contain one pattern or name per line.
Keep in mind that the --exclude
and --exclude-from
options are only supported by certain versions of the tar
command. If you are using an older version of tar
, you may need to use a different method to exclude certain files or directories. Consult the documentation for your version of tar
for more information.