You are on page 1of 33

UNIX

Manasa Panda
Accelerant Software

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
UNIX

 Overview of UNIX
 Why UNIX ?
 Basic UNIX commands
 vi editor
 Advanced UNIX
 Shell scripting

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Overview
What’s UNIX ?
 UNIX is the name of a computer operating system
and its family of related utility programs.
 First developed in the 1960s

What’s an Operating System?


An operating system is a control program for a
computer. It schedules tasks and allocates
computer resources e.g. CPU, memory, disk/tape
storage, printers, terminals etc.

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Contd.
Names of some Op. Systems:
SUN : SunOS, Solaris
SGI : IRIX
IBM : AIX
HP : HP-UX

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Contd.
Advantages: Disadvantages:

1. Robust 1. Expensive
2. Reliable. 2. System administration needs
special skill
3. Scalable, multi-user 3. Multiple vendors

4. Multiple vendors

5. Can run on different platforms

6. Good for CPU intensive applications.

7. Better process/resource* control. *resource: cputime, filesize, datasize

8. “Open System” memorysize, descriptors, coredumpsize

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Basic Unix Commands

 ls - list the contents


 pwd - present working directory
 cat - prints the file in console
-combine individual sequence files into one file
cat seq1 seq2 > seq

 cd - change directory
 mkdir <dirname>- create the directory
 mv - move a file or directory
 cp - copy a file or directory
 rm - remove a file or directory

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Basic Unix Commands

 df - to check disk space ;


Usage: df -k ., df -t /home (t =total)
 diff - Find the difference between two files.
Usage: diff file1 file2.

 dircmp - compares two dirs.


 du - to check disk usage. flags: s (summary), a(all)
Usage: du /home/sam , du -a /home/sam
 echo - echoes string/environment variables to console.

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Basic Unix Commands

 exit - to logout from a shell, to exit from a shell script.


 head - Display first 10 lines(default) of a file.
Usage: head -10 filename
 tail - 30 - Display last 30 lines of a file.
 history - List the previous commands issued in shell.
 hostname - to get the host name

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Basic Unix Commands

 kill -9 kills the job with that ID


 ls - list (flags l, t, r, F, R)
usage: ls - lists all the files and directories
ls - ltr - lists all the files and directories with
time stamp in reversed order
ls *.dat - lists all the *.dat files in current dir.

ls -R dir1 - lists dir1 recursively.


 mail / mailx -
usage: mailx -s “subject” address@here < file.
 mkdir - create a new directory
usage: mkdir dir_name

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Contd.

more - to see the content of the file.


usage: more filename
press v to go into vi editor.
pattern search: /pattern - forward search
?pattern - backward search
press q to exit from more.
 mv - move
usage: mv filename filename_new
 ps - process status
 pwd - present working directory.

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Contd.

 rm - Remove file (flag i, interactive )


usage: rm -i filename , other flags : r, f
 rmdir - Remove directory.
usage: rm -r dirname
 ssh hostname - remote login - logging into remote system

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Contd.

tee - to display as well as save the output of a


command to a file.

Usage: ls -l | tee list.11152012_110234.log >>  daily.log


(the above will write to daily.log and also create a new log
file list_11152012_110234.log ) 

wc - word count, usage: wc filename

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Contd.

whoami - log in details of the user (effective current user)


gzip/gunzip - to compress a file.
Usage: gzip filename - creates a file called filename.gz
gunzip filename.gz - restores the file filename
Redirection of output :
command_name_1 > log1 : output is written to log1 file
command_name_2 >> log1 : o/p gets appended to log1
 Pipe - pipe operator is used as a connector of two
commands.
Usage: whoami | wc

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Contd.
up/down arrows scroll up and down through most recent
commands
left/right arrows use for editing current line
grep takes only one search string.
grep can be used with these flags - c, i, v, n.
c = count; i= ignore cases ; v =inverse, n = line number

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Contd.
 grep - command searches the given file for lines containing a
match to the given strings or words
Usage:
grep ‘string1’ filename
grep ‘string1 string2’ filename
cat filename | grep ‘string1’
more filename |grep ‘string1’
more filename |grep -i‘string1’ (-i -to ignore word case)
grep -i ‘string1’ filename
grep -i ‘string1’ *.csv (look for the string in all csv in the
present working directory)
grep -i ‘string1’ */*.csv (look for the string in all csv in
all directory)

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Contd.
 find - The find command is used to locate files in Unix

Usage:
find . (will display the pathnames of all files in the current
directory and all subdirectories)
find / -name “filename” (look for the file in the all directories)
find pathname -name “filename”

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Contd.
du
The du command outputs the number of kilobytes used by each
subdirectory. Useful if you have gone over quota and you want to
find out which directory has the most files. In your home-
directory, type
% du -s *
The -s flag will display only a summary (total size) and the *
means all files and directories.
gzip
This reduces the size of a file, thus freeing valuable disk space.
For example, type
% ls -l science.txt
and note the size of the file using ls -l . Then to compress
science.txt, type
Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Contd.
% gzip science.txt
This will compress the file and place it in a file
called science.txt.gz
To see the change in size, type ls -l again.
To expand the file, use the gunzip command.
% gunzip science.txt.gz

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
File Transfer
tar / untar - flags c (copy), x(extract),v(verbose), f(path)
usage: tar cvf dirname2.tar dirname1

tar xvf dirname2.tar


FTP - related commands : bin, mget, mput, cd,
lcd (local cd) etc.
Ex. ftp <server name>
ftp> mget *

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Shell Scripts
The shell is also a programming language that executes shell
scripts in interpretive mode i.e. one line at a time.
Bourne shell (sh) - original shell that came with UNIX.
Korn shell(ksh), C shell(csh), tcsh(super set of C-shell) - shells
developed later.

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Shell Scripts
#!/usr/bin/bash
echo 'Hello'
echo 'This is my first script'
echo 'Bye'

status=$?
echo "Exit Status : $status"
if [[ "$status" != "0" ]]
then
exit $status
fi

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
vi Editor
 Create a new file/open an old file using vi editor :
Command: vi filename
 Write to a file using vi editor :
Write mode: press ‘i’ or ‘a’, then start typing.
 Go to command mode: press ‘ESC’
 Save/abort a file:
Save: press : then w ; Abort: press : the q!
 Search for a string in a file in vi editor:
press / then type the search string
 Go to the end of a line: in command mode type $
 Go to the beginning of a line: in command mode press 0

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Contd.
 Go to nth line of a file: press : then n
 Go to the end of a file: press ‘G’ or press : then $
 Show line numbers of a file: press : then ‘set number’
 Remove line numbers: press : then ‘set nonumber’
 Delete a single character: press x when the cursor is on it.
 Delete a line: press d twice on the line to be deleted.
 Delete n lines: press n and then press d twice.
 Copy n lines: press n and then press y twice, then press p
at the location you want to paste it.
 Undo the last change: press u

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Contd.
 Replace a character : press r, then the new character
 To navigate the file: press h (for moving towards left)
press l (for moving towards right)
press k (for moving up)
press j (for moving down)

 To repeat the last command: press . (dot)


 To join two lines: press J
 To recover a file after crash : vi -r filename
 To open a file with cursor at nth line: vi +n filename
 To open a file with cursor at the eof : vi + filename

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Contd.
ls
The ls command ( lowercase L and lowercase S ) lists the contents of your current working directory.

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Contd.
Command Meaning
ls list files and directories
list all hidden files and
ls -a directories starting with dot
mkdir make a directory
cd directory change to named directory
cd change to home-directory
cd ~ change to home-directory
cd .. change to parent directory
display the path of the current
pwd directory

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc
Contd.
Command Meaning
cp file1 file2 copy file1 and call it file2
mv file1 file2 move or rename file1 to file2
rm file remove a file
rmdir directory remove a directory
display a file(cat file1 grep science science.txt
cat file file2>file3 grep Science science.txt
less file /o display a file a page at a time grep -i science science.txt
display the first few lines of a grep -i 'spinning top'
head -5 file file science.txt.
display the last few lines of a
tail file file Some of the other options of grep
search a file for keywords grep are:
grep 'keyword' file H file1(grep is case sensitive -v display those lines that do NOT
command) match 
count number of -n precede each matching line with
lines/words/characters in file the line number 
wc file wc -l science.txt -c print only the total count of
wc -w science.txt
matched lines
Accelerant Confidential
Contd.
Command Meaning
cat > list1
redirect standard output to pear
command > file a file
banana
append standard output to apple
command >> file a file ^D {this means press [Ctrl] and [d]
redirect standard input to stop}.
command < file from a file cat list1

command1 | command pipe the output of


command1 to the input of cat >> list1
2 command2
cat file1
cat list1 list2 > biglist
concatenate file1 and file2
file2 > file0 to file0  cat biglist
sort sort data
sort < biglist
list users currently logged
who in cat file1 | grep science

Accelerant Confidential
Contd.
The * wildcard
The character * is called a wildcard, and will match against none or more character(s)
in a file (or directory) name. For example, in your unixstuffdirectory, type
ls list*
This will list all files in the current directory starting with list....
Try typing
ls *list1
This will list all files in the current directory ending with ....list
The ? wildcard
The character ? will match exactly one character.
So ?ouse will match files like house and mouse, but not grouse. 
Try typing
ls list?

On-line Manuals
For example, to find out more about the wc (word count) command, type
man wc

Accelerant Confidential
Contd.
Access rights on files
r (or -), indicates read permission (or otherwise), that is, the presence or absence of
permission to read and copy the file 
w (or -), indicates write permission (or otherwise), that is, the permission (or otherwise)
to change a file 
x (or -), indicates execution permission (or otherwise), that is, the permission to
execute a file, where appropriate
Symbol Meaning chmod (changing a file mode
u user chmod go-rwx biglist
g group
chmod a+rw biglist
o other
chmod 777 file1
a all
r read
w write (and delete)
execute (and access
x
directory)
+ add permission

- take away
permission
Accelerant Confidential
Contd.
Processes and Jobs
ps

Running background processes


sleep 5
To run sleep in the background
sleep 5 &
Listing suspended and background processes
jobs
kill (terminate or signal a process)
sleep 10 &
jobs
kill %1
History
history

df
The df command reports on the space left on the file system. For example, to find out
how much space is left on the fileserver, type
df .

Accelerant Confidential
Contd.
du
The du command outputs the number of kilobyes used by each subdirectory. you want
to find out which directory has the most files. In your home-directory, type
du -s *
The -s flag will display only a summary (total size) and the * means all files and
directories.
gzip
This reduces the size of a file, thus freeing valuable disk space. For example, type
ls -l science.txt
and note the size of the file using ls -l . Then to compress science.txt, type
gzip science.txt
This will compress the file and place it in a file called science.txt.gz
To see the change in size, type ls -l again.
To expand the file, use the gunzip command.
gunzip science.txt.gz
zcat
zcat will read gzipped files without needing to uncompress them first.
zcat science.txt.gz
If the text scrolls too fast for you, pipe the output though less .
zcat science.txt.gz | less
Accelerant Confidential
UNIX Emulator
 Download Mobaxterm – an UNIX emulator to practice UNIX
commands on your windows OS, from the following URL :

http://mobaxterm.mobatek.net/download.html

Accelerant Confidential
Please do not copy or redistribute without written consent of Accelerant Software, Inc

You might also like