You are on page 1of 8

Archiving and

Archiving with tar


Compressing Data

Archiving and Compressing Data Archiving with tar


This subject can fall into the general Creating a file archive (tarfile) can
“housekeeping” of your files. help you organize your files for
storage or transfer.
You can put all of your related files and
store them as one larger separate file
called a “tarfile.”

Archiving with tar Archiving with tar


You can use a file archive (tarfile) Tar (tape archive) is one UNIX
when you: command we use to accomplish this.
Tar can also send files directly to a
want to keep files for later reference magnetic tape but for our purposes we
but are done with them at the will use it to make file archives.
moment.
want to compress groups of files for We used this the first day to extract your
UNIX_class subdirectory structure.
storage or transfer to other hosts.

1
Archiving with tar tar Practice
Start by moving into your UNIX_class
Creating a tarfile subdirectory.
$ ls –l
Suppose we want to archive a drwxrwxr-x 2 userid userid 512 Jun 28 17:52 Animal
directory. Let’s take dir2 from our drwxrwxr-x
drwxrwxr-x
2
2
userid
userid
userid
userid
512
512
Jun
Jun
18
18
21:31
20:10
Shakespeare
Wildcards
UNIX_class examples. drwxrwxr-x 2 userid userid 512 Jun 24 15:38 dir1
drwxrwxr-x 4 userid userid 512 Jun 24 13:55 dir2
It is always a good idea to verify the drwxrwxr-x 4 userid userid 512 Jun 28 15:19 dir3

name of the directory you want to


We will keep the example simple and create
archive.
the archive file in the same directory.

tar Practice tar Practice


a dir2/ 0K
Type: a dir2/.DS_Store 7K
a dir2/address_list 1K
a dir2/final.paper 1K
$ tar –cvf dir2.tar dir2 a dir2/history.txt 7K
a dir2/picts/ 0K
a dir2/picts/unixbutton.JPG 24K
a dir2/cats/ 0K
a dir2/cats/catsup/ 0K
a dir2/cats/cathode/ 0K
a dir2/cats/caterpillar/ 0K
a dir2/cats/caterpillar/butterfly 1K
a dir2/cats/caterpillar/larva 1K
a dir2/cats/catalyst 1K

tar Practice tar Practice


If we list our directory we should see You will see that you have a file
the dir2.tar at the same level in named dir2.tar and it is not a
hierarchy as dir2. directory, but a regular file.
$ ls –l $ ls –l
drwxrwxr-x 2 userid userid 512 Jun 28 17:52 Animal drwxrwxr-x 2 userid userid 512 Jun 28 17:52 Animal
drwxrwxr-x 2 userid userid 512 Jun 18 21:31 Shakespeare drwxrwxr-x 2 userid userid 512 Jun 18 21:31 Shakespeare
drwxrwxr-x 2 userid userid 512 Jun 18 20:10 Wildcards drwxrwxr-x 2 userid userid 512 Jun 18 20:10 Wildcards
drwxrwxr-x 2 userid userid 512 Jun 24 15:38 dir1 drwxrwxr-x 2 userid userid 512 Jun 24 15:38 dir1
drwxrwxr-x 4 userid userid 512 Jun 24 13:55 dir2 drwxrwxr-x 4 userid userid 512 Jun 24 13:55 dir2
-rw-rw-r-- 1 userid userid 49664 Ju1 8 16:28 dir2.tar -rw-rw-r-- 1 userid userid 49664 Ju1 8 16:28 dir2.tar
drwxrwxr-x 4 userid userid 512 Jun 28 15:19 dir3 drwxrwxr-x 4 userid userid 512 Jun 28 15:19 dir3

2
Archiving with tar Archiving with tar
What we did here is create a tarfile called $ tar –cvf filename.tar directoryname
dir2.tar which is a file archive of all the
files and directories under dir2. The options:
-c tells tar to “create”
Lets look at tar more closely. There
are many options and different ways to
use the command, but we will focus on
the ones you will use most often.

Archiving with tar Archiving with tar


$ tar –cvf filename.tar directoryname $ tar –cvf filename.tar directoryname

The options: The options:


-v tells tar to be verbose in the output -f tells tar that we are using a tarfile
it provides. This is not necessary, but it is and the next [operand] has to specify
helpful to see what is going on. It tells the name of the tarfile. If we did not
you what you are doing (appending or specify the -f option then tar would
extracting) and the file names and sizes. assume that your files are going to tape.

Archiving with tar Archiving with tar


$ tar –cvf filename.tar directoryname $ tar –cvf filename.tar directoryname

We use certain things in UNIX by We use certain things in UNIX by


convention and not by any rule. convention and not by any rule.
When we specify a name for our tarfile I also recommend, if you are tarring a
(file archive) it is good practice to use whole directory as in our example, to
the .tar extension. have the new filename be the same as
the original directoryname.

3
Archiving with tar Archiving with tar
tar –cvf /home/jsmith/tarfiles/dir2.tar dir2 tar –cvf /home/jsmith/tarfiles/dir2.tar dir2
pathname tarfile |
tar –cvf ~/tarfiles/dir2.tar dir2
original
Remember that the “tarfiles” directory has to already exist directory

If we wanted the tarfile to be stored If we wanted the tarfile to be stored


somewhere else in your directory somewhere else in your directory
structure you could specify an absolute structure you could specify an absolute
or relative pathname in front of the or relative pathname in front of the
tarfile. It could be stored anywhere in tarfile. It could be stored anywhere in
which you have write permissions. which you have write permissions.

Extracting with tar Extracting with tar


Extracting a tarfile: First, from our home directory, let’s do a
listing of our UNIX_class directory.
At some point in time you will probably
need access to individual files that
$ cd (takes us to the top of our home directory)
reside in your archive. You can extract
the file individually or the whole archive. $ ls -l UNIX_class
For the purpose of our example we will
extract the contents of dir2.tar to
another location.

Extracting with tar Extracting with tar


$ cd
$ ls –l UNIX_class When we extract our tarfile using this
drwxrwxr-x 2 userid userid 512 Jun 28 17:52 Animal
drwxrwxr-x 2 userid userid 512 Jun 18 21:31 Shakespeare particular example, we need to be in
drwxrwxr-x 2 userid
drwxrwxr-x 2 userid
userid
userid
512 Jun
512 Jun
18
24
20:10
15:38
Wildcards
dir1
the directory in which we want the files
drwxrwxr-x 4 userid userid 512 Jun 24 13:55 dir2 to end up.
-rw-rw-r-- 1 userid userid 49664 Ju1 8 16:28 dir2.tar
drwxrwxr-x 4 userid userid 512 Jun 28 15:19 dir3 For more tar options, see the man
We want to extract the tarfile to a different page on tar.
location. In this case, the top of our home
directory.

4
Extracting with tar Extracting with tar
Make sure you are in your home Type:
directory (not in UNIX_class).
$ tar –xvf UNIX_class/dir2.tar
$ pwd (always a good habit to get into)
-x option means to extract.

Extracting with tar Extracting with tar


x dir2, 0 bytes, 0 tape blocks
x dir2/.DS_Store, 6148 bytes, 13 tape blocks The verbose output tells you that it is
x dir2/address_list, 455 bytes, 1 tape blocks
x dir2/final.paper, 716 bytes, 2 tape blocks extracting (the “x” at the beginning),and
x dir2/history.txt, 7091 bytes, 14 tape blocks lists the file names and sizes.
x dir2/picts, 0 bytes, 0 tape blocks
x dir2/picts/unixbutton.JPG, 24125 bytes, 48 tape blocks
x dir2/cats, 0 bytes, 0 tape blocks
x dir2/cats/catsup, 0 bytes, 0 tape blocks
x dir2/cats/cathode, 0 bytes, 0 tape blocks
x dir2/cats/caterpillar, 0 bytes, 0 tape blocks
x dir2/cats/caterpillar/butterfly, 325 bytes, 1 tape blocks
x dir2/cats/caterpillar/larva, 103 bytes, 1 tape blocks
x dir2/cats/catalyst, 174 bytes, 1 tape blocks

Extracting with tar Extracting with tar


Verify dir2 is there in your home Remember that when you extract, your
directory: original tarfile is still there where you
put it in the first place. You can extract
$ ls
it as many times as you like to any
dir2 UNIX_class other locations in which you have write
permissions.

5
Compressing and Uncompressing
Compressing reduces the file size
Compressing and using a special encoding.
Uncompressing Tarring files and compressing often go
hand in hand.
You can compress a whole tarfile
without having to compress each
individual file.

Compressing and Uncompressing Compressing and Uncompressing


This will be helpful if you have large As you could imagine, it will also save
directories that you’ve tarred and will not on upload/download times with ‘sftp’ if
need for some time and you want to your file is compressed before sending it
save some disk space. to another host.

Compressing and Uncompressing Compressing and Uncompressing


The compression command we gzip is used to compress (zip) a
recommend is gzip. It was designed file or files
to replace the older UNIX command
simply called compress. gunzip is used to expand (unzip) a
file or files
It is more efficient and free, therefore it
is widely supported on other platforms. gzip produces files with a .gz
extension

6
Compressing and Uncompressing Compressing Practice
Let’s find our previous tarfile
(dir2.tar) that should reside at the
Using gzip to compress a file. top of your UNIX_class directory and
try this handy utility.

$ cd UNIX_class
$ ls -l

Compressing Practice Compressing Practice


$ cd UNIX_class
$ ls –l
Type:
drwxrwxr-x 2 userid userid 512 Jun 28 17:52 Animal
drwxrwxr-x 2 userid userid 512 Jun 18 21:31 Shakespeare
drwxrwxr-x 2 userid userid 512 Jun 18 20:10 Wildcards $ gzip dir2.tar
drwxrwxr-x 2 userid userid 512 Jun 24 15:38 dir1
drwxrwxr-x 4 userid userid 512 Jun 24 13:55 dir2
-rw-rw-r-- 1 userid userid 49664 Ju1 8 16:28 dir2.tar
drwxrwxr-x 4 userid userid 512 Jun 28 15:19 dir3
Let’s take a look at what we did.

Compressing Practice Compressing and Uncompressing


$ gzip dir2.tar
$ ls –l $ gzip filename
drwxrwxr-x 2 userid userid 512 Jun 28 17:52 Animal
drwxrwxr-x 2 userid userid 512 Jun 18 21:31 Shakespeare
drwxrwxr-x 2 userid userid 512 Jun 18 20:10 Wildcards
drwxrwxr-x 2 userid userid 512 Jun 24 15:38 dir1 In it’s simplest form the only [operand]
drwxrwxr-x 4 userid userid 512 Jun 24 13:55 dir2
-rw-rw-r-- 1 userid userid 29601 Ju1 8 16:28 dir2.tar.gz you have to supply to gzip is the
drwxrwxr-x 4 userid userid 512 Jun 28 15:19 dir3
filename you want to compress (zip).
In this case we used dir2.tar.

7
Compressing and Uncompressing Compressing and Uncompressing
Notice the file size difference between
the unzipped file and the zipped one.
Using gunzip to uncompress a file.
before:
-rw-rw-r-- 1 userid userid 49664 Ju1 8 16:28 dir2.tar
after:
-rw-rw-r-- 1 userid userid 29601 Ju1 8 16:28 dir2.tar.gz

Also notice the .gz extension

Compressing and Uncompressing Uncompressing Practice


Here again, the command can be very Type:
simple. Always look to the man page
for more options. $ gunzip dir2.tar.gz

The basic usage of the gunzip


command is to reverse the operation of Let’s take a look at what we did.
the gzip command.

Uncompressing Practice The End…


$ gunzip dir2.tar.gz
$ ls –l Next …
drwxrwxr-x 2 userid userid 512 Jun 28 17:52 Animal
drwxrwxr-x 2 userid userid 512 Jun 18 21:31 Shakespeare
drwxrwxr-x 2 userid userid 512 Jun 18 20:10 Wildcards
Pipes and Redirects
drwxrwxr-x 2 userid userid 512 Jun 24 15:38 dir1
drwxrwxr-x 4 userid userid 512 Jun 24 13:55 dir2
-rw-rw-r-- 1 userid userid 49664 Ju1 8 16:28 dir2.tar
drwxrwxr-x 4 userid userid 512 Jun 28 15:19 dir3

Now we are back to our original filesize


and gunzip removed the extension
.gz for us. Simple!

You might also like