basic unix commands

47
Linju C John Jerin Mathew George S5 MCA

Upload: swtjerin4u

Post on 16-Jul-2015

106 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Basic unix commands

Linju C John

Jerin Mathew George

S5 MCA

Page 2: Basic unix commands

date: Used to display the current system date and time.

Eg:

[vijai@localhost vijai]$ date

Sat Apr 9 13:03:47 IST 2011

date +%D: Displays date only.

Eg:

[vijai@localhost vijai]$ date +%D

04/09/11

General Commands

Page 3: Basic unix commands

date +%T: Displays time only

[vijai@localhost vijai]$ date +%T

13:05:33

date +% Y: Displays the year part of date

[vijai@localhost vijai]$ date +%Y

2011

date +% H: Displays the hour part of time

[vijai@localhost vijai]$ date +%H

13

Page 4: Basic unix commands

cal: Calendar of the current month

[vijai@localhost vijai]$ cal

April 2011

Su Mo Tu We Th Fr Sa

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

Page 5: Basic unix commands

cal year: Displays calendar for all months of the specified year

[vijai@localhost vijai]$ cal 1800

1800

#shows full calender of 1800

Page 6: Basic unix commands

cal month year: Displays calendar for the specified month of the year

[vijai@localhost vijai]$ cal 08 1998

August 1998

Su Mo Tu We Th Fr Sa

1

2 3 4 5 6 7 8

9 10 11 12 13 14 15

16 17 18 19 20 21 22

23 24 25 26 27 28 29

30 31

Page 7: Basic unix commands

who: Login details of all users such as their IP, Terminal No, User name

[vijai@localhost vijai]$ who

root :0 Apr 9 08:41

vijai pts/0 Apr 9 13:00 (scl-64)

cse1 pts/3 Apr 9 13:18 (scl-41.smkfomra.com)

ecea pts/4 Apr 9 13:18 (scl-29.smkfomra.com)

who am i: Used to display the login details of the user

[vijai@localhost vijai]$ who am i

vijai pts/0 Apr 9 13:00 (scl-64)

Page 8: Basic unix commands

tty: Used to display the terminal name

[vijai@localhost vijai]$ tty

/dev/pts/0

uname: Displays the Operating System

[vijai@localhost vijai]$ uname

Linux

uname –r: Shows version number of the OS (kernel).

[vijai@localhost vijai]$ uname -r

2.4.20-8smp

Page 9: Basic unix commands

uname –n: Displays domain name of the server

[vijai@localhost vijai]$ uname -n

localhost.localdomain

echo "txt“: Displays the given text on the screen

[vijai@localhost vijai]$ echo "How are you"

How are you

Page 10: Basic unix commands

echo $HOME: Displays the user's home directory

[vijai@localhost vijai]$ echo $HOME

/home/vijai

[vijai@localhost vijai]$ echo $USER

vijai

Page 11: Basic unix commands

bc: Basic calculator. Press Ctrl+d to quit

[vijai@localhost vijai]$ bc

bc 1.06

Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.

3+5

8 2%3

2

Page 12: Basic unix commands

man cmdname: Manual for the given command. Press q to exit

history: To display the commands used by the user since log on.

exit: Exit from a process. If shell is the only process then logs out

Page 13: Basic unix commands

pwd: Path of the present working directory

[vijai@localhost loops]$ pwd

/home/vijai/shellscripts/loops

mkdir dir: A directory is created in the given name under the current directory

mkdir dir1 dir2: A number of sub-directories can be created under one stroke

[vijai@localhost vijai]$ mkdir filter

[vijai@localhost vijai]$ ls

filter list.sh regexpr shellscripts

Directory Commands

Page 14: Basic unix commands

cd subdir: Change Directory. If the subdir starts with / then path starts from root (absolute) otherwise from current working directory.

[vijai@localhost vijai]$ cd shellscripts/loops/

[vijai@localhost loops]$

[vijai@localhost loops]$ cd

[vijai@localhost vijai]$

Page 15: Basic unix commands

cd: To switch to the home directory.

[vijai@localhost loops]$ cd /

[vijai@localhost /]$

cd /: To switch to the root directory.

[vijai@localhost /]$cd/home/vijai/shellscripts/loops/

Page 16: Basic unix commands

cd ..: To move back to the parent directory

[vijai@localhost loops]$ cd ..

[vijai@localhost shellscripts]$

rmdir subdir: Removes an empty sub-directory.

[vijai@localhost vijai]$ rmdir filter

[vijai@localhost vijai]$ ls

list.sh regexpr shellscripts

Page 17: Basic unix commands

cat > filename: To create a file with some contents. To end typing press Ctrl+d. The > symbol means redirecting output to a file. (< for input)

[vijai@localhost vijai]$ cat > greet

hi s5mca

wishing u the best

cat filename: Displays the file contents.

[vijai@localhost vijai]$ cat greet

hi s5mca

wishing u the best

File Commands

Page 18: Basic unix commands

cat >> filename: Used to append contents to a file

[vijai@localhost vijai]$ cat >> greet

bye

[vijai@localhost vijai]$ cat greet

hi s5mca

wishing u the best

bye

Page 19: Basic unix commands

cp src des: Copy files to given location. If already exists, it will be overwritten

[vijai@localhost vijai]$ cp greet ./regexpr/

[vijai@localhost vijai]$ ls

greet list.sh regexpr shellscripts

[vijai@localhost vijai]$ ls ./regexpr

demo greet

cp –r src des: Copies the entire directory, all its sub-directories and files.

cp –i src des: Warns the user prior to overwriting the destination file

[vijai@localhost vijai]$ cp -i greet ./regexpr/

cp: overwrite 'greet'? n

Page 20: Basic unix commands

mv old new: To rename an existing file or directory. –ioption can also be used

mv f1 f2 f3 dir: To move a group of files to a directory.

mv –v old new: Display name of each file as it is moved.

[vijai@localhost vijai]$ mv greet greet.txt

[vijai@localhost vijai]$ ls

greet.txt list.sh regexpr shellscripts

[vijai@localhost vijai]$ mv greet.txt ./regexpr/

[vijai@localhost vijai]$ ls

list.sh regexpr shellscripts

[vijai@localhost vijai]$ ls ./regexpr/

demo greet.txt

Page 21: Basic unix commands

rm file: Used to delete a file or group of files. –i option can also be used

rm *: To delete all the files in the directory.

rm –r *: Deletes all files and sub-directories

rm –f *: To forcibly remove even write-protected files

[vijai@localhost vijai]$ rm -i *.sh

rm: remove regular file `fact.sh'? y

rm: remove regular file `list.sh'? n

rm: remove regular file `prime.sh'? y

[vijai@localhost vijai]$ ls

list.sh regexpr shellscripts

Page 22: Basic unix commands

ls: Lists all files and subdirectories (blue colored) in sorted manner.

[vijai@localhost vijai]$ ls

greet list.sh regexpr shellscripts

ls name: To check whether a file or directory exists.

ls name*: Short-hand notation to list out filenames of a specific pattern.

ls –a: Lists all files including hidden files (files beginning with .)

[vijai@localhost vijai]$ ls -a

. .bash_logout .canna .gtkrc regexpr .viminfo.tmp

.. .bash_profile .emacs .kde shellscripts .xemacs

.bash_history .bashrc greet list.sh .viminfo

Page 23: Basic unix commands

ls –x dirname: To have specific listing of a directory.

ls –R: Recursive listing of all files in the subdirectories

ls –l: Long listing showing file access rights (read/write/execute-rwx for user/group/others-ugo).

[vijai@localhost vijai]$ ls -l

total 16

-rw-rw-r-- 1 vijai vijai 32 Apr 11 14:52 greet

-rw-rw-r-- 1 vijai vijai 30 Apr 4 13:58 list.sh

drwxrwxr-x 2 vijai vijai 4096 Apr 9 14:30 regexpr

drwxrwxr-x 7 vijai vijai 4096 Apr 4 14:57 shellscripts

Page 24: Basic unix commands

cmp file1 file2: Used to compare two files. Displays nothing if files are identical.

[vijai@localhost vijai]$ cmp list.sh fact.sh

list.sh fact.sh differ: byte 1, line 1

wc file: It produces a statistics of lines (l), words(w), and characters(c).

[vijai@localhost vijai]$ wc list.sh

4 9 30 list.sh

[vijai@localhost vijai]$ wc -l list.sh

4 list.sh

Page 25: Basic unix commands

chmod perm file: Changes permission for the specified file. (r=4, w=2, x=1, Owner=u, Group=g, Others=o)

[vijai@localhost vijai]$ ls -l list.sh

-rw-rw-r-- 1 vijai vijai 30 Apr 4 13:58 list.sh

[vijai@localhost vijai]$ chmod ug+x list.sh

[vijai@localhost vijai]$ ls -l list.sh

-rwxrwxr-- 1 vijai vijai 30 Apr 4 13:58 list.sh

[vijai@localhost vijai]$ chmod 740 list.sh

[vijai@localhost vijai]$ ls -l list.sh

-rwxr----- 1 vijai vijai 30 Apr 4 13:58 list.sh

Page 26: Basic unix commands

Simple Filters

Page 27: Basic unix commands

head: Used to display the first few records (10 records by default)

[vijai@localhost filters]$ head stud

20057801|Aarthi |ECE |CTS |36000

20057702|Albert Jerry |CSE |Wipro |25000

20057903|Arun |IT |Ramco |12000

20057904|Diwakar |IT |TCS |10500

20057705|Geetha |CSE |Infosys |23000

20057806|Irudayaraj |ECE |Polaris |30000

20057707|Jaya Prakash |CSE |Ramco |28000

20058008|Mahesh |EEE |Microsoft |5000

20057909|Manimaran |IT |Microsoft |9000

20058010|Mohammed Mukthar |EEE |Oracle |6000

Page 28: Basic unix commands

head -5: stud Displays first 5 records

[vijai@localhost filters]$ head -4 stud

20057801|Aarthi |ECE |CTS |36000

20057702|Albert Jerry |CSE |Wipro |25000

20057903|Arun |IT |Ramco |12000

20057904|Diwakar |IT |TCS |10500

head -1 stud | wc –c: length of first record

[vijai@localhost filters]$ head -1 stud | wc -c

49

Page 29: Basic unix commands

Tail: Used to display the last few records (10 records by default)

[vijai@localhost filters]$ tail stud

20058008|Mahesh |EEE |Microsoft |5000

20057909|Manimaran |IT |Microsoft |9000

20058010|Mohammed Mukthar |EEE |Oracle |6000

20057711|Prithivi Rajan |CSE |Ramco |25000

20057712|Pushpak Chander |CSE |CTS |27500

20057713|Ramesh |CSE |Wipro |24000

20057817|Smitha |ECE |Ramco |30000

20057718|Sri Gurumoorthy |IT |Microsoft |11000

20057719|Tamil Selvi |EEE |CTS |3500

20057720|Thamotharan |IT |CTS |9000

Page 30: Basic unix commands

tail -5 stud | tee last5: Last 5 records listed & stored in file last5 using tee

[vijai@localhost filters]$ tail -2 stud | tee last2

20057719|Tamil Selvi |EEE |CTS |3500

20057720|Thamotharan |IT |CTS |9000

[vijai@localhost filters]$ cat last2

20057719|Tamil Selvi |EEE |CTS |3500

20057720|Thamotharan |IT |CTS |9000

Page 31: Basic unix commands

cut: Used to extract specific fields. The d option specifies the delimiter and f for specifying the field list. The c option may be used if extraction is done character wise

cut –d \| -f 1,3,4 stud Fields 1,3,4 listed

cut –d \| -f 2-4 stud Fields 2,3,4 listed

paste –d \| list1 list2 merges two cut files list1 and list2

Page 32: Basic unix commands

[vijai@localhost filters]$ cut -d \| -f 1-3 stud20057801|Aarthi |ECE20057702|Albert Jerry |CSE20057903|Arun |IT20057904|Diwakar |IT20057705|Geetha |CSE20057806|Irudayaraj |ECE20057707|Jaya Prakash |CSE20058008|Mahesh |EEE20057909|Manimaran |IT20058010|Mohammed Mukthar |EEE20057711|Prithivi Rajan |CSE20057712|Pushpak Chander |CSE20057713|Ramesh |CSE20057817|Smitha |ECE20057718|Sri Gurumoorthy |IT20057719|Tamil Selvi |EEE20057720|Thamotharan |IT

Page 33: Basic unix commands

[vijai@localhost filters]$ cut -d \| -f 2,4 stud > nameorg[vijai@localhost filters]$ cut -d \| -f 5 stud > sal[vijai@localhost filters]$ paste -d \| nameorg salAarthi |CTS |36000Albert Jerry |Wipro |25000Arun |Ramco |12000Diwakar |TCS |10500Geetha |Infosys |23000Irudayaraj |Polaris |30000Jaya Prakash |Ramco |28000Mahesh |Microsoft |5000Manimaran |Microsoft |9000Mohammed Mukthar |Oracle |6000Prithivi Rajan |Ramco |25000Pushpak Chander |CTS |27500Ramesh |Wipro |24000Smitha |Ramco |30000Sri Gurumoorthy |Microsoft |11000Tamil Selvi |CTS |3500Thamotharan |CTS |9000

Page 34: Basic unix commands

sort: reorders the file as per ASCII sequence. The t option is used to specify delimiter sort stud Sorted on 1st column by default

[vijai@localhost filters]$ sort stud20057702|Albert Jerry |CSE |Wipro |2500020057705|Geetha |CSE |Infosys |2300020057707|Jaya Prakash |CSE |Ramco |2800020057711|Prithivi Rajan |CSE |Ramco |2500020057712|Pushpak Chander |CSE |CTS |2750020057713|Ramesh |CSE |Wipro |2400020057718|Sri Gurumoorthy |IT |Microsoft |1100020057719|Tamil Selvi |EEE |CTS |350020057720|Thamotharan |IT |CTS |900020057801|Aarthi |ECE |CTS |3600020057806|Irudayaraj |ECE |Polaris |3000020057817|Smitha |ECE |Ramco |3000020057903|Arun |IT |Ramco |1200020057904|Diwakar |IT |TCS |1050020057909|Manimaran |IT |Microsoft |900020058008|Mahesh |EEE |Microsoft |500020058010|Mohammed Mukthar |EEE |Oracle |6000

Page 35: Basic unix commands

sort –t \| +2 stud: Sort as per 3rd column[vijai@localhost filters]$ sort -t \| +1 stud20057801|Aarthi |ECE |CTS |3600020057702|Albert Jerry |CSE |Wipro |2500020057903|Arun |IT |Ramco |1200020057904|Diwakar |IT |TCS |1050020057705|Geetha |CSE |Infosys |2300020057806|Irudayaraj |ECE |Polaris |3000020057707|Jaya Prakash |CSE |Ramco |2800020058008|Mahesh |EEE |Microsoft |500020057909|Manimaran |IT |Microsoft |900020058010|Mohammed Mukthar |EEE |Oracle |600020057711|Prithivi Rajan |CSE |Ramco |2500020057712|Pushpak Chander |CSE |CTS |2750020057713|Ramesh |CSE |Wipro |2400020057817|Smitha |ECE |Ramco |3000020057718|Sri Gurumoorthy |IT |Microsoft |1100020057719|Tamil Selvi |EEE |CTS |350020057720|Thamotharan |IT |CTS |9000

Page 36: Basic unix commands

nl:display file content with lines numbered. The s option is used to specify separator

nl –s "|" stud Displays entries numbered with separator |[vijai@localhost filters]$ nl -s "|" stud1|20057801|Aarthi |ECE |CTS |360002|20057702|Albert Jerry |CSE |Wipro |250003|20057903|Arun |IT |Ramco |120004|20057904|Diwakar |IT |TCS |105005|20057705|Geetha |CSE |Infosys |230006|20057806|Irudayaraj |ECE |Polaris |300007|20057707|Jaya Prakash |CSE |Ramco |280008|20058008|Mahesh |EEE |Microsoft |50009|20057909|Manimaran |IT |Microsoft |900010|20058010|Mohammed Mukthar |EEE |Oracle |600011|20057711|Prithivi Rajan |CSE |Ramco |2500012|20057712|Pushpak Chander |CSE |CTS |2750013|20057713|Ramesh |CSE |Wipro |2400014|20057817|Smitha |ECE |Ramco |3000015|20057718|Sri Gurumoorthy |IT |Microsoft |1100016|20057719|Tamil Selvi |EEE |CTS |350017|20057720|Thamotharan |IT |CTS |9000

Page 37: Basic unix commands

tr: Translates characters. Can be used to change text case. It works with standard input <

tr '[a-z]' '[A-Z]' < stud: Changes text to upper case[vijai@localhost filters]$ tr '[a-z]' '[A-Z]' < stud20057801|AARTHI |ECE |CTS |3600020057702|ALBERT JERRY |CSE |WIPRO |2500020057903|ARUN |IT |RAMCO |1200020057904|DIWAKAR |IT |TCS |1050020057705|GEETHA |CSE |INFOSYS |2300020057806|IRUDAYARAJ |ECE |POLARIS |3000020057707|JAYA PRAKASH |CSE |RAMCO |2800020058008|MAHESH |EEE |MICROSOFT |500020057909|MANIMARAN |IT |MICROSOFT |900020058010|MOHAMMED MUKTHAR |EEE |ORACLE |600020057711|PRITHIVI RAJAN |CSE |RAMCO |2500020057712|PUSHPAK CHANDER |CSE |CTS |2750020057713|RAMESH |CSE |WIPRO |2400020057817|SMITHA |ECE |RAMCO |3000020057718|SRI GURUMOORTHY |IT |MICROSOFT |1100020057719|TAMIL SELVI |EEE |CTS |350020057720|THAMOTHARAN |IT |CTS |9000

Page 38: Basic unix commands

Regular Expression

Page 39: Basic unix commands

grep this demo: Lists the lines that contains the string this

[vijai@localhost regexpr]$ grep this demo

this line is the 1st lower case line in this file.

Two lines above this line is empty.

grep 'end of' demo: Quotes mandatory for text containing space

[vijai@localhost regexpr]$ grep 'end of' demo

1. e - go to the end of the current word.

2. E - go to the end of the current WORD.

grep this demo* Search this in multiple files

Page 40: Basic unix commands

grep –c to demo: Number of occurrence of the word to in the file

[vijai@localhost regexpr]$ grep -c to demo

5

grep –n sequence demo: Display line numbers along with matching lines

[vijai@localhost regexpr]$ grep -n sequence demo

15:WORD - WORD consists of a sequence of non-blank characters

16:Word - word consists of a sequence of letters, digits and underscores.

Page 41: Basic unix commands

grep –v word demo: Displays lines that does not contain the text word

[vijai@localhost regexpr]$ grep -v word demoTHIS LINE IS THE 1ST UPPER CASE LINE IN THISFILE.this line is the 1st lower case line in this file.This Line Has All Its First Character Of The Word With Upper Case.Two lines above this line is empty.vim Word Navigation2. E - go to the end of the current WORD.4. B - go to the previous WORD.WORD - WORD consists of a sequence of non-blank characterstelnet 172.16.4.256

Page 42: Basic unix commands

grep –i WORD demo: Search for text ignoring case differences

[vijai@localhost regexpr]$ grep -i WORD demoThis Line Has All Its First Character Of The Word

With Upper Case.vim Word NavigationYou may want to do several navigation in relation to

words, such as:1. e - go to the end of the current word.2. E - go to the end of the current WORD.3. b - go to the previous word.4. B - go to the previous WORD.WORD - WORD consists of a sequence of non-blank

charactersWord - word consists of a sequence of letters, digits

and underscores.

Page 43: Basic unix commands

grep '^[0-9]' demo: Lines that start with a number

[vijai@localhost regexpr]$ grep '^[0-9]' demo

1. e - go to the end of the current word.

2. E - go to the end of the current WORD.

3. b - go to the previous word.

4. B - go to the previous WORD.

grep '[0-9]$' demo: Lines that end with a number

[vijai@localhost regexpr]$ grep '[0-9]$' demo

telnet 172.16.4.256

Page 44: Basic unix commands

grep "2....$" stud: Display lines that ends in the range 20000–29999

egrep "lower|UPPER" demo: Display lines that match either lower or upper

[vijai@localhost regexpr]$ egrep "lower|UPPER" demo

THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.

this line is the 1st lower case line in this file.

Page 45: Basic unix commands

egrep "(previous|current)word" demo: Display lines that match either previous word or current word

[vijai@localhost regexpr]$ egrep"(previous|current) word" demo

1. e - go to the end of the current word.

3. b - go to the previous word.

touch: Create a new file.

[vijai@localhost regexpr]$ touch jmg

[vijai@localhost regexpr]$ ls

jmg demo

Page 46: Basic unix commands

kill: Terminates a process.

kill [options] <process-number>

$kill -9 absolute kill.

Page 47: Basic unix commands