Tuesday, December 14, 2010

Learning UNIX - IV

________________________________________

Advance Unix Management
________________________________________

xyx:/>compress
-command compresses a file and returns the original file with .z extension, to uncompress this filename’s file use uncompress filename command.

Available Options
-bn limit the number of bits in coding to n
-c write to standard output
-f compress conditionally, do not prompt before overwriting files
-v Print the resulting percentage of reduction for files
________________________________________

xyx:/>uncompress
- command file uncompresses a file and return it to its original form.
uncompress filename.Z this uncompressed the compressed file to its original name.
-c write to standard output without changing files
________________________________________

xyx:/>cpio
- command is useful to backup the file systems. It copy file archives in from or out to tape or disk, or to another location on the local machine.

cpio flags [options]
It has three flags, -i, -o, -p
cpio -i [options] [patterns]
-> cpio -i copy in files who names match selected patterns
-> If no pattern is used all files are copied in
-> It is used to write to a tape
cpio -o
-> Copy out a list of files whose name are given on standard output
cpio -p
-> Copy files to another directory on the same system.
Options
-a reset access times of input files
-A append files to an archive (must use with -o)
-b swap bytes and half-words. Words are 4 bytes
-B block input or output using 5120 bytes per record
-c Read or write header information as Ascii character
-d create directories as needed
-l link files instead of copying
-o file direct output to a file
-r rename files interactively
-R ID reassign file ownership and group information to the user's login ID
-V print a dot for each file read or written
-s swap bytes
-S swap half bytes
-v print a list of filenames

Imp Examples
-> find . -name "*.old" -print | cpio -ocvB > /dev/rst8 will backup all *.old files to a tape in /dev/rst8
-> cpio -icdv "save"" < /dev/rst8 will restore all files whose name contain "save" -> find . -depth -print | cpio -padm /mydir will move a directory tree.
________________________________________

xyx:/>dump
- command is useful to backup the file systems, copies all the files in file system that have been changed after a certain date. This command is good for incremental backups on HP Unix
date is derived from /var/adm/dump dates and /etc/fstab.

/usr/sbin/dump [option [argument ...] file system]
Options where
where 0-9 dump level. 0 option causes entire file system to be dumped or copied
b blocking factor taken into argument
d density of tape, default value is 1600
f place the dump on next argument file instead of tape
________________________________________

xyx:/>pack
- command compacts each file and combine them together into a filename.z file. The original file is replaced. pcat and unpack will restore packed files to their original form.

Available Options
-p Print number of times each byte is used
-f Force the pack even when disk space isn't saved
To display Packed files in a file use pcat command
-> pcat filename.z
To unpack a packed file use unpack command as unpack filename.z
________________________________________

xyx:/>tar
- command creates an archive of files into a single file. tar copies and restore files to a tape or any storage media.

Examples:
xyx:/>tar cvf /dev/rmt/0 /bin /usr/bin creates an archive of /bin and /usr/bin, and store on the tape in /dev/rmt0.
xyx:/>tar tvf /dev/rmt0 will list the tape's content in a /dev/rmt0 drive.
xyx:/>tar cvf - 'find . -print' > backup.tar will creates an archive of current directory and store it in file backup.tar.

Functions and options:
c creates a new tape
r append files to a tape
t print the names of files if they are stored on the tape
x extract files from tap
b n use blocking factor of n
l print error messages about links not found
L is symbolic links
v print function letter (x for extraction or a for archive) and name of files.
________________________________________


xyx:/>mt
- command is used for tape and other device functions like rewinding, ejecting, etc. It give commands to tape device rather than tape itself. mt command is BSD command and is seldom found in system V unix versions.
syntax is
mt [-t tapename] command [count]
mt for HP-UX accept following commands.

Important Examples to use
mt -t /dev/rmt/0mnb rew will rewind the tape in this device
mt -t /dev/rmt/0mnb offl will eject the tape in this device

________________________________________

xyx:/>calendar
- command reads your calendar file and displays only lines with current day.

Examples:-
12/20 Test new software.
1/15 Test newly developed 3270 product.
1/20 Install memory on HP 9000 machine.
On dec 20th the first line will be displayed. you can use this command with your crontab file or in your login files.
________________________________________

xyx:/>nohup
- command, is used with prefix of any command, which continue running the command even if you shut down your terminal or close your login session.

xyz:/>nohup top
________________________________________

xyx:/>tty
- command is used to display terminal information.
-l will print the synchronous line number
-s will return only the codes: 0 (a terminal), 1 (not a terminal), 2 (invalid options) (good for scripts)
________________________________________

Handy for Unix Shell Programming
________________________________________

Shell programming concepts and commands:
Shell programming is an integral part of Unix operating system. It is command line user interface to Unix system, User have an option of picking an interface on Unix such as ksh, csh, or default sh, these are called shells. Shell programming is used to automate many tasks. ________________________________________

Bourne Shell (sh shell):-
sh or Bourne shell is default shell of Unix operating systems and is the simplest shell in Unix systems.

Ksh shell (Korn):-
Ksh or Korn shell is widely used shell.

csh or c shell:-
csh is second most used shell.

echo command
The echo utility writes its arguments, separated by BLANKs and terminated by a NEWLINE, to the standard output. If there are no arguments, only the NEWLINE character will be written.
echo is useful for producing diagnostics in command files, for sending known data into a pipe, and for displaying the contents of environment variables.

line command
The line utility copies one line (up to and including a new-line) from the standard input and writes it on the standard output. It returns an exit status of 1 on EOF and always prints at least a new-line. It is often used wwithin shell files to read from the user's terminal.

sleep command
The sleep utility will suspend execution for at least the integral number of seconds specified by the time operand.
Example 1: Suspending command execution for a time
To execute a command after a certain amount of time:
example% (sleep 105; command)&
Example 2: Executing a command every so often
example% while true
do
command
sleep 37
done

test command
The test utility evaluates the condition and indicates the result of the evaluation by its exit status. An exit status of zero indicates that the condition evaluated as true and an exit status of 1 indicates that the condition evaluated as false.

cc compiler (c programming language compiler).
Since Unix is itself is written in C programming language, most Unix operating systems come with c compiler called cc.

Tuesday, December 7, 2010

Learning UNIX - III

File Transfer and Communication Management

________________________________________

xyx:/>cu command, is used for communications over a modem or direct line with another Unix system.
________________________________________

xyx:/>ftp command is used to execute for files transfer over two systems.

-d enable debugging.
-g disable filename globing.
-i turn off interactive prompts.
-v verbose on. Show all responses from remote server.
________________________________________

xyx:/>login
command invokes a login session to a Unix box, System prompts you to enter userid and password.

xyx:/>rlogin command is used to log on to remote Unix systems, user must have permissions on both systems as well as same user id, or an id defined in .rhosts file.
xyx:/>rlogin host
________________________________________

xyx:/>talk
command used to invoke talk program available on all unix system which lets two users exchange information back and forth in real time.

xyx:/>talk userid@hostname
________________________________________

xyx:/>telnet - command invokes a telnet protocol which lets you log on to different unix, vms or any machine connected over TCP/IP protocol, IPx protocol or otherwise.

xyx:/>telnet host1
xyx:/> telnet
telnet>
________________________________________

xyx:/>vacation
command is used for out of office message to be send. It returns a mail message to sender announcing that you are on vacation.
/export/home/crk> vacation
This program can be used to answer your mail automatically
when you go away on vacation.
You need to create a message file in /export/home/crk/.vacation.msg first.
Please use your editor (vi) to edit this file.
"/export/home/crk/.vacation.msg" 4 lines, 130 characters
Subject: away from my mail
I will not be reading my mail for a while.
Your mail regarding "$SUBJECT" is read when I return.
N.B:- To disable this feature, use mail -F " "
-d will append the date to the log file.
-F user will forward mail to user when unable to send mail to mail file.
-l log file will record in the log file the names of senders who received automatic reply.
-m mail file will save received messages in mail file.
________________________________________

xyx:/>write
command initiate an interactive conversation with user.

xyx:/>write user1 tty
________________________________________
System reporting and Status Management
________________________________________

xyx:/>at command.
at command is also used like crontab command, to schedule jobs.

-f file execute commands in a file.
-m sends mail to user after job is completed.
-l list all jobs that are scheduled and their jobnumbers.
-r job number will remove specified jobs that were previously scheduled.
________________________________________

xyx:/>chmod Command is used to change permissions on a file.
xyx:/>ls -la test.txt
-rw-rw-rw- 1 qtr pmpcr 135 OCT 20 16:14 test.txt
-rw-rw-rw- meaning that owner can read and write file, member of the owner's group can read and write this file and anyone else connected to this system can read and write this file., next qtr is owner of this file pmpcr is the group of this file, there are 135 bytes in this file, this file was created on Oct 20 at time16:14 and at the end there is name of this file.
xyx:/>chmod 600 file1
________________________________________

xyx:/>chgrp command is used to change the group of a file or directory.
xyx:/>chgrp [options] newgroup files
Newgroup is either a group Id or a group name located in /etc/group .
Available Options:
-h will change the group on symbolic links.
-R recursively descend through directory changing group of all files and subdirectories.
________________________________________

xyx:/>chown command.
chown command to change ownership of a file or directory to one or more users.

xyx:/>chown options new owner files
Available important Options
-h will change the owner on symbolic links.
-R will recursively descend through the directory, including subdirectories and symbolic links.
________________________________________

xyx:/>crontab is used to schedule jobs. Permission to run this command should have been granted.
Normally Jobs are scheduled in five numbers, as follows.
Minutes 0-59
Hour 0-23
Day of month 1-31
month 1-12
Day of week 0-6 (0 is sunday)
Example
To schedule a job which runs from script named backup_jobs in /usr/local/bin directory on sunday (day 0) at 11.25 (22:25) on 15th of month.

* represents all values.
25 22 15 * 0 /usr/local/bin/backup_ xyx:/>jobs
The * here tells system to run this each month.
Syntax is
xyx:/>crontab file ## To create a file with the scheduled jobs like above
xyx:/>crontab file1 # to schedule job
________________________________________

xyx:/>date - command displays to days date, to use it type date at command prompt
xyx:/>date
Fri Oct 20 16:10:58 EDT 2010

________________________________________

xyx:/>df
df command displays information about mounted filesystems.
xyx:/>df
/ (/dev/md/dsk/d0 ): 3838540 blocks 431434 files
/proc (/proc ): 0 blocks 29848 files
/etc/mnttab (mnttab ): 0 blocks 0 files
/dev/fd (fd ): 0 blocks 0 files
/var (/dev/md/dsk/d20 ): 2559692 blocks 587286 files
/var/run (swap ):31292080 blocks 789270 files
/dev/vx/dmp (dmpfs ):31292080 blocks 789270 files
/dev/vx/rdmp (dmpfs ):31292080 blocks 789270 files
/tmp (swap ):31292080 blocks 789270 files
/opt (/dev/md/dsk/d30 ): 2509926 blocks 609248 files
/opt/crk (/dev/md/dsk/d40 ):32416940 blocks 2240301 files

Available Options
-b show only the number of free blocks.
-e show only the number of free files.
-k list allocation in kilobytes.
-l show only on local file systems.
-n list only the file system name type
________________________________________

xyx:/>du
du command displays disk usage.
________________________________________

xyx:/>env
env command displays all the variables set in your profile
________________________________________

xyx:/>finger - command used to display information about local and remote users.
xyx:/>finger user1@abc
________________________________________

xyx:/>ps
ps command is the most useful command for systems administrators. It reports information on active processes.
Available options.
-a Lists all processes in system except processes not attached to terminals.
-e Lists all processes in system.
-f Lists a full listing.
-j print process group ID and session ID.
xyx:/>ps -eafj |more
UID PID PPID PGID SID C STIME TTY TIME CMD
root 0 0 0 0 0 Sep 08 ? 0:19 sched
root 1 0 0 0 0 Sep 08 ? 2:51 /etc/init -r
root 2 0 0 0 0 Sep 08 ? 0:00 pageout
root 3 0 0 0 1 Sep 08 ? 634:05 fsflush
root 6817 1 6817 6817 0 Sep 08 ? 0:00 /usr/lib/saf/sac -t 3
00
root 590 1 590 590 0 Sep 08 ? 0:06 /usr/lib/inet/xntpd
root 689 1 0 0 0 Sep 08 ? 0:00 /sbin/sh - /usr/lib/v
xvm/bin/vxconfigbackupd
root 20 1 20 20 0 Sep 08 ? 3:48 vxconfigd -x syslog -
m boot
________________________________________

xyx:/>ruptime - command tells the status of local networked machines.

Available options.
-a include user even if they've been idle for more than one hour
-l sort by load average
-r reverse the sort order
-t sort by uptime
-i sort by number of users
________________________________________

xyx:/>shutdown command executed by root. To gracefully bring down a system, shutdown command is used.
-gn use a grace-period of n seconds (default is 60).
-ik tell the init command to place system in a state k.
s single-user state (default)
shutdown for power-off
1 like s, but mount multi-user file systems
5 stop system, go to firmware mode
6 stop system then reboot
-y suppress the default prompt for confirmation.
________________________________________

xyx:/>stty - command sets terminal input output options for the current terminal.
Without options stty reports terminal settings.

-a report all options.
-g report current settings.
Available Modes
0 hang up phone.
n set terminal baud.
erase keyname, will change your keyname to be backspace key.
________________________________________

xyx:/>who - command displays information about the current status of system.
who options file
Who as default prints login names of users currently logged in.
Available Options
-a use all options.
-b Report information about last reboot.
-d report expired processes.
-H print headings.
-p report previously spawned processes.
-u report terminal usage.
________________________________________
File Redirection Management
________________________________________
xyx:/>cal > cal.txt , create a new file called cal.txt that has calendar for current month.
> sign redirects output from stdout (screen) to a file. If file already exists, it will over right the content with latest information being redirected
>> will append the file if it is already existing

Monday, December 6, 2010

Learning UNIX - II

Comparison and Search Management
________________________________________

xyx:/>diff command. Compares the two files and print out the differences between them

Let two ascii text files file1 and file2
Contents of file1:
This is a test file to compare the file contents
This is a test file to compare the file contents
This is a test file to compare the file contents
This is a test file to compare the file contents

Contents of file2 contains
This is a second test file to compare the file contents
This is a test file to compare the file contents
This is a test file to compare the file contents
This is a test file to compare the file contents
xyx:/>diff file1 file2
0a1
> This is a second test file to compare the file contents
3d3
This is a test file to compare the file contents
________________________________________

xyx:/>cmp command. Compares the two files, similarly as diff.
xyx:/>cmp file1 file2
file1 file2 differ: char 12, line 1
________________________________________

xyx:/>dircmp Command compares two directories. Let us say dir1 and dir2 and each has 5-10 files in it. Then
xyx:/> dircmp dir1 dir2
Nov 20 14:14 2010 dir1 only and dir2 only Page 1
./userfile/duplicate_Tue.lo ./add_test_profiles.sh
./ userfile/duplicate2 _Tue_09 ./afiedt.buf
./ userfile/duplicate3 _Fri.log ./alter_user_profiles.sh
./ userfile/duplicate4 _Fri_113 ./kb.sh

________________________________________

xyx:/>grep Command is the most useful search command. Mostly used with combination with other commands like PS command, to find processes running on system, a pattern in a file, search one or more files to match an expression etc.

xyx:/> ps -ef | grep sleep will display all the sleep processes running in the system as follows.
root 15258 4190 0 14:21:25 ? 0:00 sleep 300
root 15890 15879 0 14:24:59 ? 0:00 sleep 360
_______________________________________

xyx:/>find command very useful for search of any file anywhere , provided that file and directory you are searching has read write permission set to you ,your group or all.
Some common Examples:

xyx:/>find $crk -print will lists all files in crk home directory.
xyx:/>find /crkdir -name client1 -print will list all files named client1 in /crkDir directory.
xyx:/>find / -type d -name 'man*' -print will list all manpage directories.
xyx:/>find / -size 0 -ok rm {} \; will remove all empty files on system.

_______________________________________

Text processing tools
________________________________________

xyx:/>cut command selects a list of columns or fields from one or more files and show on screen

-c is for character from first columns and -f for fields.
Example:
xyx:/>cut -c1,4 file1
ts
ts
ts
It is printing character of columns 1 and 4 of this file which contains t and s (part of this).

-c list cut the column positions identified in list.
-f list cut the fields identified in list.
-s used with -f to suppress lines without delimiters.
________________________________________

xyx:/>paste command merge the lines of one or more files into vertical columns separated by a tab.
xyx:/>paste file1 file2
This is a test file to compare the file contents This is a second test file to compare the file contents
This is a test file to compare the file contents This is a test file to compare the file contents
This is a test file to compare the file contents This is a test file to compare the file contents
This is a test file to compare the file contents This is a test file to compare the file contents
Let us take file1
this is firstline
and a file named testfile2 contains
this is testfile2
then running this command
paste testfile testfile2 > outputfile ##will put this into outputfile
this is firstline this is testfile2
it contains contents of both files in columns.
who | paste - - will list users in two columns.

Available imp Options:
-d'char' separate columns with char instead of a tab.
-s merge subsequent lines from one file.
________________________________________

xyx:/>sort command.
sort command sort the lines of a file or files, in alphabetical order. for example if you have a file named testfile with these contents
zzz
aaa
1234
yuer
wer
qww
wwe
Then running
> sort testfile
will give us output of

1234
aaa
qww
wer
wwe
yuer
zzz
Available Options:
-b ignores leading spaces and tabs.
-c checks whether files are already sorted.
-d ignores punctuation.
-i ignores non-printing characters.
-n sorts in arithmetic order.
-o file put output in a file.
+m[-m] skips n fields before sorting, and sort upto field position m.
-r reverse the order of sort.
-u identical lines in input file apear only one time in output.
________________________________________

xyx:/>uniq command.
uniq command removes duplicate adjacent lines from sorted file while sending one copy of each second file.
Examples

sort names | uniq -d will show which lines appear more than once in names file.
Available Options:
-c print each line once,
-d print duplicate lines once, but no unique lines.
-u print only unique lines.
________________________________________
xyx:/>awk and nwak command.
awk is more like a scripting language builtin on all unix systems. Although mostly used for text processing, etc.

Examples: to show total space in your system

xyz:/>df -t | awk 'BEGIN {tot=0} $2 == "total" {tot=tot+$1} END {print (tot*512)/1000000}'

Here the output of command df -t is being passed into awk which is counting the field 1 after pattern "total" appears. Same way if you change $1 to $4 it will accumulate and display the addition of field 4 which is used space.
________________________________________

xyx:/>sed command.
sed command launches a stream line editor which are used at command line.
you can enter your sed commands in a file and then using -f option edit your text file. It works as
sed [options] files
Available options:
-e 'instruction' editing instruction to the files.
-f script set of instructions from the editing script.
-n suppress default output.
_______________________________________

xyx:/>vi editor.
vi command launches a visual editor. vi editor is a default editor of all Unix systems. It has several modes. In order to write characters you will need to hit i to be in insert mode and then start typing. Make sure that your terminal has correct settings, vt100 emulation works good if you are logged in using pc.
Once you are done typing then to be in command mode where you can write/search/ you need to hit
Examples:-
Case I. To edit a file type
vi filename
:w filename to write
Case II. In case you are done writing and want to exit
:w! will write and exit.
Available options:
i for insert mode.

I inserts text at the cursor
A appends text at the end of the line.
a appends text after cursor.
O open a new line of text above the cursor.
o open a new line of text below the cursor.

: for command mode
to invoke command mode from insert mode.
:!sh to run unix commands.
x to delete a single character.
dd to delete an entire line
ndd to delete n number of lines.
d$ to delete from cursor to end of line.
yy to copy a line to buffer.
P to paste text from buffer.
nyy copy n number of lines.
:%s/string A/string B /g to replace string A with string B in whole file.
G to go to last line in file.
1G to go to the first line in file.
w to move forward to next word.
b to move backwards to next word.
$ to move to the end of line.
J join a line with the one below it.

/string to search string in file.
n to search for next occurrence of string.