linux shell scripting

23
MOHAMED ABUBAKAR SITTIK A 327218 Linux Shell Scripting

Upload: mohamed-abubakar-sittik-a

Post on 17-Jun-2015

427 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Linux shell scripting

MOHAMED ABUBAKAR SITTIK A327218

Linux Shell Scripting

Page 2: Linux shell scripting

Linux operating system

Page 3: Linux shell scripting

Linux Directory Structure

/bin – User Binaries Contains binary executables.Common linux commands you need to use in single-user modes are located under this directory.Commands used by all the users of the system are located here.For example: ps, ls, ping, grep, cp.

/ – RootEvery single file and directory starts from the root directory.Only root user has write privilege under this directory.Please note that /root is root user’s home directory, which is not same as /.

/sbin – System BinariesJust like /bin, /sbin also contains binary executables.But, the linux commands located under this directory are used typically by system aministrator, for system maintenance purpose.For example: iptables, reboot, fdisk, ifconfig, swapon

/etc – Configuration FilesContains configuration files required by all programs.This also contains startup and shutdown shell scripts used to start/stop individual programs.For example: /etc/resolv.conf, /etc/logrotate.conf

/dev – Device FilesContains device files.These include terminal devices, usb, or any device attached to the system.For example: /dev/tty1, /dev/usbmon0

/proc – Process InformationContains information about system process.This is a pseudo filesystem contains information about running process. For example: /proc/{pid} directory contains information about the process with that particular pid.This is a virtual filesystem with text information about system resources. For example: /proc/uptime

/var – Variable Filesvar stands for variable files.Content of the files that are expected to grow can be found under this directory.This includes — system log files (/var/log); packages and database files (/var/lib); emails (/var/mail); print queues (/var/spool); lock files (/var/lock); temp files needed across reboots (/var/tmp);

/tmp – Temporary Files

Directory that contains temporary files created by system and users.Files under this directory are deleted when system is rebooted.

/usr – User ProgramsContains binaries, libraries, documentation, and source-code for second level programs./usr/bin contains binary files for user programs. If you can’t find a user binary under /bin, look under /usr/bin. For example: at, awk, cc, less, scp/usr/sbin contains binary files for system administrator. If you can’t find a system binary under /sbin, look under /usr/sbin. For example: atd, cron, sshd, useradd, userdel/usr/lib contains libraries for /usr/bin and /usr/sbin/usr/local contains users programs that you install from source. For example, when you install apache from source, it goes under /usr/local/apache2

/home – Home Directories

Home directories for all users to store their personal files.For example: /home/john, /home/nikita

/boot – Boot Loader Files

Contains boot loader related files.Kernel initrd, vmlinux, grub files are located under /bootFor example: initrd.img-2.6.32-24-generic, vmlinuz-2.6.32-24-generic

/lib – System Libraries

Contains library files that supports the binaries located under /bin and /sbinLibrary filenames are either ld* or lib*.so.*For example: ld-2.11.1.so, libncurses.so.5.7

/opt – Optional add-on Applications

opt stands for optional.Contains add-on applications from individual vendors.add-on applications should be installed under either /opt/ or /opt/ sub-directory.

/mnt – Mount Directory

Temporary mount directory where sysadmins can mount filesystems.

/media – Removable Media Devices

Temporary mount directory for removable devices.For examples, /media/cdrom for CD-ROM; /media/floppy for floppy drives; /media/cdrecorder for CD writer

/srv – Service Data

srv stands for service.Contains server specific services related data.For example, /srv/cvs contains CVS related data.

Page 4: Linux shell scripting

Moving around the file system

pwd - Present Working Directory

$ pwd/home/srahuma$ cd test$ cd dir1$ cd ..$ cd $HOME$ cd ~rxbala2

cd - Change Directory

$ pwd/home/srahuma/test$

$$

$ pwd/home/srahuma/test/dir1$

$

$ pwd/home/srahuma/test$

$

$ pwd/home/srahuma$

$

$ pwd/home/rxbala2$

$

Page 5: Linux shell scripting

Listing directory contents

ls list a directory ls -l List a directory in long (detailed) format

For example:drwxr-xr-x 2 srahuma qdefault 4096 Jun 5 01:03 HL4IT1019

-rw-r--r-- 1 srahuma qdefault 4577 Aug 24 21:00 ftplog

ls -a List the current directory including hidden files. Hidden files start with "." ls -ld * List all the file and directory names in the current directory using long format.

Without the "d" option, ls would list the contents of any sub-directory of the current. With the "d" option, ls just lists them like regular files.

name time date size group owner number of links to file or directory contents

permissions for world permissions for members of group

permissions for owner of file: r = read, w = write, x = execute -=no permission type of file: - = normal file, d=directory, l = symbolic link, and others...

Page 6: Linux shell scripting

Changing file permissions and attributes

chmod 755 file Changes the permissions of file to be rwx for the owner, and rx for the group and the world. (7 = rwx = 111 binary. 5 = r-x = 101 binary)

chgrp user file Makes file belong to the group user

chown cliff file Makes cliff the owner of file.

chown -R cliff dir Makes cliff the owner of dir and everything in its directory tree.

You must be the owner of the file/directory or be root before you can do any of these things.

Page 7: Linux shell scripting

Moving, renaming and copying files

cp file1 file2 copy a file mv file1 newname move or rename a file mv file1 ~/AAA/ move file1 into sub-directory AAA in your home directory. rm file1 [file2 ...] remove or delete a file rm -r dir1 [dir2...] recursively remove a directory and its contents BE

CAREFUL! mkdir dir1 [dir2...] create directories mkdir -p dirpath create the directory dirpath, including all implied directories

in the path. rmdir dir1 [dir2...] remove an empty directory

Page 8: Linux shell scripting

Viewing and editing files

cat filename

Dump a file to the screen in ascii. more filename

Progressively dump a file to the screen: ENTER = one line down SPACEBAR = page down q=quit

less filename

Like more, but you can use Page-Up too. Not on all systems. head -n filename

Show the first n lines of a file. tail -n filename

Show the last n lines of a file. vi filename

Edit a file using the vi editor. All UNIX systems will have vi in some form.

Page 9: Linux shell scripting

Redirecting output

Echo “This is a sample text” > sample.txtEcho “This is the first line” > sample.txtEcho “Adding second line” >> sample.txt

Sample.txt

This is a sample text

Sample.txt

This is the first line

Sample.txt

This is the first lineAdding second line

Page 10: Linux shell scripting

Piping the output

cat sample.txt | head -1 | cut –d’ ‘ –f4

This is the first lineAdding second line

This is the first line

first

Page 11: Linux shell scripting

Command Substitution

You can use the output of one command as an input to another command in another way called command substitution. Command substitution is invoked when by enclosing the substituted command in backwards single quotes.

for example:

cat `find . -name aaa.txt`

which will cat ( dump to the screen ) all the files named aaa.txt that exist in the current directory or in any subdirectory tree.

You can also use this ` `(backward single quotes) to assign the result of a command to a variable

for example,

a=5

b=4

value=`expr $a \* $b`

Page 12: Linux shell scripting

Searching for files

find search_path -name filename

find . -name aaa.txt

Finds all the files named aaa.txt in the current directory or any subdirectory tree.

find / -name vimrc

Find all the files named 'vimrc' anywhere on the system.

find /usr/local/games -name "*xpilot*"

Find all files whose names contain the string 'xpilot' which exist within the '/usr/local/games' directory tree.

Page 13: Linux shell scripting

Searching in the files

Grep command is used to search for a specific pattern in a file. Displays the line with the matching pattern. It can be executed with more options.

grep -w <pattern> <filename>

Searches for the expression as a complete word, ignoring those matches that are substrings of larger words.

grep -i <pattern> <filename>

Searches for both uppercase and lowercase characters grep -v <pattern> <filename>

Inverts the search to display lines that do not match the pattern grep -n <pattern> <filename>

Precede each matching line with the line number

Page 14: Linux shell scripting

Searching in the files (contd.)

grep ’^pattern’ <filename>

Matches all lines beginning with “pattern”

grep ’pattern$’ <filename>

Matches all lines ending with “pattern”

grep ’p.....n’ <filename>

Matches lines containing a “p,” followed by Five characters, and followed by an “n” pattern

grep -c <pattern> <filename>

Prints only the total count of the matching lines

Page 15: Linux shell scripting

Archiving

The tar command stands for "tape archive". It is the "standard" way to read and write archives (collections of files and whole directory trees).

stuff.tar, or stuff.tar.gz

tar examples: tar cvf archive.tar file1 [file2...] Create a tar archive as a file "archive.tar" containing file1, file2...etc.

tar xvf archive.tar extract from the archive file

tar cvfz archive.tar.gz dname Create a gzip compressed tar archive containing everything in the directory 'dname'. This does not work with all versions of tar.

tar xvfz archive.tar.gz Extract a gzip compressed tar archive. Does not work with all versions of tar.

Page 16: Linux shell scripting

Compressing

The standard UNIX compression commands are compress and uncompress. Compressed files have a suffix .Z added to their name.

compress part.igs Creates a compressed file part.igs.Z uncompress part.igs Uncompresses part.igs from the compressed file

part.igs.Z. Note the .Z is not required.

Another common compression utility is gzip (and gunzip). gzip usually gives better compression than standard compress, but may not be installed on all systems. The suffix for gzipped files is .gz

gzip part.igs Creates a compressed file part.igs.gz gunzip part.igs Extracts the original file from part.igs.gz

Page 17: Linux shell scripting

Looking for help

Most of the commands have a manual page which give sometimes useful, often more or less detailed, sometimes cryptic and unfathomable descriptions of their usage.

man ls Shows the manual page for the ls command

You can search through the man pages using apropos

Example:

apropos build Shows a list of all the man pages whose descriptions contain the word "build"

type – command will give the directory that contains the executable of a Linux command.

type grep – Will give the result below

grep is a tracked alias for /bin/grep

Page 18: Linux shell scripting

Basics of vi editor

Opening a filevi filename

Creating text Edit modes: These keys enter editing modes and type in the text of your document.

i Insert before current cursor positionI Insert at beginning of current linea Insert (append) after current cursor positionA Append to end of liner Replace 1 characterR Replace mode<ESC>Terminate insertion or overwrite mode

Page 19: Linux shell scripting

Basics of vi editor (contd.)

Deletion of text

x Delete single characterdd Delete current line and put in bufferndd Delete n lines (n is a number) and put them in bufferJ Attaches the next line to the end of the current line (deletes carriage return).

Oops

u Undo last command

Page 20: Linux shell scripting

Basics of vi editor (contd.)

Cut and paste

yy Yank current line into buffer nyy Yank n lines into buffer p Put the contents of the buffer after the current line P Put the contents of the buffer before the current line

Cursor positioning

^d Page down ^u Page up :n Position cursor at line n :$ Position cursor at end of file ^g Display current line number h,j,k,l Left,Down,Up, and Right respectively.

Page 21: Linux shell scripting

Basics of vi editor (contd.)

String substitution

:n1,n2:s/string1/string2/[g] Substitute string2 for string1 on lines n1 to n2. If g is included (meaning global), all instances of string1 on each line are substituted. If g is not included, only the first instance per matching line is substituted.

^ matches start of line . matches any single character $ matches end of line

Examples:

:1,$:s/dog/cat/g Substitute 'cat' for 'dog', every instance for the entire file - lines 1 to $ (end of file)

:23,25:/frog/bird/ Substitute 'bird' for 'frog' on lines 23 through 25. Only the first instance on each line is substituted.

Page 22: Linux shell scripting

Basics of vi editor (contd.)

Saving and quitting and other "ex" commandsThese commands are all prefixed by pressing colon (:) and then entered in the lower left corner of the window. They are called "ex" commands because they are commands of the ex text editor - the precursor line editor to the screen editor vi. You cannot enter an "ex" command when you are in an edit mode (typing text onto the screen)

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

Page 23: Linux shell scripting

In our upcoming session

Variables ($# $1 …)External programsFunctionsEscape CharactersLoops – for loops and while loopsTest – if statementsCaseUsing awk and sedJob control and process management