Friday, April 26, 2013


Before understanding the difference between soft link and hard link, we need to understand the concept of soft link and hard link as well as concept of Inode in unix file system. Actually, link concept originated to replicate the file at different location without copying it and it is based on file system. File System is closely related withinode which is unique in system.

How to create Soft Link in Linux?

We will create soft link as well as hard link in two examples and then we will check for difference.

root@hello:~/ctier/shellscript# pwd
/root/hello/shellscript
root@hello:~/ctier/shellscript# ls -li shellscript.sh
5644164 -rwxr-xr-x 1 root root 446 2013-04-17 17:48 shellscript.sh
root@hello:~/ctier/shellscript# ln -s /root/ctier/shellscript/shellscript.sh /root/ctier/test/shells.sh
root@hello:~/ctier/shellscript# ls -li /root/ctier/test/shells.sh
6554654 lrwxrwxrwx 1 root root 16 2013-04-24 22:10 /root/ctier/test/shells.sh -> /root/hello/shellscript/shellscript.sh
root@hello:~/ctier/shellscript#

As per above example, we have created soft link of shellscript.shto this file /root/ctier/test/shells.sh. i.e. new file is /root/ctier/test/shells.sh. If you check inode of new file, it is different. And in long listing, it is showing that it points to some other file. Size of file is different at both the locations. Because in soft link, there is no copy of file, it is just pointing to the original location. If we are trying to open a /root/ctier/test/shells.sh this file, then it will open original file i.e. /root/ctier/shellscript/shellscript.sh. If we made any changes in original file then it will reflect in soft linked file as well.

How to create Hard Link in Linux?

root@hello:~/hello/shellscript# ls -li shellscript.sh
5644164 -rwxr-xr-x 1 root root 446 2013-04-17 17:48 shellscript.sh
root@hello:~/hello/shellscript# mkdir test
root@hello:~/hello/shellscript# ln shellscript.sh ./test/hardlink.sh
root@hello:~/hello/shellscript# ls -li ./test/hardlink.sh
5644164 -rwxr-xr-x 2 root root 446 2013-04-17 17:48 ./test/hardlink.sh
root@hello:~/hello/shellscript#

If you see above example of hardlink then you can understand thatin hard link, file’s new copy is created at new location with its original size. Original file’s changes will reflect in new file. But there is no linking showing in long listing because new file is pointing to the original file’s inode. if we delete any one of file or original file then it will not impact on this hard link. Another file will be accessible.

Difference between Soft Link and Hard Link

Difference
Soft Link
Hard Link
Inode
Inode will be different for both the files
Inode will be same for both the files
Deletion of original file
Deletion of original file, impact on link. Another file will not be accessible.
Deletion of original file, no impact on link. Another file will be accessible.
Time to execute
Access time is slow as compared to hard link
Access time is fast as compared to soft link
Cross File System
In Cross file system, Soft link works
In cross file system, hard link is not working. Needed same file system




Posted by Machindra Dharmadhikari On 4/26/2013 10:03:00 AM 1 comment READ FULL POST
Advanced use of sed command in Linux                              mpstat command to check cpu usage

Inode in file system is a fundamental concept of unix file system and each object in the file system represented by an inode. Inodes store information about files and directories like ownership, access mode, file type etc. Each file is having unique inode. When we copy file from one location to other that time new inode number assigned to the new file.

If you want to see the inode number of file then use following command:

root@hello:~/hello/shellscript# ls -i shellscript.sh
5644164 shellscript.sh
root@hello:~/hello/shellscript#

5644164 is the inode of shellscript.sh file.
We will copy this file in other location and check the inode for same file:

root@hello:~/hello/shellscript# mkdir test
root@hello:~/hello/shellscript# cp shellscript.sh test/
root@hello:~/hello/shellscript# cd test
root@hello:~/hello/shellscript/test#ls -i shellscript.sh
5770669 shellscript.sh
root@hello:~/hello/shellscript/test#

You can see same file is having different Inode5770669. i.e. Each file is having unique inode.

Inode stores following information
  • File Size
  • File Type
  • File modification time
  • File Change time
  • File Access time
  • File deletion time
  • Permission
  • Owner
  • Group

Inode contains above information about file.

There is another way to check the inode of a file by stat command.

root@hello:~/hello/shellscript/test# stat shellscript.sh
  File: `shellscript.sh'
  Size: 446             Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d      Inode: 5770669     Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-04-24 21:30:46.473060619 +0530
Modify: 2013-04-24 21:30:46.493059771 +0530
Change: 2013-04-24 21:30:46.493059771 +0530
root@hello:~/hello/shellscript/test#

Stat command provides the detailed statistics of file. Access Permissions, type of file, IO Block, Size, Device information etc information provided by stat command. Inode is most important concept in Unix operating system.

Inode do not contain file name but it stores metadata of a file.
Posted by Machindra Dharmadhikari On 4/26/2013 10:01:00 AM No comments READ FULL POST

Wednesday, April 24, 2013


How to provide expiry date to user                                         How to change login shell of user

We will go through the examples of Sed substitution using regular expression as follows. Sed command in unix examples, we have given as follows. For all examples, we will consider one file as an input and get desired output from that file. i.e. employee.txt

root@rhel:~/test# cat employee.txt
1001,Ajay,Manager,25000
101,Satish,Founder,30000
302,Atul,CEO,26000
434,Raj,Senior Manager, 26000
1231,Kalyan,Human Resource,20000
root@rhel:~/test#

Replace the last five characters in every line of file

In this example, we are replacing last five characters of each line by “Salary not decided” phrase.

root@rhel:~/test# cat employee.txt
1001,Ajay,Manager,25000
101,Satish,Founder,30000
302,Atul,CEO,26000
434,Raj,Senior Manager, 26000
1231,Kalyan,Human Resource,20000
root@rhel:~/test# sed 's/.....$/Salary not decided/g' employee.txt
1001,Ajay,Manager,Salary not decided
101,Satish,Founder,Salary not decided
302,Atul,CEO,Salary not decided
434,Raj,Senior Manager, Salary not decided
1231,Kalyan,Human Resource,Salary not decided
root@rhel:~/test#

Delete all commented lines in a file

In this example, we are deleting all commented lines of a file by sed command.

root@rhel:~/test# cat pattern.txt
1001,Ajay,Manager,Salary not decided
101,Satish,Founder,Salary not decided
302,Atul,CEO,Salary not decided
434,Raj,Senior Manager, Salary not decided
#1231,Kalyan,Human Resource,Salary not decided
#101,Satish,Founder,Salary not decided
434,Raj,Senior Manager, Salary not decided
1231,Kalyan,Human Resource,Salary not decided
root@rhel:~/test# sed 's/#.*//; /^$/d' pattern.txt
1001,Ajay,Manager,Salary not decided
101,Satish,Founder,Salary not decided
302,Atul,CEO,Salary not decided
434,Raj,Senior Manager, Salary not decided
434,Raj,Senior Manager, Salary not decided
1231,Kalyan,Human Resource,Salary not decided
root@rhel:~/test#


Recommended Article :  How to Lock User's Password by usermod command 

Strip all HTML tags from html file or delete all html tags from html file

We can remove all html tags from html file using sed command.


root@rhel:~/test# cat emp.html
<html>
<body>
<h1>
Linux Concepts and Commands….roundoverlinux.blogspot.com
</h1>
</body></html>
root@rhel:~/test#
root@rhel:~/test#
root@rhel:~/test# sed 's/<[^>]*>//g' emp.html
Linux Concepts and Commands ..roundoverlinux.blogspot.com
root@rhel:~/test#

Insert Line after the matched pattern in file

Below is the syntax for inserting line after matched pattern line in a file.

sed '[address] a the-line-to-append' input-file

root@rhel:~/test# cat employee.txt
1001,Ajay,Manager,25000
101,Satish,Founder,30000
302,Atul,CEO,26000
434,Raj,Senior Manager, 26000
1231,Kalyan,Human Resource,20000
root@rhel:~/test# sed '2 i 1034,Sagar,CFO,34000' employee.txt
1001,Ajay,Manager,25000
1034,Sagar,CFO,34000
101,Satish,Founder,30000
302,Atul,CEO,26000
434,Raj,Senior Manager, 26000
1231,Kalyan,Human Resource,20000
root@rhel:~/test#

Append line after the matched pattern containing line:

Example shows the line appended after the second line. i.e. new line will be 3rd line in file.

root@rhel:~/test# cat employee.txt
1001,Ajay,Manager,25000
101,Satish,Founder,30000
302,Atul,CEO,26000
434,Raj,Senior Manager, 26000
1231,Kalyan,Human Resource,20000
root@rhel:~/test# sed '2 a 1034,Sagar,CFO,34000' employee.txt
1001,Ajay,Manager,25000
101,Satish,Founder,30000
1034,Sagar,CFO,34000
302,Atul,CEO,26000
434,Raj,Senior Manager, 26000
1231,Kalyan,Human Resource,20000
root@rhel:~/test#

Delete the line that matches pattern and replaces it with multiple new lines
We can replace single line with multiple lines. 

Advanced Tricks of Sed command                               10 useful baisc tricks of sed command
Posted by Machindra Dharmadhikari On 4/24/2013 08:41:00 AM No comments READ FULL POST

Wednesday, April 17, 2013

Useradd command examples                                                 usermod command examples

Groupadd command is used to create a new group account using the specified values with options or default values from the system. All information related to the group added into the system files as needed. There are many options present in group add command to change account settings of group.

We will understand all major options of groupadd command with examples:

How to create new group ?

Suppose, we want to create a new group and but we are not sure given group ID is used somewhere or not. So, in this case we can use –f option with –g option to create group forcefully. i.e. if gid is already used then it will take new gid and create the group.

As below example, 1010 gid is used already but due to –f option, command executes and it has taken next group ID i.e. 1011.

root@rhel:~# cat /etc/group | grep test
test1:x:1010:
root@rhel:~# groupadd -fg 1010 login
root@rhel:~# cat /etc/group
login:x:1011:

How to create system group?

We can create system group with –r option with groupadd command. The numeric id of system groups always chosen in the SYS_GID_MIN-SYS_GID_MAX range, which is defined in login.defs instead of GID_MIN-GID_MAX

root@rhel:~# groupadd -r hello
root@rhel:~# cat /etc/group | grep hello
hello:x:999:
root@rhel:~#
Groupdel Command 

Groupdel command is used to delete a group. Before deleting the group, we need to must take care of following things:
  • We can’t remove the primary group of any existing user.
  • We need to remove user first and then group
  • For safe side, we need to check all file systems to ensure that no files owned by this group.

Groupdel Command example:

[root@rhel ~]# groupdel test
[root@rhel ~]#



How to turn of startup services in Linux                                     Check java Performance Statistics
Posted by Machindra Dharmadhikari On 4/17/2013 09:14:00 PM 2 comments READ FULL POST
How to Remove HTML tags from HTML file using sed  Command

Usermod command is used to modify user account. This command is useful to change the parameters of only existing users. It can change home directory of existing user, login name, login shell, comment of passwd file etc.

We will understand usermod command with examples:

How to change home directory of existing user?

Sometimes we need to change user’s home directory with new directory but all content of old directory should have to move new directory. In this case, we need to use –m option with –d, which moves all old directory content to new directory. If we don’t want to move old directory content to new then no need to use –m option.

Test1 is already created user. We need to move old directory content to new location and set it as new home directory.
Old home directory= /home/test1
New home directory=/root/test1

root@rhel:~/ctier/shellscript# cat /etc/passwd | grep test1
test1:x:1008:1010::/home/test1:/bin/sh
root@rhel:~/ctier# usermod -md /root/test1 test1
root@rhel:~/ctier#
How to change the login shell of user?

Sometimes, we forget to mention shell while creating user. But we can change the default shell to desired as below:

root@rhel:~/test2# useradd test1
root@rhel:~/test2# su test1
$
$
$ echo $USER
test1
$
$ echo $SHELL
/bin/sh
$
$ exit
root@rhel:~/test2# usermod -s /bin/bash test1
root@rhel:~/test2# su test1
test1@rhel:/root/test2$ echo $SHELL
/bin/bash
test1@rhel:/root/test2$


Rename the user

We can change the login name of user with usermod command.  But if you change the username then it will not impact on other details like home directory, expiry date etc. It will remain same.

root@rhel:~/test2# cat /etc/passwd | grep test
test1:x:1008:1010::/home/test1:/bin/bash
root@rhel:~/test2# usermod -l login1 test1
root@rhel:~/test2# cat  /etc/passwd | grep test
login1:x:1008:1010::/home/test1:/bin/bash
root@rhel:~/test2# su login1
login1@rhel:/root/test2$

Lock user’s password

Root user can lock user’s password as well as unlock it using the usermod command. As below example, we locked user’s password by –L option through root user and then tried to switch user from normal user but couldn’t login. Again login by root user, unlock the user’s password by –U option and the tried to login then it is allowing successfully.

root@rhel:~# usermod -L login1
root@rhel:~# exit
logout
machindra@rhel:~$
machindra@rhel:~$ su login1
Password:
su: Authentication failure
machindra@rhel:~$ sudo -i
root@rhel:~# usermod -U login1
root@rhel:~# exit
logout
machindra@rhel:~$ su login1
Password:
login1@rhel:/home/test$
login1@rhel:/home/test$

Change UID of existing user

We can change the UID of existing user by usermod command. For this purpose, we need to use –u option with desired UID and it should not be used by any other user.
Old UID: 1008
New UID: 3045

root@rhel:~#
root@rhel:~# cat /etc/passwd | grep test
login1:x:1008:1010::/home/test1:/bin/bash
root@rhel:~# usermod -u 3045 login1
root@rhel:~#
root@rhel:~# cat /etc/passwd | grep test
login1:x:3045:1010::/home/test1:/bin/bash
root@rhel:~#

Change the group of user

We can change the group of user with –g option. Old group id of user login1 is 1010 and we will change it to 130 i.e. group will be rdeck.
root@rhel:~# cat /etc/passwd | grep test
login1:x:3045:1010::/home/test1:/bin/bash
root@rhel:~#
root@rhel:~# usermod -g 130 login1
root@rhel:~# cat /etc/passwd | grep test
login1:x:3045:130::/home/test1:/bin/bash
root@rhel:~#

Advanced use of sed command                                                                   Basic use of sed command
Posted by Machindra Dharmadhikari On 4/17/2013 08:20:00 PM 1 comment READ FULL POST

Tuesday, April 16, 2013

Change Properties of Existing user                                         Groupadd/Groupdel command

Useradd command is very useful in unix to add user or update the user’s information or user’s home directory, comment etc. Almost all options are same for useradd command in solaris/Linux/Ubuntu. There are many uses of useradd command. We will understand the useradd command with examples:

Add user with home directory:

If you are not specifying the home directory while creating user, then system will create default base directory for that user. The base directory is concatenated with account name to define home directory. If you want to specify different home directory then we need to specify home dir(-d). When we use –d option then no use of –b option.
[root@rhel home]# useradd -d /home/test test
[root@rhel home]#
[root@rhel home]# ls -lrta /home/test
total 28
-rw-r--r--. 1 test test  124 Jun 22  2010 .bashrc
-rw-r--r--. 1 test test  176 Jun 22  2010 .bash_profile
-rw-r--r--. 1 test test   18 Jun 22  2010 .bash_logout
drwxr-xr-x. 2 test test 4096 Jul 14  2010 .gnome2
drwxr-xr-x. 4 test test 4096 Mar 20 18:06 .mozilla
drwxr-xr-x. 4 root root 4096 Apr 12 22:59 ..
drwx------. 4 test test 4096 Apr 12 22:59 .
[root@rhel home]#

Add user with comment, home directory:

If we want to give comment to identify the user then we can use –c option and for home directory –d option.
[root@rhel home]#
[root@rhel home]# useradd -c "linux concepts and commands" -d /home/test2 test2
[root@rhel home]# cat /etc/passwd | grep linux
test2:x:502:502:linux concepts and commands:/home/test2:/bin/bash
[root@rhel home]#

Disable user automatic after some period:

We can disable added user automatically after some period. For this purpose, we need to specify date on which the user account will disable. The date should have to specify in YYYY-MM-DD format. If this parameter is not specified while creating user then system will  consider default expiry date (/etc/default/useradd) specified in this file. If you want to extend the expiry date of user then you can do it by usermod command.
[root@rhel home]# useradd -e 2013-04-17 -d /home/test3 test3
[root@rhel home]#
[root@rhel home]# cat /etc/passwd | grep test3
test3:x:503:503::/home/test3:/bin/bash
[root@rhel home]#

Add user in existing group:

We can add user in existing group by specifying group name while creating user first time. We can change the group by usermod command after creation of user as well. Or we can insert user in multiple groups as well. If group is not already exist then create it by groupadd command.
[root@rhel home]#
[root@rhel home]# useradd -d /home/test4 -c "Linux Concepts" -e 2013-04-18 -g test3 test4
[root@rhel home]# cat /etc/passwd | grep test4
test4:x:504:503:Linux Concepts:/home/test4:/bin/bash
[root@rhel home]# cat /etc/group | grep 503
test3:x:503:
[root@rhel home]#

We can change any parameter of user after creation for this we need to use usermod command. To delete the user permanently userdel command is available for this purpose.
Posted by Machindra Dharmadhikari On 4/16/2013 11:14:00 PM 1 comment READ FULL POST
  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

    Chitika Ads 2

    Histat

    About

    Enter your email address:

    Delivered by FeedBurner