Skip to content

Instantly share code, notes, and snippets.

@redspider
Created July 31, 2012 05:47
Show Gist options
  • Save redspider/3214075 to your computer and use it in GitHub Desktop.
Save redspider/3214075 to your computer and use it in GitHub Desktop.
Tar extraction example

Demonstration of a partial tar extraction.

First we set up a dir for testing:

$ ls -lR
.:
total 4
drwxr-xr-x 5 richard richard 4096 2012-07-31 17:46 root_dir

./root_dir:
total 12
drwxr-xr-x 2 richard richard 4096 2012-07-31 17:46 dir1
drwxr-xr-x 2 richard richard 4096 2012-07-31 17:46 dir2
drwxr-xr-x 2 richard richard 4096 2012-07-31 17:46 dir3

./root_dir/dir1:
total 0
-rw-r--r-- 1 richard richard 0 2012-07-31 17:46 file1
-rw-r--r-- 1 richard richard 0 2012-07-31 17:46 file2
-rw-r--r-- 1 richard richard 0 2012-07-31 17:46 file3

./root_dir/dir2:
total 0
-rw-r--r-- 1 richard richard 0 2012-07-31 17:46 file1

./root_dir/dir3:
total 0
-rw-r--r-- 1 richard richard 0 2012-07-31 17:46 file1
-rw-r--r-- 1 richard richard 0 2012-07-31 17:46 file2

Then we tar it up

$ tar -cvzf test_tar.tar.gz root_dir
root_dir/
root_dir/dir3/
root_dir/dir3/file2
root_dir/dir3/file1
root_dir/dir1/
root_dir/dir1/file3
root_dir/dir1/file2
root_dir/dir1/file1
root_dir/dir2/
root_dir/dir2/file1

Then we list the contents

$ tar -tzf ./test_tar.tar.gz 
root_dir/
root_dir/dir3/
root_dir/dir3/file2
root_dir/dir3/file1
root_dir/dir1/
root_dir/dir1/file3
root_dir/dir1/file2
root_dir/dir1/file1
root_dir/dir2/
root_dir/dir2/file1

Then we extract only dir3

$ tar -xvzf test_tar.tar.gz root_dir/dir3
root_dir/dir3/
root_dir/dir3/file2
root_dir/dir3/file1

Then we extract only dir1, file 2

$ tar -xvzf test_tar.tar.gz root_dir/dir1/file2
root_dir/dir1/file2

Then we extract every file called "file2"

$ tar -xvzf test_tar.tar.gz --wildcards '*/file2'
root_dir/dir3/file2
root_dir/dir1/file2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment