Tuesday, August 21, 2012

What is the use of sticky bit in Linux?

    In an earlier article, we discussed the purpose of SUID, an advanced file permision. In the same lines, Sticky bit is also an advanced file permission used in Unix/Linux or its flavors. This feature was originally meant for regular files and directories, though is now used only in directories in many Unix flavors. This permission, when applied on a directory, allows users to maintain 'their' files in directories which are shared with everybody.  

Thursday, August 16, 2012

15 examples of sort command in Linux

sort command is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the contents are ascii. Using options in sort command, it can also be used to sort numerically. Let us discuss it with some examples:

File with Ascii data:
 Let us consider a file with the following contents:
$ cat file
Unix
Linux
Solaris
AIX
Linux
HPUX

1. sort simply sorts the file in alphabetical order:
$ sort file
AIX
HPUX
Linux
Linux
Solaris
Unix
All records are sorted alphabetically.

Monday, August 13, 2012

7 examples to print few lines before a pattern match in Linux

In this article, we will discuss the different ways for how to print a few lines before a pattern match in log files. You will do well to know how to print a few lines AFTER a pattern match.

 Let us consider a sample file as below. The requirement is to print 2 lines before the pattern 'Linux':
$ cat file
Unix
AIX
Solaris
Linux
SCO
1. grep will do it for you if your grep is a GNU grep.
$ grep -B2 Linux file
AIX
Solaris
Linux
The option '-B' will give n lines before the pattern. Hence '-B2 Linux' gives 2 lines before the pattern 'Linux' including the line matching the pattern.

Monday, August 6, 2012

awk - Passing awk variables to shell

In one of our earlier articles, we discussed how to access or pass shell variables to awk. In this, we will see how to access the awk variables in shell? Or How to access awk variables as shell variables ?  Let us see the different ways in which we can achieve this.

 Let us consider a file with the sample contents as below:
$ cat file
Linux 20
Solaris 30
HPUX 40
1. Access the value of the entry "Solaris" in a shell variable, say x:
$ x=`awk '/Solaris/{a=$2;print a}' file`
$ echo $x
30
This approach is fine as long as we want to access only one value. What if we have to access multiple values in shell?

Thursday, August 2, 2012

Move files and directories : Inodes Part 3

  In our earlier inode articles, we discussed about the inode basics and inode relations with the soft links and hard links and their differences. In this, we will discuss about the changes in inodes during the move(mv) of a file or directory.
  
1.  Does the mv command rename a file or move a file?
      It actually does both, depending on the context. If the source and target are both files, it does rename. If the source is a file and the target is a directory, it does move. However, once we understand the internal working of the mv command with respect to inodes, we will realize both are same.