How to tar and untar directory

How to

1. at first we will create testing environment. There will be 10 directories test[1-10] with files test[a-g]

# for i in {1..10}; do mkdir test$i; for f in {a..g}; do echo "hello" > test$i/file$f; done; done

2. we will check if everything was done fine.

# ll test*
test1:
total 28
-rw-r--r-- 1 root root 6 Aug 15 12:37 filea
-rw-r--r-- 1 root root 6 Aug 15 12:37 fileb
-rw-r--r-- 1 root root 6 Aug 15 12:37 filec
-rw-r--r-- 1 root root 6 Aug 15 12:37 filed
-rw-r--r-- 1 root root 6 Aug 15 12:37 filee
-rw-r--r-- 1 root root 6 Aug 15 12:37 filef
-rw-r--r-- 1 root root 6 Aug 15 12:37 fileg

test2:
total 28
-rw-r--r-- 1 root root 6 Aug 15 12:37 filea
-rw-r--r-- 1 root root 6 Aug 15 12:37 fileb
-rw-r--r-- 1 root root 6 Aug 15 12:37 filec
-rw-r--r-- 1 root root 6 Aug 15 12:37 filed
-rw-r--r-- 1 root root 6 Aug 15 12:37 filee
-rw-r--r-- 1 root root 6 Aug 15 12:37 filef
-rw-r--r-- 1 root root 6 Aug 15 12:37 fileg

.
.
.

test10:
total 28
-rw-r--r-- 1 root root 6 Aug 15 12:37 filea
-rw-r--r-- 1 root root 6 Aug 15 12:37 fileb
-rw-r--r-- 1 root root 6 Aug 15 12:37 filec
-rw-r--r-- 1 root root 6 Aug 15 12:37 filed
-rw-r--r-- 1 root root 6 Aug 15 12:37 filee
-rw-r--r-- 1 root root 6 Aug 15 12:37 filef
-rw-r--r-- 1 root root 6 Aug 15 12:37 fileg
#

3. move to directory above

# cd ..

4. now we can tar whole directory

# tar -cvf test.tar test
test/
test/test7/
test/test7/filef
test/test7/filea

.
.
.

test/test8/filec
test/test8/filed
test/test8/fileg
test/test8/fileb
#

5. show it

# ll
total 96
drwxr-xr-x  3 root root  4096 Aug  6 10:48 c
drwxr-xr-x  4 root root  4096 Aug  7 10:49 perl
drwxr-xr-x 12 root root  4096 Aug 15 12:37 test
-rw-r--r--  1 root root 81920 Aug 15 12:40 test.tar
#
# du -skx test.tar
84      test.tar
#

6. we will remove whole test directory and untar created tar file.

#
# rm -r test
#
# ll
total 92
-rw-r--r-- 1 root root 81920 Aug 15 12:40 test.tar
#
#
# tar -xvf test.tar test
test/
test/test7/
test/test7/filef
test/test7/filea
test/test7/filee

.
.
.

test/test8/filec
test/test8/filed
test/test8/fileg
test/test8/fileb
#

7. now we can check if everything was done right

#
# ll
total 96
drwxr-xr-x 12 root root  4096 Aug 15 12:37 test
-rw-r--r--  1 root root 81920 Aug 15 12:40 test.tar
#
#
# cd test
#
#
# ll test*
test1:
total 28
-rw-r--r-- 1 root root 6 Aug 15 12:37 filea
-rw-r--r-- 1 root root 6 Aug 15 12:37 fileb
-rw-r--r-- 1 root root 6 Aug 15 12:37 filec
-rw-r--r-- 1 root root 6 Aug 15 12:37 filed
-rw-r--r-- 1 root root 6 Aug 15 12:37 filee
-rw-r--r-- 1 root root 6 Aug 15 12:37 filef
-rw-r--r-- 1 root root 6 Aug 15 12:37 fileg

test10:
total 28
-rw-r--r-- 1 root root 6 Aug 15 12:37 filea
-rw-r--r-- 1 root root 6 Aug 15 12:37 fileb
-rw-r--r-- 1 root root 6 Aug 15 12:37 filec
-rw-r--r-- 1 root root 6 Aug 15 12:37 filed
-rw-r--r-- 1 root root 6 Aug 15 12:37 filee
-rw-r--r-- 1 root root 6 Aug 15 12:37 filef
-rw-r--r-- 1 root root 6 Aug 15 12:37 fileg

.
.
.

test9:
total 28
-rw-r--r-- 1 root root 6 Aug 15 12:37 filea
-rw-r--r-- 1 root root 6 Aug 15 12:37 fileb
-rw-r--r-- 1 root root 6 Aug 15 12:37 filec
-rw-r--r-- 1 root root 6 Aug 15 12:37 filed
-rw-r--r-- 1 root root 6 Aug 15 12:37 filee
-rw-r--r-- 1 root root 6 Aug 15 12:37 filef
-rw-r--r-- 1 root root 6 Aug 15 12:37 fileg
#

Leave a Reply