C2110 UNIX and Programming 13th lesson -1C2110 UNIX and Programming Petr Kulhánek kulhanek@chemi.muni.cz National Centre for Biomolecular Research, Faculty of Science, Masaryk University, Kamenice 5, CZ-62500 Brno 13th lesson C2110 UNIX and Programming 13th lesson -2- Contents ➢ Data compression • lossless vs. lossy compression ➢ Archives • types of archives, creating and extracting archives ➢ Program compilation using source codes • Archive extraction • Configuration • Compilation • Installation ➢ New commands • gzip, bunzip, bzip2, bunzip2, zip, unzip, tar C2110 UNIX and Programming 13th lesson -3- Compression ➢ lossless ➢ lossy C2110 UNIX and Programming 13th lesson -4- Compresion Compression is a process which reduces size of the data (files). This is achieved by searching redundant or irrelevant information in the data, which are then stored more efficiently. Compression algorithms can be divided into two basic categories: • lossy compression – leads to irreversible loss of some irrelevant information, which is usually tolerated, i.e., compressing video or audio data • lossless compression – no loss of original information, compressed data can be fully restored to their original state, the compression ratio is much lower than with lossy compression Restoring compressed data is called decompression. Compress ratio is the measure of the compression quality. It is expressed as the ratio between the size of the original and compressed data. C2110 UNIX and Programming 13th lesson -5Lossy compression Programs for lossy compression and decompression: • mplayer • mencoder • convert (Image Magick) • and other... Converting the image in PNG (Portable Network Graphics) format to JPEG (Joint Photographic Experts Group): $ convert input.png -quality number output.jpeg uses lossless compression uses lossy compresion quality ratio of the final image from 1 (worst quality with most compression) to 100 (best quality with worst compression) C2110 UNIX and Programming 13th lesson -6- Exercise 1. From the directory /home/kulhanek/Data/Komprese, copy the image test.png into your home directory. 2. What is size of the image file in bytes? 3. Perform the lossy compression of the image to jpeg image. Use compression quality 10, 50 and 90. Store the resulting pictures separately. 4. Compare the visual quality of the compressed images (display command) 5. What is the compression ratio for the quality 10 and 90? C2110 UNIX and Programming 13th lesson -7Lossless compression Programs for lossless compression and decompression: • gzip/gunzip • bzip2/bunzip2 • zip/unzip • and other ... Compression of a text file: : $ gzip file.txt $ bzip2 file.txt resulting file will be named file.txt.bz2 resulting file will be named file.txt.gz Decompression of compressed data : $ gunzip file.txt.gz $ bunzip2 file.txt.bz2 Result of compression or decompression can be sent to the standard output (original file then remains unchanged), e.g.: $ bunzip2 --stdout file.txt.bz2 | wc C2110 UNIX and Programming 13th lesson -8- Exercise 1. From /home/kulhanek/Data/Komprese, copy the text file bu6_f.log to your home directory. 2. What is the the size of the file in bytes? 3. Perform lossless compression of the file using programs gzip and bzip2. Which of these two program has higher compress ratio? 4. Which of these two program compresses faster? C2110 UNIX and Programming 13th lesson -9- Archives ➢ types ➢ creating and extracting archives C2110 UNIX and Programming 13th lesson -10Archives - tar tar (abbreviation from tape archiver) is a collective name for both the file format used to store many different files and for single-purpose programs that work with this format. The format itself was created at the beginning of Unix and later was standardized within the POSIX standards. Originally it assisted with archiving files onto tape drives, but later its usage expanded and today it is used, when appropriate for the purpose of distribution or archiving, to join multiple files in one file, in a way that preserves information about the directory structure, access rights and other attributes, which the file system normally contains. www.wikipedia.org Archive extraction: $ tar xvf archive.tar Archive creation: $ tar cvf archive.tar directory/ $ cd adresar $ tar cvf /path/to/archive.tar * if the name of the archive contains suffix .gz or .bz2, archive is automatically compressed or decompressed. C2110 UNIX and Programming 13th lesson -11- Exercise 1. What is meaning of the options cvf for command tar? 2. What is meaning of the options xvf for command tar? 3. Create archive from files stored in the directory: /home/kulhanek/Data/Archive 4. What is the size of archive file? 5. Perform compression of the archive. What is the compression ratio? 6. Extract archive into the directory /scratch/your_login/archive C2110 UNIX and Programming 13th lesson -12Application Compilation ➢ Application Armagetronad ➢ Extracting the archive ➢ Configuration ➢ Compilation ➢ Installation C2110 UNIX and Programming 13th lesson -13- Armagetronad http://armagetronad.org/ Procedure: 1) Downloading of the source code 2) Extract the archive 3) Instruction for the installation (README, INSTALL, doc/README, doc/INSTALL) 4) Configuration 5) Compilation 6) Installation Saint triple $ ./configure $ make $ make install C2110 UNIX and Programming 13th lesson -14Armagetronad, Step I All is performed in scratch. 1) Archive extraction: $ tar xvf armagetronad-0.2.8.3.2.src.tar.gz 2) Making of installation directory, i.e., where the program will be installed (necessary if you are not root) $ mkdir armagetronad $ pwd /scratch/kulhanek/game/armagetronad 3) Change of working directory to extracted archive: $ cd armagetronad-0.2.8.3.2 4) Configuration for compilation and installation: $ ./configure --prefix=/scratch/kulhanek/game/armagetronad \ --disable-etc --disable-uninstall In this phase, some libraries or application might be missing. They can be installed in the same way, but it is more appropriate (and faster) to ask admin to install them. To compile, development packages of individual libraries must be installed. e.g.: # apt-get install libxml2-dev where the program will be installed C2110 UNIX and Programming 13th lesson -15Armagetronad, Step II 5) Compilation $ make 6) Installation $ make install 7) Starting of the program $ cd /scratch/kulhanek/game/armagetronad $ ./bin/armagetronad where the program is installed