Sunteți pe pagina 1din 16

UNIX Basics and Shell Scripting

September 20, 2012

1
An Overview
Open Source - It means the source code is free, any body can add the
functionality without disturbing the original functionality.

.
UNIX is interactive, Time-shared, Multi User, and Multi Tasking.

All the commands can be broadly classified as Internal and External Commands.

The commands which are executed by shell are called internal commands.eg.echo.

On the other hand the command those are not belongs to shell, that is whose
executables are stored in the /bin, /sbin, or in some other directory, are called
external commands. eg – cat.

2
Managing Directories and Files
Moving around the file system:
pwd – Show present working directory or current directory.
cd – Change to home directory
cd .. – Change to the parent directory

Creating a directory:
mkdir directory_name(s)
$ mkdir /home/user01/test #absolute path name
or

$ cd /home/user01
$ mkdir test #relative path name

Removing a directory:
rmdir directory_name(s)
**The directory must be empty

$ rmdir books
rmdir: books not empty

Recursively removing a directory along with its contents (Be cautious)


$ rm –r books

3
Listing Directories and Files
To list the contents of your current directory:
$ ls

To list all files, including hidden (.) files:


$ ls –a

To list all files to the end of the directory tree:


$ ls –R

To obtain more information about the files in a directory (Long Listing).


$ ls -l
total 2
d rwx rwx r-x 3 nadw dba 512 Oct 31 10:37 myDir
- rw- rw- r-- 1 nadw dba 0 Oct 31 10:37 myfile.txt
$

r = read w = write x = execute


rwx rwx rwx
user group others

The default protections for newly created files and directories are:
File -rw-r--r-- 644
Directory drwxr-xr-x 755

4
Create, View and Copying Files
Commands for creating and viewing text files:
cat - versatile command that is used to create, display and append text to files.
For creating a file
$ cat > file_name
For viewing a file
$ cat file_name
For appending content to a file
$ cat >> file_name
file contents.....
<ctrl-d>
Copying file: cp source_file destination_file

Moving, Renaming and Deleting


Deleting file:
rm [filename](s)

Remove the file myfile.txt


$rm myfile.txt
-r option is used to recursively delete all the contents

Renaming file (Moving):


mv source_file destination_file

5
VI Editor
Opening a file
vi filename

Creating text
Edit modes: These keys enter editing modes and type in the text of
your document.
» i Insert before current cursor position
» I Insert at beginning of current line
» a Insert (append) after current cursor position
» A Append to end of line
» r Replace 1 character
» R Replace mode
» <ESC> Terminate insertion or overwrite mode

Deletion of text
x Delete single character
dd Delete current line and put in buffer
ndd Delete n lines (n is a number) and put them in buffer
J Attaches the next line to the end of the current line (deletes
carriage return).

Oops
u Undo last command
6
Continues...
Press <ESC> to exit from an editing mode.
» :w Write the current file.
» :w new.file Write the file to the name 'new.file'.
» :w! existing.file Overwrite an existing file with the file currently being
edited.
» :wq Write the file and quit.
» :q Quit.
» :q! Quit with no changes.
» :e filename Open the file 'filename' for editing.
» :set number Turns on line numbering
» :set nonumber Turns off line numbering

7
Grep (globally search for a regular expression and print out
the line)
grep [options] pattern [file_name(s)]
It is used to select a line that has the pattern that is specified in the command.

Commonly used options


c --> displays the count on the number of occurrences
l --> displays the list of filenames alone
i --> ignore case while searching
v --> displays all that does not match the search criteria
w --> displays the whole word matching

8
Sed (Stream Editor)
If you want to write a program to make changes in a file, sed is the tool to use

This editor is an non-interactive one, which means we have to insert commands to be executed on the data at the command line
or in a script to perform editing process. The sed works by reading a file line by line and perform all actions specified . After all the
actions have taken place to that line, it reads the next line.

sed [options] '{command}' [filename]

The Substitute Command “s”, is the most common uses of the sed utility 's/{old value}/{new value}/‘

$ echo My name is Santhosh | sed 's/Santhosh/Sambath/'


Output
My name is Sambath

$>sed 's/^Thu /Thursday/' filename


It will turn "Thu " into "Thursday", but only at the beginning of the line

$>sed 's/ $//' filename


The above command will replace one space character that occurs at the end of a line.

The following command deletes the lines containing the word ‘cat’
$> sed '/cat/d' filename
“d” at the end stands for delete.

$>sed '$d' filename


deletes the last line of a file

$> sed '1,10d'


deletes the first 10 lines of file.

9
find
find path expression

$ find / -name Chapter1 -type f –print Find files with name Chapter1

$ find /usr/local -name "*.html" -type f –print Find files ending with .html

$ find . -name "[0-9]*" -print find files and dirs starting with a number
Text
$ find ~/dir1 -type d -name “[0-9]*” –print Find directories starting with a number

$ find . -mtime -2 –print Find files and dirs that is 2 days older or newer

$ find -name '*' -size +1000k –print FindText


files larger than 1000k

Text

Text

10
Other commands
finding who is on the system :
$ who

finger:
Displays information about the users currently logged on

Finding which you are :


$ who am i

Touch:
It updates the access and modification times of a file.
The command can also be used to create zero-length files.
$touch newfile.txt

print name of current host system


$hostname
$uname –a

Displays the total uptime for the computer


$uptime

Estimate file space usage


$du –k
$du –h

11
Other commands
Head [options] file(s)
Used to display the first few lines of a text file
$ head -n10 file_name (or) $ head -10 file_name
will display the first 10 lines from the top of the file specified

$ head -c 10 file_name
will display the first 10 bytes of data from the specified file

tail [options] file(s) - Used to display from the bottom of the file. Just opposite of the head' command
Commonly used options will have the same options that of the 'head' command

View growing log file in real time using tail command


$ tail -f /var/log/syslog

Cut
It extracts section from the lines specified
Commonly used options
c --> used to extract specific column in the file
$ cut -c -3,6-9,10- file_name
Will cut the fist 3 chars, 6th to 9th char and cut from 10th character till the end of the line
d --> used to specify the delimiter if any
f --> fields to be cut away
$cut -d $ -f 2,4 file_name
Will get to 2nd and 3rd field that are delimited by '$‘
Word Count
wc
-I Display no. of lines
-w Display no. of words
-c Display no. of character

12
Other commands
sort [options] [file_name]
It is used to sort the lines in the file.

Commonly used options


n --> numeric sort (for files having only numbers)
r --> reverse sort
t --> delimiter

$ sort -t "%" +2 -r file_name


Will sort the file_name based on the content of the 3 field in the line
separated by the delimiter in the reverse order

uniq [options] [file_name]


It is used to get the unique fields from the file

NOTE : 'uniq' requires a sorted file as the input

DIFF:

To compare the contents of two files:

diff email addresses

13
Other commands
No Hang Up:
The nohup command will prevent a process from being killed if you log
off the system before it completes:
$ nohup ls -R / > out &
[1] 59
$_
If you do not redirect output, nohup will redirect output to a file nohup.out:

$ nohup ls -R / &
[1] 61
Sending output to nohup.out
$
Alias:

Allows you to have a small more familiar command or name to execute a long string

alias [name=['command']]

$ alias p=‘ps –ef | grep java’


$ alias l=‘ls –l’

Show all alias definitions


$ alias
history='fc -l'
l='ls -l'
p='ps -ef‘
$l
-rw-r--r-- 1 joe staff 524 Sep 19 11:31 fleas 14
-rw-r--r-- 1 joe staff 1455 Jan 23 17:18 walrus
Other commands
sort [options] [file_name]
It is used to sort the lines in the file.

Commonly used options


n --> numeric sort (for files having only numbers)
r --> reverse sort
t --> delimiter

$ sort -t "%" +2 -r file_name


Will sort the file_name based on the content of the 3 field in the line
separated by the delimiter in the reverse order

uniq [options] [file_name]


It is used to get the unique fields from the file

NOTE : 'uniq' requires a sorted file as the input

DIFF:

To compare the contents of two files:

diff email addresses

15
Other commands
sort [options] [file_name]
It is used to sort the lines in the file.

Commonly used options


n --> numeric sort (for files having only numbers)
r --> reverse sort
t --> delimiter

$ sort -t "%" +2 -r file_name


Will sort the file_name based on the content of the 3 field in the line
separated by the delimiter in the reverse order

uniq [options] [file_name]


It is used to get the unique fields from the file

NOTE : 'uniq' requires a sorted file as the input

DIFF:

To compare the contents of two files:

diff email addresses

16