Skip to content

Instantly share code, notes, and snippets.

@ApOgEE
Last active May 2, 2018 05:58
Show Gist options
  • Select an option

  • Save ApOgEE/48001f4dd85951ba42c6faf756e2d08a to your computer and use it in GitHub Desktop.

Select an option

Save ApOgEE/48001f4dd85951ba42c6faf756e2d08a to your computer and use it in GitHub Desktop.
Bash - Create tar.gz file with exclude list

Bash - How to create tar gz file with exclude list

Hi there...

I made this gists for my personal note as I keep on forgetting commands. Perhaps it may be useful for you too. Feel free to leave your comments.

Assume you already know the command to create tar gz archive. Here is the command to include a hidden file and exclude a list of files and folders.

$ tar -zcvf ../myfile.tar.gz * .one_hidden --exclude-from=../myexclude.list

Example Directory

apogee@usbdev:~/Documents/tartest$ du -ah
0	./dirtozip/dir1/in_dir_1_a.x
0	./dirtozip/dir1/in_dir_1_b.x
4.0K	./dirtozip/dir1
0	./dirtozip/file2.abc
0	./dirtozip/dir2/my_file_2.xyz
0	./dirtozip/dir2/my_file_3.xyz
0	./dirtozip/dir2/my_file_1.xyz
0	./dirtozip/dir2/my_file_4.xyz
4.0K	./dirtozip/dir2
0	./dirtozip/file1.abc
0	./dirtozip/.one_hidden
0	./dirtozip/.two_hidden
12K	./dirtozip
16K	.

Example Exclude list

apogee@usbdev:~/Documents/tartest/dirtozip$ cat ../myexclude.list
dir1
file1.abc
dir2/my_file_3.xyz

Example Usage

apogee@usbdev:~/Documents/tartest/dirtozip$ tar -zcvf ../myfile.tar.gz * .one_hidden --exclude-from=../myexclude.list
dir2/
dir2/my_file_2.xyz
dir2/my_file_1.xyz
dir2/my_file_4.xyz
file2.abc
.one_hidden

Example tar.gz Content

apogee@usbdev:~/Documents/tartest/dirtozip$ tar -tf ../myfile.tar.gz
dir2/
dir2/my_file_2.xyz
dir2/my_file_1.xyz
dir2/my_file_4.xyz
file2.abc
.one_hidden

Additonal Note

  • The file .one_hidden is an example of including one hidden file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment