Unix/Linux Basic Commands and Shell Script

27,885 views

Published on

Overview of Linux Basic Commands and shell Script

Published in: Education, Technology
13 Comments
97 Likes
Statistics
Notes
No Downloads
Views
Total views
27,885
On SlideShare
0
From Embeds
0
Number of Embeds
48
Actions
Shares
0
Downloads
0
Comments
13
Likes
97
Embeds 0
No embeds

No notes for slide

Unix/Linux Basic Commands and Shell Script

  1. 1. LINUX
  2. 2. UNIX History <ul><li>Unix was originally developed for internal use at AT&T by Ken Thompson and Dennis Ritchie. </li></ul><ul><li>First version created in Bell Labs-1969. </li></ul><ul><li>Unix flavors are AIX from IBM, HP/UX from Hewlett Packard SunOs from Sun and IRIX from SGI </li></ul>
  3. 3. UNIX Principles <ul><li>Everything is a file-Including hardware </li></ul><ul><li>By treating everything as a file, a consistency emerges.You can secure access to hardware in the same as you secure access to document </li></ul><ul><li>Configuration data stored in text </li></ul><ul><li>Storing Configuration in text allows an administrator to move a configuration from one machine to another easily. </li></ul><ul><li>Small,single-purpose programs </li></ul><ul><li>Unix provides many small utilities that perform one task very well. </li></ul><ul><li>Avoid captive user Interfaces </li></ul><ul><li>Most commands expect their options and arguments to be typed on the command line when the command is launched. </li></ul><ul><li>Ability to chain programs together to perform complex tasks </li></ul><ul><li>A core design of UNIX is that output of one program can be input for another. </li></ul>
  4. 4. GNU Project/GPL <ul><li>Gnu project started in 1984 </li></ul><ul><li>Goal: To create “Free” UNIX clone </li></ul><ul><li>Free Software Foundation </li></ul><ul><li>“ Free Software” doesn’t refer to the cost of the software, but the fact that end user has free to modify and change the program. </li></ul><ul><li>GPL -GNU General Public License </li></ul><ul><li>Primary License for Open Source software </li></ul><ul><li>Encourage free software </li></ul>
  5. 5. Linux Origins <ul><li>Linux Torvalds </li></ul><ul><li>Finnish college student in 1991 </li></ul><ul><li>Created Linux Kernel </li></ul><ul><li>When Linux kernel combined with GNU applications,complete free UNIX- OS possible. </li></ul>
  6. 6. Linux Principles <ul><li>Fresh implementation of UNIX APIs </li></ul><ul><li>Open source development model </li></ul><ul><li>Multi-user and Multi-tasking </li></ul><ul><li>Linux is a multi-user and multi-tasking operating system. That means more than one person can be logged on to the same Linux computer at the same time. </li></ul><ul><li>Multitasking, a user can have more than one process at the same time </li></ul><ul><li>Supports wide variety of hardware </li></ul><ul><li>Supports most piece of modern X86-Compatible PC hardware. </li></ul><ul><li>Supports many configuration protocols and configurations </li></ul><ul><li>Fully Supported </li></ul>
  7. 7. Hardware Specifications <ul><li>Pentium-class or better CPU </li></ul><ul><li>128 MB or more RAM </li></ul><ul><li>2-6 GB disk space depending on intended use </li></ul><ul><li>Bootable CD-ROM or 3 ½” floppy drive </li></ul>
  8. 8. Overview of Linux <ul><li>Components of the Linux User Environment </li></ul><ul><li>Local Logins </li></ul><ul><li>Device files </li></ul><ul><li>Getting Help </li></ul><ul><li>Date, Time and Calendar </li></ul><ul><li>Virtual Consoles </li></ul>
  9. 9. Component of the Linux User Environment <ul><li>Kernel </li></ul><ul><li>Its jobs is to perform many low-level and system-level functions, to interpret and send instructions to the system hardware </li></ul><ul><li>Shell </li></ul><ul><li>It provides an interface by which the user can communicate with the kernel. </li></ul><ul><li>Terminal Emulator </li></ul><ul><li>It gives the shell a place to accept typed commands to display the results. </li></ul><ul><li>X Window System </li></ul><ul><li>It Provides the bitmapped color display and the environment in which program such as web browser, mail clients can display the window </li></ul><ul><li>Desktop Environment </li></ul><ul><li>It is the collection of Software applications with which users can interact to accomplish their tasks. </li></ul>
  10. 10. Local Logins <ul><li>Basically user can Login in 2 modes </li></ul><ul><li>1. Text-mode login </li></ul><ul><li>It nothing but, user switching to runlevel 3 environment </li></ul><ul><li>2. Graphical Login </li></ul><ul><li>It nothing but, User switching to runlevel 5 environment </li></ul>
  11. 11. Device Files <ul><li>Linux access hardware devices through files.All hardware devices are treated as “special files” </li></ul><ul><li>Basically there are two types </li></ul><ul><li>1. Character devices </li></ul><ul><li>2. Block devices </li></ul><ul><li>Character devices: </li></ul><ul><li>Character devices, such as serial ports and modems, transfer data one byte at a time. </li></ul><ul><li>Block Devices: </li></ul><ul><li>Block devices such as hard drives, transfer data in blocks. Block typically range in size from 512 bytes to 32 KB . </li></ul>
  12. 12. Getting Help <ul><li>man displays pages from reference manual. </li></ul><ul><li>Manual page contain multiple section </li></ul><ul><li>1. User commands </li></ul><ul><li>2. System calls </li></ul><ul><li>3. Library calls </li></ul><ul><li>4. Special files </li></ul><ul><li>5. File Formats </li></ul><ul><li>6. Games </li></ul><ul><li>7. Miscellaneous </li></ul><ul><li>8. Administrative commands </li></ul><ul><li>Other Help Utilities </li></ul><ul><li>info - Text based help system that organizes its information into sections. </li></ul>
  13. 13. Date Time and Calendar <ul><li>Date - display date and time </li></ul><ul><li>Cal -Prints an ASCII character of the current month </li></ul><ul><li>cal 8 2005 </li></ul><ul><li>display calendar of the 8 month of the year 2005. </li></ul>
  14. 14. Virtual Consoles <ul><li>Multiple non-GUI logins are possible through the use of Virtual consoles. </li></ul><ul><li>There are by default 6 available virtual consoles </li></ul><ul><li>Virtual console available through Ctrl-Alt-F7 </li></ul>
  15. 15. File and Shell Basics
  16. 16. File Information <ul><li>Files names may be upto 255 characters </li></ul><ul><li>File names are case-sensitive. </li></ul><ul><li>Files and directories on Linux system can be named by any combination of letters, numbers and most punctuation symbols. </li></ul><ul><li>pwd- Displays the absolute path to the current directory </li></ul><ul><li>Location of directory can be specified by two methods </li></ul><ul><li>Absolute pathname : </li></ul><ul><li>Absolute pathname starts with /. It is complete road map to a location. </li></ul><ul><li>Relative Pathname: </li></ul><ul><li>Relative pathname do not begin with /. Specifies location relative to your current working directory. </li></ul>
  17. 17. Changing & Listing Directories <ul><li>cd –change directories </li></ul><ul><li>cd /home/joshua/work : To an absolute path </li></ul><ul><li>cd .. : To a directory one level up </li></ul><ul><li>cd : To your home directory </li></ul><ul><li>cd - : To your previous working directory </li></ul><ul><li>Listing Directory contents </li></ul><ul><li>ls :Lists the contents of the current directory or a specified directory </li></ul><ul><li>ls –l :long listing of contents of information in directory </li></ul><ul><li>ls –a :listing of hidden files and directories whose file begin with a dot. </li></ul><ul><li>ls –R :recurses through the subdirectory it encounters,listing the contents too. </li></ul><ul><li>ls –d :lists directory names </li></ul>
  18. 18. System Directories <ul><li>/bin, /usr/bin : User commands </li></ul><ul><li>/sbin, /usr/sbin : administrator commands </li></ul><ul><li>/var : logs,PID files, mail </li></ul><ul><li>/proc : “virtual window” into the kernel </li></ul><ul><li>/etc : Configuration files </li></ul><ul><li>/lib : shared libraries </li></ul><ul><li>/dev : device files </li></ul><ul><li>/boot : Linux kernel and boot files </li></ul><ul><li>/home : user’s home directories </li></ul><ul><li>/opt : third party packages </li></ul>
  19. 19. Checking Free Space <ul><li>df : Reports filesystem disk space usage in kilobytes, megabyte and gigabyte </li></ul><ul><li>du –h : displays filesystem information in human readable format </li></ul><ul><li>du - Estimated file space usage. </li></ul><ul><li>-h : makes the output in “human readable” format </li></ul><ul><li>-s : To summarize space in a directory </li></ul>
  20. 20. Copying Files and directories <ul><li>cp -copy files and directories </li></ul><ul><li>Syntax: </li></ul><ul><li>cp [options] source file destination file </li></ul><ul><li>cp [options] source files destination file </li></ul><ul><li>cp –r : recursive copy </li></ul><ul><li>cp –p: Preserve time and date information when making a copy </li></ul><ul><li>cp –f : forceful copy of file to destination file </li></ul>
  21. 21. Moving and Removing files and directories <ul><li>mv –move and/or rename files and directories </li></ul><ul><li>mv-must always be given at least two arguments. </li></ul><ul><li>Syntax: </li></ul><ul><li>mv [ options ] source file destination </li></ul><ul><li>mv [ options ] source files… destination </li></ul><ul><li>mv and copy functions are identical, the only difference is that cp-results in matching identical files; with mv, the source disappears leaving only the destination files </li></ul>
  22. 22. Creating & Removing files and directories <ul><li>touch - create empty files or update file timestamps </li></ul><ul><li>mkdir - create a directory </li></ul><ul><li>rmdi r- remove an empty directory </li></ul><ul><li>rm – remove files </li></ul><ul><li>-I : interactive </li></ul><ul><li>-r : recursive </li></ul><ul><li>-f :force </li></ul>
  23. 23. Types of viewing file <ul><li>cat –contents are displayed sequentially with no break. </li></ul><ul><li>less - Displays the contents of a text file one screen at a time </li></ul><ul><li>Syntax: </li></ul><ul><li>less [options] [filename] </li></ul><ul><li>tail - Displays last few lines of a text in a file. </li></ul><ul><li>Use –n or – lines to change number of lines displayed. </li></ul><ul><li>Syntax: </li></ul><ul><li>tail [ options] [ filename ] </li></ul><ul><li>Head : Displays first few lines of text in a file. </li></ul><ul><li>Syntax: </li></ul><ul><li>head [ options ] [ filename ] </li></ul>
  24. 24. Users and Group Information
  25. 25. Users <ul><li>Every user of the system is assigned a unique User ID </li></ul><ul><li>User names and uids are stored in /etc/passwd </li></ul><ul><li>Users are assigned a home directory and a program that is run when they login </li></ul><ul><li>Users cannot read, write and execute each other files without permission </li></ul>
  26. 26. Groups <ul><li>Users are assigned to groups with unique group ID number ( gid ) </li></ul><ul><li>gid are stored in /etc/group </li></ul><ul><li>Each user is given their own private group. They are also added to other group to gain additional access. </li></ul><ul><li>All user in a group can share files that belong to the group. </li></ul>
  27. 27. Root User <ul><li>Root user is administrative account on Linux system. </li></ul><ul><li>Root is sometime called super user. </li></ul><ul><li>Root has complete control over the system. </li></ul><ul><li>Root has complete control over the system. </li></ul><ul><li>When operating as a root, small typo of rm command can irretrievably delete all the files on the system. </li></ul><ul><li>Server programs such as web or print servers typically run as privileged users, not as root. Because running program in this way limits the amount of damage </li></ul>
  28. 28. User Information Commands <ul><li>whoami: </li></ul><ul><li>Find out who you are </li></ul><ul><li>Groups, id : </li></ul><ul><li>Use to find out what group you belong to </li></ul><ul><li>Users, who , w: </li></ul><ul><li>Find out who is logged in </li></ul><ul><li>Last : </li></ul><ul><li>Login/reboot history </li></ul><ul><li>Finger: </li></ul><ul><li>User information lookup </li></ul><ul><li>Su: To start a new shell as a different user </li></ul>
  29. 29. File Access Permissions
  30. 30. Linux File security <ul><li>Every file is owned by a user and associated with a group </li></ul><ul><li>Access to a file or directory is determined by the read, write and execute permission for its user and group ownership and for “others” </li></ul><ul><li>File permission can be viewed by ls –l </li></ul><ul><li>File type and file access permissions are symbolized by a 10-character string </li></ul>
  31. 31. User categories <ul><li>File permission file access in three general categories. Each category is denoted by one symbol </li></ul><ul><li>U- the file’s owner </li></ul><ul><li>G-other users in the file’s group </li></ul><ul><li>O- everyone else </li></ul>
  32. 32. File Permission Types <ul><li>r – Permission to read a file or list or directory contents. </li></ul><ul><li>w- Permission to write or change the file or create and remove files from the directory </li></ul><ul><li>x- Permission to execute a program </li></ul><ul><li>Each of the standard permission types can be used to a restrict access for the file’s user, access for the file’s group and access for everyone else. </li></ul><ul><li>A file may be removed by anyone who has write permission to the directory in which the file resides regardless the ownership or permission on the file itself. </li></ul>
  33. 33. Examining user, group and other permission <ul><li>User Permissions: </li></ul><ul><li>Characters 2,3 and 4 in the access mode identify user permission </li></ul><ul><li>-rwx------ 1 root root 1024 Aug 17:10 README </li></ul><ul><li>Group Permissions </li></ul><ul><li>Characters 5,6 and 7 in the access mode identify group permission </li></ul><ul><li>----rwx--- 1 student visitor 1024 Aug 18.00 README </li></ul><ul><li>Other Permission: </li></ul><ul><li>Character 8,9 and 10 in the access mode identify permission for others </li></ul><ul><li>-------rwx 1 student visitor 1024 Aug 18.10 README </li></ul><ul><li>This file can be read and written and executed by people in visitor. </li></ul>
  34. 34. Changing Access Permission <ul><li>Each mode can be either be a numerical expression, or can be defined by combining the letter for who has access to file, an operator for selecting how the permissions should be changed, and permission for the file or directory. </li></ul><ul><li>Who may be chosen from </li></ul><ul><li>u- Users who owns the file </li></ul><ul><li>g- Users in the file Group </li></ul><ul><li>o- Others users </li></ul><ul><li>a- All three category </li></ul><ul><li>Operator may be chosen from Permission may be chosen from </li></ul><ul><li>+ add a permission r- read </li></ul><ul><li>_ Remove a permission w- write </li></ul><ul><li>= Assign a permission x- Execute </li></ul><ul><li>s- set user ID </li></ul><ul><li>t– sticky bit </li></ul>
  35. 35. Changing Access Permissions <ul><li>chmod- to change access modes </li></ul><ul><li>Syntax: </li></ul><ul><li>chmod [ mode ] filename… </li></ul><ul><li>Eg: </li></ul><ul><li>chmod g+w .bashrc </li></ul><ul><li>chmod u=rw bashrc </li></ul><ul><li>chmod –R : </li></ul><ul><li>This mode traverse an entire directory tree to change the permission of all its files and directories </li></ul>
  36. 36. Changing Permissions-Numeric method <ul><li>Numeric method uses three-digit mode number </li></ul><ul><li>First digit represents owner permission </li></ul><ul><li>Second digit represents group permission </li></ul><ul><li>Third digit represents other’s permission </li></ul><ul><li>Permission are calculated by adding </li></ul><ul><li>4- read </li></ul><ul><li>2-write </li></ul><ul><li>1-execute </li></ul><ul><li>Syntax: </li></ul><ul><li>chmod [ mode ] filename </li></ul><ul><li>Eg: </li></ul><ul><li>chmod 644 file : </li></ul><ul><li>Read and write permission set for the owner and group; read only for others </li></ul>
  37. 37. Special Permission for Executables <ul><li>Apart from three user categories, there is fourth permission category that sets the special permission </li></ul><ul><li>set-user-ID(suid) </li></ul><ul><li>set-group-ID(sgid) </li></ul><ul><li>There are displayed for “s” for user or group in executable position ‘ x ’ </li></ul>
  38. 38. Linux Filesystem
  39. 39. ext2/ext3 Filesystem Details <ul><li>SuperBlocks: </li></ul><ul><li>When an ext2 or ext3 filesystem is created, the filesystem specific metadata is stored in a superblock. Because this data is critical to the operations of the filesystem, copies of the filesystem superblock are also created. </li></ul><ul><li>Dumpe2fs: </li></ul><ul><li>It used to view much of the data stored in a filesystem superblock. </li></ul><ul><li>Inodes: </li></ul><ul><li>Inodes act as maps or descriptions to the data contained in the various filesystem block. While inodes to not store the actual data of a file,they do not contain information about the file. </li></ul>
  40. 40. Links <ul><li>Hard Links: </li></ul><ul><li>Hard links enable one file to have two or more names. A user can create a link to a file that he does not own.But that user is still bound by the information stored in the inode for that file. </li></ul><ul><li>Syntax: </li></ul><ul><li>ln filename [ linkname ] </li></ul><ul><li>Soft ( Symbolic ) links: </li></ul><ul><li>Symbolic link stores the name of the file to which it points. If the referenced file is moved or renamed the symbolic link will point nowhere. </li></ul><ul><li>Syntax: </li></ul><ul><li>ln –s filename [ linkname ] </li></ul>
  41. 41. Mounting Removable device <ul><li>Before you can access the data on newly inserted removable media, the filesystem on the media must first be mounted. </li></ul><ul><li>When using X, inserting a CD into the drive automatically mounts the CD and bring up a nautilus window. </li></ul><ul><li>We can mount the CD and Floppy drive using the following command </li></ul><ul><li>Floppy: </li></ul><ul><li>mount /mnt/floppy </li></ul><ul><li>unmount /mnt/floppy </li></ul><ul><li>CD-ROM: </li></ul><ul><li>mount /mnt/cdrom </li></ul><ul><li>unmount /mnt/cdrom </li></ul>
  42. 42. Location Files <ul><li>Slocate: </li></ul><ul><li>Slocate prints names of files or directories that match a supplied pattern. </li></ul><ul><li>Slocate is more powerful than find. It uses the database instead of searching in real time. </li></ul><ul><li>Syntax: </li></ul><ul><li>slocate [ pattern ] </li></ul><ul><li>Find: </li></ul><ul><li>Find can also be used to search for files by type,owner,modification date and many other criteria </li></ul><ul><li>Syntax: </li></ul><ul><li>Find < dirs> [conditions] [action] </li></ul>
  43. 43. Compression Utilities <ul><li>gzip, gunzip: </li></ul><ul><li>Standard Linux compression utility, Up to 75% compression of text files.When a filename is compressed with utility carry the extension .gz. The file compressed by this utility is uncompressed by gunzip. </li></ul><ul><li>bzip2, bunzip2: </li></ul><ul><li>A newer Linux compression utility. File </li></ul><ul><li>compressed by this utility carry the extension .bz2 and are </li></ul><ul><li>uncompressed with bunzip2. Bzip2 compressed file are smaller in size </li></ul><ul><li>than their gzipped file. </li></ul><ul><li>Other utility: </li></ul><ul><li>Another utility available in linux is zip, pkzip, winzip. This utility is compatible in Dos/Window, and is used to compress more than one file into one file. </li></ul>
  44. 44. Creating/Extracting Archive <ul><li>Archiving places many file into one target file. It is easier to backup, store and transfer. </li></ul><ul><li>tar - Standard linux archiving command. </li></ul><ul><li>Creating an archive: </li></ul><ul><li>tar -cvf archive_name files…. </li></ul><ul><li>c-create a new archive </li></ul><ul><li>v-produce verbose messages </li></ul><ul><li>f-archive_name is name of new file </li></ul><ul><li>Extracting an Archive: </li></ul><ul><li>tar –xvf archive_name.tar </li></ul><ul><li>x- extract a archive file </li></ul>
  45. 45. Compression Archives <ul><li>tar can perform the compression on the fly with either the z or j option </li></ul><ul><li>When creating tar archives, output files are often named with a .tgz extension. </li></ul><ul><li>Compression switches during creating and extraction </li></ul><ul><li>z for gzip compression </li></ul><ul><li>j for bzip2 compression </li></ul>
  46. 46. Bash Shell
  47. 47. Varieties of Shell <ul><li>Bash -Bourne Again Shell(bash) </li></ul><ul><li>Developed for the GNU Project. </li></ul><ul><li>Sh-Bourne Shell </li></ul><ul><li>Originally UNIX shell written by Steven Bourne at AT&T </li></ul><ul><li>csh-C shell </li></ul><ul><li>Written by bill joy at UC Berkeley.C shell was so named because its scripting language has a syntax similar to the C Programming language. </li></ul><ul><li>ksh -Korn Shell </li></ul><ul><li>Written by David Korn of AT&T. </li></ul><ul><li>tcsh- Enhanced C shell </li></ul><ul><li>Enhanced C shell includes many innovative features such as spell-checking commands and filenames </li></ul>
  48. 48. Shell Variables and Environment <ul><li>A variable is a label that has a value, used to configure the shell or other programs </li></ul><ul><li>Variables are resident in memory. Variable are two types </li></ul><ul><li>1. Local variables: </li></ul><ul><li>Local variables are used only by the shell. </li></ul><ul><li>2. Environment variables: </li></ul><ul><li>Environment variables are passed onto another commands </li></ul><ul><li>Display variables and values using </li></ul><ul><li>set- used to display all variables </li></ul><ul><li>env- used to display environment variables </li></ul>
  49. 49. Environment Variables <ul><li>Shell variables exists only in current shell instance. </li></ul><ul><li>Environment variables passed to subshells environment </li></ul><ul><li>Commonly used Environment variables. </li></ul><ul><li> HOME - Path to user’s home directory. </li></ul><ul><li> PATH - Executable search path; A list of directories to look for programs in </li></ul><ul><li> LANG - Identification of default language programs should use </li></ul><ul><li> PWD - User’s current working directory. </li></ul><ul><li> USER - Username of user </li></ul><ul><li> TERM - Login terminal type </li></ul><ul><li> SHELL - Path to login shell </li></ul><ul><li> DISPLAY- X display name </li></ul><ul><li> VISUAL - Name of visual editor </li></ul><ul><li> EDITOR - Name of default editor </li></ul>
  50. 50. Bash Commands <ul><li>which - shows path of executable run when a command is typed </li></ul><ul><li>History - It is used to see list of “remembered” commands. </li></ul><ul><li>~ - It is refer to the home directory </li></ul><ul><li>Aliases : aliases let you to create shortcuts to commands </li></ul><ul><li>alias dir=‘ls –laf’ </li></ul><ul><li>Command Line Expansion: </li></ul><ul><li>$ - Substitute the value of a variable in a command line </li></ul><ul><li>{ } – A string is created for every pattern inside the braces regardless if any file exists </li></ul><ul><li>$( ) – Substitute output from a command in a command line </li></ul><ul><li>$ [ ] - Substitute result of arithmetic expression in a command line. </li></ul><ul><li>* - Matches any string of characters </li></ul><ul><li>? - Matches any one character </li></ul><ul><li>[ ] – Matches any characters inside brackets </li></ul><ul><li> - Used as a last character on line to continue command on next line. </li></ul>
  51. 51. Command Editing Tricks <ul><li>[ TAB ] to complete command lines </li></ul><ul><li>[ ctrl –a ] -> moves to beginning of line. </li></ul><ul><li>[ ctrl –e ] -> moves cursor to end of line. </li></ul><ul><li>[ Esc ] – f -> moves cursor to beginning of next word. </li></ul><ul><li>[ Esc ] – b -> moves cursor to beginning of previous or current word. </li></ul><ul><li>[ ctrl –k ] -> deletes to end of line. </li></ul><ul><li>[ ctrl –u ] -> deletes entire line. </li></ul><ul><li>Set –o vi -> to change keybinding to vi-style </li></ul>
  52. 52. Startup Scripts <ul><li>Scripts of commands executed at login </li></ul><ul><li>Order of Execution </li></ul><ul><li>1. Login shell: Login shell are first shell started </li></ul><ul><li>/etc/profile </li></ul><ul><li>/etc/profile.d </li></ul><ul><li>~/.bashrc </li></ul><ul><li>/etc/bashrc </li></ul><ul><li>Non-login shells : Non-login shell reference some of the same files, but note the difference in order. </li></ul><ul><li>~/.bashrc </li></ul><ul><li>/etc/bashrc </li></ul><ul><li>/etc/profile.d </li></ul>
  53. 53. Standard I/O and Pipes
  54. 54. Standard Input and Output <ul><li>One of most important features of Linux is the streaming nature of data as standard input, output and standard error. </li></ul><ul><li>In general, this allows the input from a program to come from any source, and the output to go to any source. </li></ul><ul><li>Linux provides three I/O channels to processes. </li></ul><ul><li>1.Standard input- Keyboard is default </li></ul><ul><li>2.Standard output- terminal window is default </li></ul><ul><li>3.Standard error- terminal window is default. </li></ul>
  55. 55. Redirecting Input and Output <ul><li>Standard output of commands, which ordinarily displays on the terminal, can be redirected into a file or piped into another command. </li></ul><ul><li>Standard error, which also ordinarily displays on the terminal, can be redirected into a file. </li></ul><ul><li>Standard input, ordinarily coming from the keyboard, can be redirected from a file. </li></ul><ul><li>Commonly Redirection Operators </li></ul><ul><li>> command > file Directs standard output of command to file </li></ul><ul><li>>>command >> file Appends standard output of command to file. </li></ul><ul><li>< command < file Command receives its input from file. </li></ul><ul><li>2> command 2> file Error messages from command are directed to file. </li></ul><ul><li>2>> command 2>> file Errors messages from command are appended to file </li></ul><ul><li>| command 1 | command2 -Pipes the standard output of command1 into the standard input of command2 </li></ul>
  56. 56. Introduction to String Processing
  57. 57. String Processing Commands <ul><li>Wc ( word count ): It is used to count lines and characters </li></ul><ul><li>Sort: Sort is used to sort data. This data can be in a file or the output of another command. </li></ul><ul><li>Syntax: </li></ul><ul><li>Sort [ options ] file( s ) </li></ul><ul><li>Uniq: Uniq removes duplicate adjacent lines from a file. </li></ul><ul><li>Cut: Cut is used to cut fields or column of text from a file and display it to standard output </li></ul><ul><li>Paste: Paste files together </li></ul><ul><li>tr -character translator. </li></ul>
  58. 58. Formatting Tools <ul><li>expand expand tabs into space </li></ul><ul><li>fmt reformat text into paragraphs </li></ul><ul><li>pr reformat text for printing </li></ul><ul><li>diff diff is used to compare the contents of two files for </li></ul><ul><li> difference. </li></ul><ul><li>aspell Aspell is an interactive spell-checker </li></ul>
  59. 59. String Processing with regular expression <ul><li>Regular expression are pattern matching engine </li></ul><ul><li>Regular expression can be divided into 3 broad categories: </li></ul><ul><li>1. Wildcard character stand for another single character </li></ul><ul><li>2. Anchor match the beginning or end of a line or word. </li></ul><ul><li>3. Modifiers determine the number of the previous character. </li></ul><ul><li>Tools that use regular expression are </li></ul><ul><li>1. grep </li></ul><ul><li>2. Sed </li></ul><ul><li>3. less </li></ul><ul><li>4. awk </li></ul>
  60. 60. Wildcard Characters <ul><li>Wildcard characters in regular expression represent some other character. </li></ul><ul><li>The wild character sequence are: </li></ul><ul><li>. Represents any single character. </li></ul><ul><li>[ abc ] any single character in the set. </li></ul><ul><li>[ a-c ] any single character in the range. </li></ul><ul><li>[ ^abc ] any single character not in the set. </li></ul><ul><li>[ ^a-c ] any single character not in the range. </li></ul>
  61. 61. Modifiers <ul><li>Modifiers determine the number of the previous character. </li></ul><ul><li>Modifiers sequence are </li></ul><ul><li>* zero or more of the previous character </li></ul><ul><li>+ one or more of the previous character </li></ul><ul><li>? Zero or one of the previous character </li></ul><ul><li> { i } exactly i of the previous character </li></ul><ul><li> { i, } i or more of the previous character </li></ul><ul><li> { I, j } i to j of the previous character </li></ul>
  62. 62. Anchors <ul><li>Anchors match the beginning or end of a line or word. </li></ul><ul><li>Anchors sequence are </li></ul><ul><li>^ line begins with </li></ul><ul><li>$ line ends with </li></ul><ul><li>< word begins with </li></ul><ul><li>> word ends with </li></ul>
  63. 63. Grep <ul><li>General regular expression processor, displays the lines in a file that match a pattern. </li></ul><ul><li>It can also process standard input. </li></ul><ul><li>Also used as filter in pipelines </li></ul><ul><li>ls | grep.c </li></ul><ul><li>Command options used with the grep command includes </li></ul><ul><li>-v returns lines that do not contain the pattern </li></ul><ul><li>-n precede returned lines with line number </li></ul><ul><li>-c only return a count of lines with the matching pattern </li></ul><ul><li>-l only return the names of files that have at least one line containing the pattern </li></ul><ul><li>-r perform a recursive search of files, starting with the named directory. </li></ul>
  64. 64. sed <ul><li>sed- stream Editor </li></ul><ul><li>Reads a file or stream of data; writes out the data, performing search and replace instructions. </li></ul><ul><li>Uses regular expression in search string. </li></ul><ul><li>Quote search and replace instructions </li></ul><ul><li>Sed ‘ s/dog/cat/g ‘ pets </li></ul><ul><li>Multiple sed instructions </li></ul><ul><li>Sed –e ‘s/ dog / cat / ’ –e ‘ s / hi / lo / ’ pets </li></ul>
  65. 65. awk <ul><li>Programming language for editing text </li></ul><ul><li>Searches a file for lines matching a pattern or pattern </li></ul><ul><li>Performs specified actions on matching lines </li></ul><ul><li>Search patterns are extended regular expression </li></ul><ul><li>Awk programs are data-driven </li></ul><ul><li>Awk rules contain a pattern and an action in curly braces. </li></ul><ul><li>Pattern { action } </li></ul><ul><li>Action is taken on any line matching the pattern </li></ul>
  66. 66. Process Control
  67. 67. How Process are created <ul><li>A process is an executing program with several components and properties. </li></ul><ul><li>Process has many components and properties like exec thread, PID, priority, memory context, environment, file descriptors, security credentials. </li></ul><ul><li>One process “forks” a child, pointing to the same pages of memory, and making the area as read only. </li></ul><ul><li>Then the child “ execs “ the new command, causing a copy-on-write fault, thus copying to a new area of memory. </li></ul><ul><li>A process can exec, without forking. </li></ul><ul><li>The child maintains the process ID of the parent </li></ul>
  68. 68. Process States <ul><li>Init is the first process started at boot time- always has PID 1 </li></ul><ul><li>Except init, every process has a parent. </li></ul><ul><li>Processes can be both a parent and a child at the same time. </li></ul><ul><li>Process can be in one of many states </li></ul><ul><li>R - Process is on the run queue </li></ul><ul><li>S - Process is not executing and is typically waiting for an event to occur to wake it up. </li></ul><ul><li>T - Process is not executing </li></ul><ul><li>D - Process is sleeping and cannot be woken up until an event occurs. </li></ul><ul><li>Z - Defunct Process (zomble). Just before a process dies, it notifies its parent and waits for an acknowledgment. If it never receives it, its PID is not freed but all other resources are freed. </li></ul>
  69. 69. Sending Signal to Processes <ul><li>Signals can be specified by their name, such as KILL, or by their number such as 9. </li></ul><ul><li>Kill can send many signals, but processes only respond to the signals they have been programmed to recognize. </li></ul><ul><li>Kill [ -signal ] pid ( s ) </li></ul><ul><li>Kill [ -signal ] %job ID </li></ul><ul><li>Default signal is TERM </li></ul><ul><li>In addition to kill, there is a killall command that can be used to send a signal to a group of commands such as getty processes. </li></ul><ul><li>Kill –l lists all available signals. </li></ul><ul><li>Signals can be sent to processes interactively using top. </li></ul><ul><li>Ps- displays information about processes on the system. </li></ul><ul><li>ps –a Display all processes, not including processes not controlled by a terminal </li></ul>
  70. 70. Altering Process Scheduling Priority <ul><li>Nice: Nice can be used to run an executable scheduling priority. </li></ul><ul><li>With –n option, the process uses default value of 10. </li></ul><ul><li>Priority value can range from –20 to 19. </li></ul><ul><li>Top: top can be used to display process’s scheduling priority, and to determine which ones are monopolizing system resource </li></ul><ul><li>Renice: renice changes the priority of a running process </li></ul><ul><li># is the priority value. </li></ul><ul><li>-p only alters priority of the listed process and not any child process </li></ul><ul><li>Once a priority value is raised, a non privileged user cannot lower it. </li></ul><ul><li>-g will alter an entire process group’s priority. </li></ul>
  71. 71. Suspending and Running a Process <ul><li>Running a process in Foreground and Background: </li></ul><ul><li>When a process is started from a terminal's command line, it is running in the foreground. The process can be stopped, restarted in the background, or terminated. This is called job control </li></ul><ul><li>A background process is child of the processes that spawned it. The parent process does not wait for the child process to terminate before executing. </li></ul><ul><li>When a process is started in background, a new “ sub-shell ” is created. </li></ul><ul><li>Suspending a Process: </li></ul><ul><li>Jobs that have started in the foreground can be suspended. They are </li></ul><ul><li>Resumed in the background (bg ) </li></ul><ul><li>Resumed in the foreground (fg ) </li></ul><ul><li>[ ctrl-z ] sends a signal to the foreground process to suspend </li></ul>
  72. 72. Listing and Resuming Suspended jobs <ul><li>Jobs displays all reports running in the background or that are suspended. </li></ul><ul><li>Jobs [ id ] </li></ul><ul><li>The number in brackets is a job number, used to kill jobs or bring them back to the foreground. </li></ul><ul><li>Jobs are referenced by %. </li></ul><ul><li>When a command is suspended or backgrounded, it can be brought back to the foreground with fg. </li></ul><ul><li>Suspended jobs can be resumed in the background with bg. </li></ul><ul><li>fg [ %job_number ] </li></ul><ul><li>bg [ %job_number ] </li></ul>
  73. 73. Usage of Vi and Vim Editors
  74. 74. Starting vi and vim <ul><li>vi: The “visual editor”, the standard Linux and Unix editor </li></ul><ul><li>vim : the “vi improved” editor, standard Linux editor </li></ul><ul><li>To start vi: </li></ul><ul><li>If the file exists, the file is opened and the contents are displayed. </li></ul><ul><li>If the file doesn’t exist, vi creates it when the edits are saved for the first time. </li></ul><ul><li>vi filename </li></ul><ul><li>When invoking vi in this way, you actually invoke vim, as vi is an alias to vim. </li></ul>
  75. 75. Modes of vi and vim <ul><li>The vi editor operator in three modes in three different operation. </li></ul><ul><li>The three modes are </li></ul><ul><li>1.Command mode </li></ul><ul><li>2.Insert mode </li></ul><ul><li>3.Ex mode </li></ul><ul><li>When you first enter vi, you will be in command mode . This is considered as “home” mode, the mode to which you will return when you are otherwise not performing some specific action. </li></ul><ul><li>In insert mode , your keystrokes are actually data entered into your document, rather than commands. </li></ul><ul><li>In ex mode , you can enter extended commands. Among the extended commands are saving, exiting, and search-and-replace commands. </li></ul>
  76. 76. Cursor Movement <ul><li>Cursor movement command: </li></ul><ul><li>H left </li></ul><ul><li>J down </li></ul><ul><li>K up </li></ul><ul><li>L right </li></ul><ul><li>W word ahead </li></ul><ul><li>B word back </li></ul><ul><li>( sentence back </li></ul><ul><li>) sentence forward </li></ul><ul><li>{ paragraph above </li></ul><ul><li>} paragraph below </li></ul><ul><li>Arrow keys also work. </li></ul>
  77. 77. Insert Mode <ul><li>We can enter into insert mode by entering keystroke i. </li></ul><ul><li>a append after the cursor </li></ul><ul><li>i insert before the cursor </li></ul><ul><li>o open a line below </li></ul><ul><li>A append to end of line </li></ul><ul><li>I insert at beginning of line </li></ul><ul><li>O open a line above </li></ul><ul><li>To get from insert mode back to command mode, press the < esc > key. </li></ul>
  78. 78. Change,Delete and Yank <ul><li>change delete Yank </li></ul><ul><li>Line cc dd yy </li></ul><ul><li>Letter cl dl yl </li></ul><ul><li>Word cw dw yw </li></ul><ul><li>Paragraph above c{ d{ y{ </li></ul><ul><li>Paragraph below c} d} y} </li></ul><ul><li>Sentence ahead c( d( y( </li></ul><ul><li>Sentence behind c) d) y) </li></ul><ul><li>Use p of P to put(paste) copied deleted data </li></ul><ul><li>Line oriented data: </li></ul><ul><li>p puts the data below the current line </li></ul><ul><li>P puts the data above the current line </li></ul><ul><li>Character Oriented data: </li></ul><ul><li>p puts the data after the cursor </li></ul><ul><li>P puts the data before the cursor </li></ul>
  79. 79. Searching for text <ul><li>To search for a string of text, use the / or ? </li></ul><ul><li>/text search downward for “text” </li></ul><ul><li>?text search upward for “text” </li></ul><ul><li>n continue search in the same direction </li></ul><ul><li>N continue search in the opposite direction </li></ul><ul><li>Undoing Changes: </li></ul><ul><li>u Undo most recent changes. </li></ul><ul><li>U undo all changes to the current line, since the cursor landed on the line. </li></ul><ul><li>< ctrl-r > redo all last “undone” changes. </li></ul>
  80. 80. Saving and Exiting:ex mode <ul><li>The most important functions of ex mode is save and exit commands. Ex mode is entered from command mode using colon character. </li></ul><ul><li>save changes abandon changes </li></ul><ul><li>Exit :wq :q! </li></ul><ul><li>Do not exit :w :e! </li></ul><ul><li>Forcing changes </li></ul><ul><li>saving changes </li></ul><ul><li>Exit :wq! </li></ul><ul><li>Do not exit :w! </li></ul><ul><li>Other commands: </li></ul><ul><li>dtc delete from cursor to the letter c </li></ul><ul><li>x delete a character </li></ul><ul><li>rc replace a character with c </li></ul>
  81. 81. Search and Replace <ul><li>Vi and vim perform search and replace, much like the sed command. </li></ul><ul><li>Different default addressing rule </li></ul><ul><li>No address current line only </li></ul><ul><li>1,$ for changes to entire file </li></ul><ul><li>. , . +10 from current line( “. “ ) to current line plus 10 lines ( “. 10 “) </li></ul><ul><li>8,12 changes lines 8 through 12. </li></ul><ul><li>Regular Expression in vi and vim: </li></ul><ul><li>Regular expressions operate in less-like search operations </li></ul><ul><li>:1, $s / [ Cc ] at > / & and dog /g </li></ul><ul><li>This will replace all instances of the word “Cat” or “cat” with the expression. </li></ul>
  82. 82. File & Screen Repositioning <ul><li>File Repositioning: </li></ul><ul><li>G go to last line in a file </li></ul><ul><li>1G go to first line in file </li></ul><ul><li>< ctrl-f > go forward one full screen </li></ul><ul><li>< ctrl-b > go back one full screen </li></ul><ul><li>< ctrl-d > go down half a screen </li></ul><ul><li>< ctrl-u > go up half a screen </li></ul><ul><li>Screen Repositioning: </li></ul><ul><li>H go to first line on screen(high) </li></ul><ul><li>M go to middle line on screen(middle) </li></ul><ul><li>L go to last line on screen(low) </li></ul><ul><li>z <enter> make current line first line on screen </li></ul><ul><li>z- make current line last line on screen </li></ul>
  83. 83. bash Shell Scripting
  84. 84. Basics of Scripting <ul><li>A shell script is simply a text file containing commands. </li></ul><ul><li>UNIX users involved with system administration and troubleshooting often work with shell scripts. </li></ul><ul><li>/etc/profile.d is the system-wide user login scripts which runs whenever user logins into the scripts. </li></ul><ul><li>Shell Scripting are useful for: </li></ul><ul><li>Automating command entry </li></ul><ul><li>Performing system administration and troubleshooting </li></ul><ul><li>Creating simple applications </li></ul>
  85. 85. Handling Shell Scripts <ul><li>Shell scripts generally contain command per line, but you can have multiple commands on a line if you separate them with semicolons. </li></ul><ul><li>Line continuation character for bash is . </li></ul><ul><li>Creating shell scripts </li></ul><ul><li>Use a text editor such as vi or pico to create a text file containing commands </li></ul><ul><li>Make sure first line contains magic #!, contains name of shell(interpreter) to be used to parse script. </li></ul><ul><li>Making Scripts Executable </li></ul><ul><li>After creating scripts, we can save it using text editor.You will need to change its file permissions in order to make it executable. </li></ul><ul><li>chmod a+x filename.sh </li></ul>
  86. 86. Handling Input/Output <ul><li>Read: </li></ul><ul><li>It is used to assign an input value to an shell variable. </li></ul><ul><li>Syntax: </li></ul><ul><li>read word/character </li></ul><ul><li>Echo: </li></ul><ul><li>Echo is used to generate simple output. </li></ul><ul><li>Syntax: </li></ul><ul><li>echo ‘ Welcome to world of Linux” </li></ul><ul><li>Printf </li></ul><ul><li>printf is used to produce formatted output. </li></ul><ul><li>Syntax is similar to C printf () function. </li></ul><ul><li>Exit: </li></ul><ul><li>0 for success, 1 to 255 for failure. </li></ul><ul><li>shell script set an exit status with the exit command. </li></ul>
  87. 87. Conditional Execution <ul><li>We can execute command conditionally, depending on the exit status of another command. </li></ul><ul><li>&& Logical AND </li></ul><ul><li>|| Logical OR </li></ul><ul><li>&& When executing two commands separated by &&, the latter command will only run if the former command exists successfully. </li></ul><ul><li>|| When executing two commands separated by || the second command runs successfully if the first command failed. </li></ul>
  88. 88. Conditional Execution using if statement <ul><li>Conditional logic statements evaluate test expressions and perform different actions depending upon the results of the test. </li></ul><ul><li>Basic format of if statement </li></ul><ul><li>Syntax: </li></ul><ul><li>if condition </li></ul><ul><li>then </li></ul><ul><li>command ( s ) </li></ul><ul><li>else </li></ul><ul><li>Command ( s ) </li></ul><ul><li>fi </li></ul><ul><li>If the test expression is true then the commands after then are executed, otherwise the commands after else are executed. </li></ul>
  89. 89. Conditional Execution Using the case Statement <ul><li>The case statement provides an alternative method for performing conditional execution </li></ul><ul><li>Syntax: </li></ul><ul><li>Case expression in </li></ul><ul><li>Pattern 1 ) </li></ul><ul><li>Statements ; ; </li></ul><ul><li>Pattern 2 ) </li></ul><ul><li>Statements ; ; </li></ul><ul><li>#default case action </li></ul><ul><li>esac </li></ul><ul><li>If the expression matches any one of the pattern then the statements following the pattern are executed </li></ul>
  90. 90. For-Loop <ul><li>The for loop structure provides an method for executing commands repeatedly for a list of values. </li></ul><ul><li>Syntax: </li></ul><ul><li>for variables in list of values </li></ul><ul><li>do </li></ul><ul><li>commands … </li></ul><ul><li>done </li></ul><ul><li>The values are normally strings. Looping continues until the list-of-values is exhausted. </li></ul>
  91. 91. While-loop <ul><li>While loop structure provides a useful method for performing a set of commands while a condition remains true. </li></ul><ul><li>Syntax: </li></ul><ul><li>while conditions </li></ul><ul><li>do </li></ul><ul><li>commands… </li></ul><ul><li>done </li></ul>
  92. 92. continue and break <ul><li>Continue </li></ul><ul><li>Continue stops the current execution of the loop and reexamines the initial condition possibly restarting the loop. </li></ul><ul><li>Break </li></ul><ul><li>break stops processing the loop entirely, jumping past the done statement. </li></ul><ul><li>Exit: </li></ul><ul><li>Exit exits form the shell script entirely </li></ul><ul><li>Positional Parameter </li></ul><ul><li>Positional Parameter are special variables that hold the command-line argument to the script. </li></ul><ul><li>$* hold all command-line arguments </li></ul>
  93. 93. Basic Networking
  94. 94. Networking commands <ul><li>Ping: Ping command is used to test network connectivity and reliability. </li></ul><ul><li>traceroute: Shows network path between local and remote systems. </li></ul><ul><li>netstat: Lists network statistics and parameters. </li></ul><ul><li>host: Host is useful for resolving hostnames to IP address. </li></ul><ul><li>Dig: Dig will resolve hostname </li></ul><ul><li>Fwhois : Fetches domain registration codes from InterNIC and domain registrars </li></ul><ul><li>Lftp: lftp is a useful command-line ftp client with such features as bookmarks and tab completion. </li></ul><ul><li>Wget: It can be used to retrieve a single file via HTTP or FTP. </li></ul><ul><li>telnet: It is useful for checking and troubleshooting services. </li></ul><ul><li>ssh: It allows remote login and remote command execution via a secure encrypted connection. </li></ul><ul><li>rsync: It is a protocol allows to transfer files using efficient checksum algorithm </li></ul>
  95. 95. System Tools
  96. 96. Terminal Configuration <ul><li>The terminal is the window which you interact with the command line. </li></ul><ul><li>TERM: </li></ul><ul><li>This variable tells the system what terminal type is being used. </li></ul><ul><li>Stty: </li></ul><ul><li>This utility sets or displays terminal settings. </li></ul><ul><li>Reset: </li></ul><ul><li>Sometimes the output of a program or command will cause the terminal to stop displaying correct characters.This can be fixed by using reset. </li></ul><ul><li>eval ‘resize’: </li></ul><ul><li>A pseudo-terminal window, such as xterm, may be stretched to any size by dragging with the mouse. </li></ul>
  97. 97. System Information <ul><li>One of the duties of the system administrator is to be aware of the settings and activities on a system </li></ul><ul><li>/etc/redhat-releases: </li></ul><ul><li>This file contains the version number of the currently installed operating system. </li></ul><ul><li>Uname: </li></ul><ul><li>This command prints information about the current system. </li></ul><ul><li>ifconfig: </li></ul><ul><li>This command, when enters with no arguments, list the active interfaces </li></ul><ul><li>Who: </li></ul><ul><li>Display information about the list of user logged on and their login time. </li></ul><ul><li>Last: </li></ul><ul><li>List of all logged in users </li></ul><ul><li>Free: </li></ul><ul><li>Display free and used memory. </li></ul>
  98. 98. Scheduling Process <ul><li>at : </li></ul><ul><li> This command is used to schedule a process according to the time parameters </li></ul><ul><li>atq: </li></ul><ul><li>This command list the current at jobs pending </li></ul><ul><li>atrm: </li></ul><ul><li> It is used to remove pending at jobs </li></ul><ul><li>Cron: </li></ul><ul><li> This daemon wakes up every minute or so determines if an entry in user’s cron table need to be executed. A cron job can be scheduled as often as once a minute or as infrequently as once a year </li></ul>
  99. 99. RPM Queries <ul><li>Enables a user to build and query software package </li></ul><ul><li>RPM package filename of the format package-version-build-architecture.rpm </li></ul><ul><li>RPM package installation: </li></ul><ul><li>rpm –ivh package name.rpm </li></ul><ul><li>RPM package Upgrade: </li></ul><ul><li>rpm –Uvh package name.rpm </li></ul><ul><li>RPM package “Freshening”: </li></ul><ul><li>rpm –Fvh packagename.rpm </li></ul><ul><li>RPM Package Removal </li></ul><ul><li>rpm –e packagename </li></ul>
  100. 100. Programming Languages <ul><li>Languages and programming libraries supported by Linux include: </li></ul><ul><li>Perl </li></ul><ul><li>Java </li></ul><ul><li>Gcc </li></ul><ul><li>Tcl </li></ul><ul><li>Tk </li></ul><ul><li>Qt </li></ul><ul><li>Php </li></ul><ul><li>Expect </li></ul>

×