Wednesday, October 31, 2012

Link count: File vs Directory

  One of the results of the ls -l command  is the link count. What is this link count? How it is useful? What is the difference between the link count of a file and a directory? Let us discuss in this article.

1. What is the link count of a file?
      The link count of a file tells the total number of links a file has which is nothing but the number of hard-links a file has. This count, however, does not include the soft-link count.
Note: The soft-link is not part of the link count since the soft-link's inode number is different from the original file.

2. How to find the link count of a file or directory?
   The link count can be seen by using the ls -l command :
$ ls -l test.c
-rwxr-xr-x 1 guru users 267 Jul 18 16:59 test.c
    By default, a file will have a link count of 1 i.e., any new file created will have a link count 1.

Monday, October 29, 2012

How to remove a component of absolute path?

How to remove a component of absolute PATH?

Let us consider an absolute path "/export/home/guru/prog/perl".
$ x="/export/home/guru/prog/perl" 
The requirement is to remove the 4th component of the absolute path so that it becomes "/export/home/prog/perl".

1. cut command:
$ echo $x | cut -d"/" -f -3,5-
cut is the straightforward option. Since we want to drop the 4th field, cut fields 1 to 3, and then from 5 onwards till the end.

Thursday, October 11, 2012

How to find the total count of a word / string in a file?

How to find/calculate the total count of occurences of a particular word in a file?

Let us consider a file with the following contents. The requirement is to find the total number of occurrences of the word 'Unix':
$ cat file
Unix is an OS
Linux is also Unix
Welcome to Unix, and Unix OS.

Tuesday, October 9, 2012

10 examples of split command in Unix

split command in Unix is used to split a large file into smaller files. The splitting can be done on various criteria: on the basis of number of lines, or the number of output files or the byte count, etc. In an earlier article, we discussed how to split files into multiple files using awk. In this, we will discuss it using the split command itself:
 Let us consider a file with the following content:
$ cat file

Wednesday, October 3, 2012

How to find duplicate records of a file in Linux?

How to find the duplicate records / lines from a file in Linux?
  Let us consider a file with the following contents. The duplicate record here is 'Linux'.
$ cat file
Let us now see the different ways to find the duplicate record.

1. Using sort and uniq:
$ sort file | uniq -d
   uniq command has an option "-d" which lists out only the duplicate records. sort command is used since the uniq command works only on sorted files. uniq command without the "-d" option will delete the duplicate records.