Lab Chapter 3 Mod 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Lab Chapter 3: Commonly used Commands in Linux

Module 4: Archiving, Compression, Pipes and Redirection


~~*~~*~~
Archiving (tar)

– An archive file is a collection of files and directories stored in one file.

– The archive file is not compressed — it uses the same amount of disk

space as all the individual files and directories combined.

– Be polite and add “.tar” extension

>> Create tar Archive File


 The below example (1) command will create a tar archive file
MyfirstBackup.tar for a directory /root/Desktop in current working directory.
[root@server1 Desktop]#tar -cvf MyFirstBackup.tar /root/Desktop
1. c – Creates a new .tar archive file.
2. v – Verbosely show the .tar file progress.
3. f – File name type of the archive file.
 The below example (2) command will create a tar archive file MyData.tar
on Desktop of root to collect all files and directories from /mnt and /home.
[root@server1 Desktop]#tar -cvf MyData.tar /{mnt,home}

>> View tar file contents

េលក្រគ� គឹម េនា | RUPP


1 |ទំព័រ
tar -tf ArchivingBackup.tar
-t : list content of an archive
-f : use archive file

>> Untar tar Archive File


To untar or extract a tar file, just issue following command using
option x (extract). For example the below command will untar the file
test.tar in present working directory.
tar -xvf test.tar

>> To add Files or Directories to tar Archive File


To add files or directories to existing tar archived file we use the option r (append). For
example we add file xyz.txt and directory Back1 to existing ArchivingBackup.tar
tar -rvf ArchivingBackup.tar xyz.txt Backup1
Compression (zip, gzip and bzip2)

េលក្រគ� គឹម េនា | RUPP


2 |ទំព័រ
Compression makes a file smaller. It can be applied to individual files, groups of files or
entire directory trees.
I. ZIP
o Zip is a simple, cross-platform file packaging and compression
utility for Unix-like systems including Linux and Windows OS;
plus many other operating systems.
o Will automatically add extension “.zip” to file
>> To Create ZIP
zip ZipName file1 or dir1 or tar file

>> To view the contents of a zip file without extracting it, use the
following zip command:
zip -sf MyZIP.zip

>> UnZIP
unzip ZipName.zip # extract all

II. GZIP
o Gzib: Standard linux compression algorithm
េលក្រគ� គឹម េនា | RUPP
3 |ទំព័រ
o In general, GZIP is much better compared to ZIP, in terms of
compression, especially when compressing a huge number of
files.
o Will automatically add extension “.gz” to file
>> Create tar.gz Archive File
To create a compressed gzip archive file we use the command.
gzip ArchivingName.tar

>> If you would like to recursively compress an entire directory, you


can pass the -r flag like this:
gzip -r directory1
This will move down through a directory and compress each file individually.

>> View Contents of Compressed .tar.gz


tar -tvf backup.tar.gz

េលក្រគ� គឹម េនា | RUPP


4 |ទំព័រ
>> To decompress a file
gunzip tar.gz Archive File

III. BZIP2
o The bzip2 tools can create significantly more compact files than gzip, but take much
longer to achieve those results due to a more complex algorithm.
o Better linux compress algorithm
o Automatically add extension “.bz2” to file

>> Create tar.bzip2 Archive File


bzip2 ArchivinName.tar

>> How to View Contents of Compressed .tar.bz2


tar -jtvf file.tar.bz2

េលក្រគ� គឹម េនា | RUPP


5 |ទំព័រ
>> To decompress a bzip2 compressed file
bunzip2 tar.bzip2 Archive File or bzip2 -d tar.bzip2 Archive File

េលក្រគ� គឹម េនា | RUPP


6 |ទំព័រ

You might also like