Thursday, May 5

Basic Unix

A Bit of Unix Technical Structure
Unix is an operating system. An operating system is a set of programs that control and organize resources of a computer.
Unix is structured with several main components: the shell, utilities, the file system, and the kernel

Directory Structure
Types of Shell
Shell is a command interpreter and acts as an interface between a user and kernel

Types of Shells

         Bourne Shell
            /usr/bin/sh
         Korn Shell
            /usr/bin/ksh
         C Shell
            /usr/bin/csh      
         bash
            /bin/sh

o      File Permissions
o      Using file permission commands
For example you to change the file permission as

user   = full access
group = read and write
Other = read and write

You can use   chmod u=rwx,g=rx,o=rx temp.txt
                      chmod 755 temp.txt

#  Changing the owner of the file.

    -rw-rw-rw- 1 nvr483 staff 5252317 Jul 25 05:46 ids.zip

    bash$ chown vkp643:staff ids.zip

    -rw-rw-rw- 1 vkp643 staff 5252317 Jul 25 05:46 ids.zip

#  Changing the group for the file.

     chgrp staff filename

o      Types of files in UNIX
There are some file types in UNIX which is used for identifying what type of the file it is.

Types

- = Ordinary file
d = directory
l = link
p = pipe
b = block special device
c = character special device
m = Shared memory file

Example

-rw-r--r--     1 root  root    96 Jul 24 16:57 50k.txt
lrwxrwxrwx 1 root   root   33 Mar 26 11:13 file -> file1
drwxr-xr-x  1 root   root    96 Jul 29 06:47 tes

o    List of Environment Variables
Some of the Environment Variables


echo $LOGNAME                      User name used for login
echo $USER                 User name (sudo)
echo $UID                                 User name        Solaris
echo $SHELL                Directory containing OS shell executables
echo $LD_LIBRARY_PATH        Path to system and user libraries
echo $MANPATH                      Directory containing manuals
echo $HOME                Directory of user's home directory
echo $TZ                                  Time zone, such as "US/Mountain"
echo $PWD                              Stores the Present working Directory
echo $PS1                                Defines the system prompt
echo $IFS                                 Defines the Internal Field Separator
echo $PATH                  Directories containing executables, searched to find                                           applications when no absolute path is specified on the                                              command line.
o      King of the editor VI
Fundamentals of the vi Editor

            The vi editor is a model editor consisting of a command mode and an insert mode. To create a new file in vi. In prompt give vi filename.

Insert mode
            How to go into the insert mode open a new file like vi filename and press i then you will be there in insert mode. Then you can add the text into the file and remove the content too.

Command Mode
            Execute some commands in vi editor
o      Vi shortcut keys
Cursor movements      

        h - left one character
        l - right one character
        j - down one line
        k - up one line
        w - right one word
        b - back one word
        $ - to the end of line
        0 - to the beginning of the line
        } - right one paragraph
        { - left one paragraph
        Ctrl-F - forward one page
        Ctrl-B - back one page
        G - go to (without arguments, go to end of file)


o      Vi shortcut keys
Deleting

:nd   -  delete nth line
d$ - delete to end of line
d0 - delete to the beginning of the line
d} - delete to the end of paragraph
dd - delete the whole line
x - delete character cursor is on
dw – delete a word
o      Vi shortcut keys
Delete the lines
:nd – delete the nth line
:m,n   -  delete lines from m to n

Move the lines
:n mo p  -  Move line n after line p
:m,n mo p  - Move lines from m to n after line p


Copy the lines
:x co y  - copy m line after line y
:x,y co z  - cpoies lines from x to y after z

Create a new file
:m,n w filename  - create a new file with m to n lines.
:m,n w >> filename  - uppend the file with m to n lines.

Redirect to file
:r !command
:r filename Reads the content of the file
o      Patten search in vi editor
/^name
/[a-z]ood
/name$
/[A-a]pple

:%s/str1/str2/g
:m,n s/str1/str2/g

n – Repeats the search again
N – Repeats the search command in the opposite direction

:set nu -  Set display of line number on.
:set list – To check the space at the end of the lines in the file
:set mesg – Permit receipt of message from other terminal
:set nomesg - Don’t receive messages from other terminal


o      File compression
         You can compress the file in different ways, Here some commands are :

compress –v filename
uncompress filename

tar –cvf filename.tar filename
tar –xvf filename.tar
tar –tvf filename.tar – To list the files in tar file

zip filename.zip filename
zip –r foldername foldername   -  For folder you have to give –r because it has to zip the                                       files inside the folder

pack filename
unpack filename
o      Messaging in UNIX
There are different ways you can sent a message to the user from your ID

mail username

         Once you have given mail and user name, You can type the message what you want to send to the user. When you have done with the message press crtl + D

mailx is the other command which you can send a file to your mail and you can send a zip file to your mail.

Ex: mailx -s subject nvr483@setti.com < file.txt
Ex: uuencode file.zip file.zip | mailx -s subject nvr483@setti.com"

write username

         Type the message and give the crtl + D the message will go to the user.

Wall “meassage”
         This command is user to broadcast the message to all the people who ever login to the UNIX box.
If you don’t want to get any message in your terminal, You can use the below command
mesg -n

o      Disk related commands
df –k   Disk  free

Filesystem            kbytes    used   avail capacity  Mounted on
/                       20971520 5652131 14362397    29%    /
/dev                  20971520 5652131 14362397    29%    /dev
/export/home/12396483 2173857 10098662    18%    /export/home/shared
/iplanet            83886080 5365111 73613416     7%    /iplanet

du –sh Disk usage  in some places this will work.


You can check the size of the files using ls –lh command


ulimit - This feature allows us to control the number of processes an existing user on the server may be authorized to have. To improve performance, we can safely set the limit of processes for the super-user root to be unlimited.

o      Who are all in our UNIX box
We can check how many people login in our server using some commands

who
bash-3.00$ who
nagios     pts/1        May 31 20:16    (qftr87-01.host.com)
nagios     pts/6        Jul 29 21:53    (10.232.24.167)

whoami – This will work in Linux

W
bash-3.00$ w
9:53pm  up 194 day(s),  8:13,  2       users,  load average: 1.01, 1.12, 1.14
User       tty                login@   idle   JCPU   PCPU  what
nagios   pts/6            9:53pm                                  w

finger
bash-3.00$ finger
nvr483   Aravind Ramani-nvr48  pts/439          Thu 20:57  10.232.24.167
hbk867   VasanthaKumar Karupp  pts/46       14: Thu 01:27  10.12.129.23
xwc768   Kavitha Ramachandran  pts/174     1:42 Thu 19:10  jrpc84-02.host.com
cqrd38   Mahendran A           pts/42         6 Thu 08:53  10.12.129.160


o      Scheduled Task
o      How to check what is going on in UNIX box
To check what are all the process is running in UNIX box.
ps –ef
 UID   PID  PPID   C    STIME TTY      TIME CMD
root  1696  1031   0   Jan 16 ?          15:27    /usr/lib/inet/inetd start
root  1103  1031   0   Jan 16 ?           0:48    /sbin/init
root  1031  1031   0   Jan 16 ?           0:00    zsched
daemon  1515  1031   0   Jan 16 ?     10:00  /usr/lib/crypto/kcfd
root  1210  1031   0   Jan 16 ?          21:20   /lib/svc/bin/svc.configd

vmstat 1 5
vmstat is to check the Report virtual memory statistics

iostat 1 5
The iostat command is used for monitoring system input/output device

prstat 
The prstat utility iteratively examines all active processes on the system and reports statistics based on the selected output mode and sort order
o      Some commands
Swap Space - Disk memory used to hold data that is not in Real or File System memory.  Swap space is most efficient when it is on a separate disk or partition, but sometimes it is just a large file in the File System

bash-2.03$ swap -l
swapfile             dev  swaplo blocks   free
/dev/vx/dsk/bootdg/swapvol 275,59002     16 6948560 1637568

bash-2.03$ swap -s
total: 3385384k bytes allocated + 136312k reserved = 3521696k used, 895184k available

grep
The grep stands for globally regular expression print. It lets us to search particular patterns in a file

grep < word > < file_name >

options

- i      Ignores the case
- n     Lists the line numbers along with matching patters
- v     Lists all the lines that do not match the patters
- l      Lists only filenames that contain match
o      Some Commands
netstat –n
Local Address        Remote Address    Swind Send-Q Rwind Recv-Q    State
-------------------- -------------------- ----- ------ ----- ------ -----------
10.177.4.221.32788   10.177.4.221.10389   49152      0 49152      0 ESTABLISHED
10.177.4.221.10389   10.177.4.221.32788   49152      0 49314      0 ESTABLISHED
10.177.4.221.32790   10.177.4.221.10389   49152      0 49152      0 ESTABLISHED
10.177.4.221.389     10.2.132.8.32874     49640      0 49640      0 ESTABLISHED
In windows
10.232.24.167:1171     10.232.29.201:1080     TIME_WAIT
10.232.24.167:1172     10.232.29.201:1080     TIME_WAIT
10.232.24.167:1173     10.232.29.201:1080     TIME_WAIT
10.232.24.167:1174     10.232.29.201:1080     ESTABLISHED

top – To check the usgae of cpu and memory.
load averages:  0.11,  0.16,  0.22                                                                                                                  21:26:55
1457 processes:1442 sleeping, 1 running, 9 zombie, 4 stopped, 1 on cpu
CPU states: 95.6% idle,  0.4% user,  4.0% kernel,  0.0% iowait,  0.0% swap
Memory: 2048M real, 220M free, 2586M swap in use, 807M swap free

  PID  USERNAME THR PRI NICE  SIZE   RES   STATE   TIME    CPU  COMMAND
26567 nvr483          1  28    0     2496K  1776K  cpu/1   0:00    1.17%  top
1884   root               6  34    0     181M   177M  sleep   779.6H  1.03%  esd
8578   root               8  33    0      35M    18M    sleep   31.5H    0.90%  BESClient




o      Some Commands
cal - To check the calendar cal command is used. If you see in any year september will be having 30 days except in 1752.

September 1752
 S  M Tu  W Th  F  S
            1  2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

You can get calendar for any year in UNIX.
cal yyyy
cat mm yyyy

cut command
Using this command we can take the particular field from any file.

cut –fx –d” “ filename
o      Some commands
wc - The wc command stands for " word count". It reads one or more input files and, by     default, writes the number of newline characters, words and bytes contained in each input file to the standard output.

Option
  -l stands for count line

Pipe |
         Unix commands alone are powerful, but when you combine them together, you can accomplish complex tasks with ease. The way you combine Unix commands is through using pipes and filters.

Example : if you want to collect the number of files in a folder. You can use combination of commands with using pipe.
ls –l | wc -l


ptree – Process tree
       The ptree utility prints the process trees containing the specified pids or users, with child processes indented from their respective parent processes. An argument of all digits is taken to be a process-ID, otherwise it is assumed to be a user login name.
Example:
            bash-2.03$ ptree 12914
                        567   /usr/sbin/inetd -s
                          12914 in.telnetd
                            12916 -ksh
                              13024 ssh t54k605 -l oracle

nohup - nohup is most often used to run commands in the background as daemons. Once you rune the command if you logout also the command will be running in background.

Example : nohup script &
o      Commands
To stop the background process or any process which is running in the system.

kill is command you stop or kill the process.

kill pid
Kill -9 pid - To kill the process forcefully

nice – Nice is a command to increase or decrease the priority of the process. Default id +15.

Example : nice +20 pid.

tail – This command is used to check the last 10 lines of the file. To check the currently updating file you have to use –f option.
Example : tail –f filename.

head - This command is used to check the first 10 lines of the file.
Example : head filename
o      Command
awk
The Unix command awk is another powerful filter. You can use awk to manipulate the contents of a file. Here is an example:
$ cat basket.txt | awk -F= '{print $1}'
Layer1
Layer2
Layer3
Layer4
Layer5                  
$ cat basket.txt | awk -F= '{print "HAS: " $2}'
HAS:  cloth
HAS:  strawberries
HAS:  fish
HAS:  chocolate
HAS:  punch cards

Just to view the file content. In this command you can’t change or edit the file content.

pg filename
more filename
o      Commands
If you copying some text files from windows to UNIX. If you open the file you can able to see ^M at the end of the file.

Example :
1060ARAN@setti.com   1060ARAN^M
1060DE@setti.com     1060DE^M

To replace this kind of issue you can use dos2unix command
dos2unix filename > newfilename

bc – The calculator in UNIX.
Unix keeps a very useful feature handy. The calculator called bc. Type bc in your shell prompt.

Example :
bash-2.03$ bc
100/2
50
quit


2 comments: