next up previous
Next: Files That Aren't Files Up: Introduction to the Linux Previous: Security

File Archiving and Compression

Sometimes it's useful to bundle up a lot of files and directories into a single file, for example so you can email it to someone. An excellent way to do this is using the tar command, though unfortunately it has a rather unusual syntax.

[phasnip@mijpnb1 ~/Teaching]$ tar cvf linux.tar Linux/*.[to]??
Linux/linux.tex
Linux/first.txt
Linux/first_attendees.odt
Linux/second_attendees.odt
Linux/second.txt
Linux/.#linux.tex

Notice that the options to tar are not preceded by a hyphen, tar assumes that the first argument contains the options. cvf means `create', `verbose', `file' i.e. create a new tar file, tell me what you're putting in it, and put the result in file linux.tar (the argument immediately after the f). After that you put a list of files and/or directories and tar will put them all in the output file.

tar does not delete the original files, so you don't need to worry about losing files. To extract the files from a tar file you use tar xvf linux.tar where x means extract. The tar command stores the path of each file and automatically re-creates the directory structure it was called with when the files are extracted.

Because they can contain lots of files, tar files can grow quite large. In fact it's common in science to generate large data files and we can exhaust the available storage easily. One way to reduce this is to compress the data with a compression algorithm, and one of the most popular is the Lempel-Ziv 1977 method. Fortunately for us, Linux comes with compression and decompression utilities so we don't have to do this ourselves.

[phasnip@mijpnb1 ~/Teaching]$ ls -l
total 208
drwxrwxr-x    2 phasnip  phasnip      4096 Oct 18 12:26 Linux
-rw-rw-r--    1 phasnip  phasnip    204800 Oct 18 12:21 linux.tar
[phasnip@mijpnb1 ~/Teaching]$ gzip linux.tar
[phasnip@mijpnb1 ~/Teaching]$ !ls
ls -l
total 76
drwxrwxr-x    2 phasnip  phasnip      4096 Oct 18 12:26 Linux
-rw-rw-r--    1 phasnip  phasnip     69602 Oct 18 12:21 linux.tar.gz
[phasnip@mijpnb1 ~/Teaching]$ gunzip linux.tar.gz
total 208
drwxrwxr-x    2 phasnip  phasnip      4096 Oct 18 12:26 Linux
-rw-rw-r--    1 phasnip  phasnip    204800 Oct 18 12:21 linux.tar


next up previous
Next: Files That Aren't Files Up: Introduction to the Linux Previous: Security
Phil Hasnip 2007-08-23