Thursday, June 12, 2014

Basic Linux commands for DBA's Part IV

Compression Related

Compress the files, Each single file will be compressed as one file and a new file will get created with filename.gz
-c -- If you specified -c it displays standard out to console and creates a new gz file without delete the original file
$gzip *.err    -- Compress all .err files and creates new file per .err with .err.gz
$ gzip errorlog
$ ls - lrt
$ gzip -c errorlog > errorlog1.gz
$ ls -lrt
$ gzip *.err
Uncompress the file that was compressed using gzip or compress commands
-c -- displays standard output to console and without unzip the file
$  gunzip *.err.gz -- Uncompress all .err.gz files into .err files
$ gunzip errolog.gz
$ ls -lrt
$  gunzip *.err.gz
Tar stands for Tape Archive in Linux. This command is used to create a archive for collection of files into highly compressed files in unix / linux systems
Tar Examples
-c -- Creates a new .tar archive file
-v -- Verbose mode, shows the progress of .tar file
-f -- filename
[root@Kalyan workspace]# tar -cvf mysqlbinaries.tar /local/mnt/workspace/mysqlbinaries/
tar: Removing leading `/' from member names
[root@Kalyan workspace]# ls -lrt
total 374356
drwxr-x--- 2 kalyan users      4096 May 27 04:07 mysqlbinaries
-rw-r--r-- 1 root   bin   382955520 Jun  2 06:50 mysqlbinaries.tar
If you observe the above command tar -cvf, It just creates a .tar files with the collection files in the mysql binaries folder, but it wont compress the collection size.
[root@Kalyan mysqlbinaries]# du -h .
366M    .
[root@Kalyanmysqlbinaries]# cd ..
[root@Kalyanworkspace]# du -h mysqlbinaries.tar
366M    mysqlbinaries.tar
The size of whole mysqlbinaries folder and size of .tar file is same.
-z -- This option helps to create a compressed gzip archive file.
$ tar -cvzf mysqlbinaries.tar.gz /loca/mnt/workspace/mysqlbinaries
$ tar -cvzf mysqlbinaries.tgz /loca/mnt/workspace/mysqlbinaries
[root@kalyan workspace]# du -h mysqlbinaries.tgz
358M    mysqlbinaries.tgz
[root@kalyan workspace]# du -h mysqlbinaries.tar
366M    mysqlbinaries.tar
-j -- This option is used to create bz2 Archive file, it compress and creates archive file less than the size of gzip but it will take more time to compress an decompress the file. It is used to create highly compressed file (.tar.bz2 or tbz or tb2)
-x -- This option is used to untar or extract the contents of an archive file
-C -- Usually if you untar any file using -x option then it will extract the contents to the current folder, if you want to change the extract location to some other folder you need to specify -C option
$ tar -xvf abc.tar      -- Extracts the contents to current folder
$ tar -xvf abc.tar -C /usr/kalyan/tmp  -- Extracts the contents to specified folder
-t -- This option helps to list the contents of any tar file without extracting it
$ tar -tvf abc.tar            -- Lists the contents of the given tar file
--extract -- This option helps to extract only particular file from the .tar file
$ tar --extract --file=<.tarfile> 
$ tar --extract --file=mysqlbinaries.tar local/mnt/workspace/mysqlbinaries/MySQL-client-advanced.x86_64.rpm
-r -- This option helps to add / append a file to existing tar file
$ tar -rvf
$ tar -rvf mysqlbinaries.tar local      --- It adds local folder to mysqlbinaries.tar file

No comments: