Monday, May 20, 2013


Tar command is specially designed to zip and extract the data. It helps to reduce the space while getting backed up data. It is used to store and extract files from an archive file. Let’s understand tar command with examples:

How to archive folder in tar extension by tar command

Tar command requires two arguments to archive the folder i.e. first argument is output file name with tar extension and second is source folder name which we want to archive.
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# tar -cvf logs.tar logs/
logs/
logs/catalina.2012-12-03.log
logs/localhost.2012-12-03.log
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28#

-c option with tar means – create a new archive file
-v option – verbose mode
-f option – forcefully create archive.
After this command you need to use gzip command to get much archived file as compared to tar as :
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logs  logs.tar
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# gzip logs.tar
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logs  logs.tar.gz
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28#

Compression ratio of this technique is very high. Generally this technique is used for backing up large data.

How to extract archive by tar command

Now extract the above archived file by tar and gunzip command. Extraction is very easy and you need to remember some options of tar only.
-x option with tar – extract the archive
And no need of any option with gunzip to extract gz file.

root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logs  logs.tar.gz
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# gunzip logs.tar.gz
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logs  logs.tar
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# rm -rf logs
##removing logs folder otherwise it will overwrite the existing folder
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logs.tar
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# tar -xvf logs.tar
logs/
logs/catalina.2012-12-03.log
logs/localhost.2012-12-03.log
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logs  logs.tar
##tar files remains as it as but new extracted folder will create

We can extract tar files or gz files directly by tar command as follows with –z option.
-z option – extract gz file
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logs.tar.gz
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# tar -xvzf logs.tar.gz
logs/
logs/catalina.2012-12-03.log
logs/localhost.2012-12-03.log
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logslogs.tar.gz
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28#

How to update archive file by tar commandi.e. add new file in archive

We can update archive file by tar command. It is really very useful trick to update our backup data instead of creating new backup file.

root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logs  logs.tar
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28#
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# touch test1
//New file created here
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# tar -uvf logs.tar test1
test1
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logs  logs.tar  test1
## test1 is already here, so we will remove it to extract tar file and then we can understand archive is updated or not
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# rm–rf  logs test1
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
 logs.tar
##Now there is no logsor test1 file.
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# tar -xvf logs.tar
logs/
logs/catalina.2012-12-03.log
logs/localhost.2012-12-03.log
test1
root@hello:~/ctier/pkgs/apache-tomcat-5.5.28# ls
logs  logs.tar  test1



Posted by Machindra Dharmadhikari On 5/20/2013 12:01:00 AM 1 comment READ FULL POST

Friday, May 10, 2013

Difference between SAN and NAS                                Netstat Command in Linux

1.       What are the run levels?
There are total seven run levels as follows:
0 – halt
1—single user mode
2—multiuser mode without NFS
3—Multiuser with NFS
4 -- Unused
5 -- Multiuser mode with X11.
6 – Reboot.

2.       How to check all logged in user currently on server?
Ans.  W and who are the commands to check all logged in users on server. There many different commands to check this but mostly used w or who or finger.
root@rhel:~/rhel/shellscript# w
 01:17:45 up 12 days,  6:43,  3 users,  load average: 0.03, 0.04, 0.05
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
nielsen2 pts/0    10.136.26.166    Wed20    3days  1.05s  0.69s -bash
nielsenpts/1    10.136.26.227    01:05    0.00s  0.62s  0.07s sshd: nielsen [priv]
nielsen2 pts/2    10.136.26.92     Wed20    4days  0.43s  3.39s sshd: nielsen2 [priv]
root@rhel:~/rhel/shellscript#
root@rhel:~/rhel/shellscript# who
nielsen2 pts/0        2013-04-24 20:24 (10.136.26.166)
nielsenpts/1        2013-04-30 01:05 (10.136.26.227)
nielsen2 pts/2        2013-04-24 20:26 (10.136.26.92)
root@rhel:~/rhel/shellscript# finger
Login     Name       Tty      Idle  Login Time   Office     Office Phone
nielsen   Nielsen   *pts/1          Apr 30 01:05 (10.136.26.227)
nielsen2             pts/0      3d  Apr 24 20:24 (10.136.26.166)
nielsen2            *pts/2      4d  Apr 24 20:26 (10.136.26.92)
root@rhel:~/rhel/shellscript#

3.       What is the difference between soft link and hard link?
Ans. Soft link also called as Symbolic link. Soft link means link the new file to original file without copying whole file to new location. And New files size will be the characters in filename of original file. If you want to get detailed difference then click on next link: Difference between Soft Link and Hard Link.

4.       How to change hostname permanently for server?
Ans. Generally, we are changing hostname from `hostname command` but this is not the way to change the hostname permanently. You should have to open the file `/etc/sysconfig/network` and change the hostname parameter of this file and then restart the network service.

5.       What is the command to convert dos file into unix format ?
Ans. `dos2unix` command is generally used for convert dos file into unix format and `unix2dos` command is used to vice versa.
6.       On which port DNS server is running ?
Ans. 53 Port .
7.       How to get the lines which matches pattern and 4 lines after the matched pattern ?
Ans. We can use grep command to get 4 lines after matched pattern.
root@rhel:~/rhel/shellscript# cat /etc/passwd | grep -nA 4 root
1:root:x:0:0:root:/root:/bin/bash
2-daemon:x:1:1:daemon:/usr/sbin:/bin/sh
3-bin:x:2:2:bin:/bin:/bin/sh
4-sys:x:3:3:sys:/dev:/bin/sh
5-sync:x:4:65534:sync:/bin:/bin/sync
root@rhel:~/rhel/shellscript#

To get 4 lines before the matched pattern:
root@rhel:~/rhel/shellscript# cat /etc/passwd | grep -nB 4 snmp
45-jenkins:x:120:65534::/var/lib/jenkins:/bin/bash
46-bind:x:121:131::/var/cache/bind:/bin/false
47-nagios:x:122:132::/var/lib/nagios:/bin/false
48-guest-ZI4XVo:x:123:133:Guest,,,:/tmp/guest-ZI4XVo:/bin/bash
49:snmp:x:124:134::/var/lib/snmp:/bin/false
root@rhel:~/rhel/shellscript#

8.       How to create 10 files in single command?
Ans.  You can use 10 different names after touch command.
root@rhel:~/rhel/shellscript/test# touch test{1..10}
root@rhel:~/rhel/shellscript/test# ls
test1  test10  test2  test3  test4  test5  test6  test7  test8  test9
root@rhel:~/rhel/shellscript/test#

9.       Find commans syntax to find the file by inodenumber ?
Ans. as follows:
root@rhel:~/rhel/shellscript# ls -li
total 32
5644165 -rw-r--r-- 1 root root 4922 2013-04-17 20:02 bid.log
5644164 -rwxr-xr-x 1 root root  479 2013-04-24 22:27 shellscript.sh
5770666 drwxr-xr-x 2 root root 4096 2013-04-30 01:42 test
5644159 -rw-r--r-- 1 root root 2173 2013-04-17 17:50 test1.log
5644160 -rwxr-xr-x 1 root root  353 2013-04-16 20:04 test1.sh
5644161 -rwxr-xr-x 1 root root  514 2013-04-16 18:30 test2.sh
5644117 -rw-r--r-- 1 root root 1731 2013-04-17 17:55 test.log
root@rhel:~/rhel/shellscript# find . -inum 5644164
./shellscript.sh
root@rhel:~/rhel/shellscript#

10.   Find command syntax to get files accessed within last 30 days:
Ans:
root@rhel:~/rhel/shellscript#
root@rhel:~/rhel/shellscript# find . -type f -atime -30
./test2.sh
./bid.log
./test/test1
./test/test9
./test/test5
./test/test6
./test/test10
./test/test4
./test/test2
root@rhel:~/rhel/shellscript#

Check CPU usage in Linux                                              Usermod command in Linux
Posted by Machindra Dharmadhikari On 5/10/2013 09:27:00 AM 5 comments READ FULL POST
  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

    Chitika Ads 2

    Histat

    About

    Enter your email address:

    Delivered by FeedBurner