Tuesday, March 27, 2012

Basic Unix Commands - Part3




File Descriptors:
Standard input        <          0
Standard Output     >          1  Append >>
Standard Error       2>         2

cat < file1   To list the contents of the file. 

ls –l > out1 --- Redirecting the output to the file out1.
ls –l >> out1 --- Appending the output to the file out1.

cp file1 file3   2> error1   --- If any error, the error redirects to the file error1.

Pipes and Filters:
Ist command | 2nd command

First command output is going to input of 2nd command.

wc  -- word count
ls –l | wc –l
who | wc –l
cut  --- cut the columns or characters :

cat /etc/passwd | cut –d “:” –f1   ----- This command cut the username alone in passwd file.
cat /etc/passwd | cut –d “:” –f1-4
cat /etc/passwd | cut –d “:” –f1,3

cat /etc/passwd | cut –c1-7
cat /etc/passwd | cut –c5-8
cat /etc/passwd | cut –c1,7

To list contents of the file:

ls –l | more
ls –l | less

To list first 10 lines of the file:
cat /etc/passwd | head

To list last 10 lines of the file:

cat /etc/passwd | tail

cat /etc/passwd | head -5
cat /etc/passwd | tail -5

tail –f /var/log/messages     -------- It follow the growth of the file. This –f  option is very helpful to the administrators.

cat /etc/passwd | pg   ----- pagewise
To search the file names in the file system:

find <path><-options><actions>

find /home/senthil/  -name f1 –print   --- to search a file name called f1.

Few options in find command:
-name
-print
-type
-inum
-perm
-user
-mtime
-atime
-mmin
-amin
-depth
-exec
-ok

find /home/senthil –name f1 –type f –print
find . –type f –print ----- to list only files in the current directory.
find . –type l –print  ---- to list the softlink files.
find . –type f –mtime 2 –print
find . –name f1 –type f –exec rm { } \ ;  --- To find and delete.

Read more on man find

To search a pattern in the file contents use grep command:
cat /etc/passwd | grep root  
cat /etc/passwd | grep –w root
cat /etc/passwd | grep ^root    ------------- To start with root
ls –l | grep ^-    ---- to list only files.
cat /etc/passwd | grep nologin$    ---- the word end  with nologin

To sort the output  use sort command:

cat names | sort   --- sorted output of names file.
cat names | sort | uniq  -- suppress the duplicate values.

cat names |sort|uniq |tee snames |more

tr command is used to translate


Senthilkumar Muthusamy

9865831007

No comments:

Post a Comment