of 41 /41
IBM System Programming Training Module 2 - Linux Basics 1

Ibm system programming training module 2 - linux basics

Embed Size (px)


Linux 1 Hand-Outs

Text of Ibm system programming training module 2 - linux basics

  • 1. IBM System Programming Training Module 2 - Linux Basics Unit 1 Introduction to Linux Effects of the License ModelA Short History of Linux Everybody has access to the source Volunteer software development on the Internet, with central 1984: Richard Stallman starts GNU project coordination GNUs Not UNIX Linus Torvalds coordinates kernel development http://www.gnu.org Others coordinate other pieces of the OS Purpose: Free UNIX Peer reviews possible "Free as in Free Speech, not Free Beer" Security Performance First step: re-implementation of UNIX Utilities C compiler, C library License cannot change emacs So your changes (and name) will stay in forever bash To fund the GNU project, the Free Software Foundation is founded Linux has become a Way of Life http://www.fsf.org Culture 1991: Linus Torvalds writes 1 version of Linux kernel st Initially a research project about the 386 protected mode Celebrities Linus UNIX -> Linux Linus Torvalds Combined with the GNU and Richard Stallman other tools forms a complete Eric Raymond UNIX system Humor 1992: First distributions emerge User-friendly Segfault Linux kernel GNU and other tools Installation procedure Mascot TuxWhats So Special About Linux? Linux Today Most software (including the Linux kernel) is GPLed (GNU General Linux covers the whole spectrum of computing Public License) Embedded devices http://www.gnu.org/copyleft/gpl.html Laptops Desktop systems Is called "copyleft" (instead of "copyright") Development systems You may copy the software Small and large servers You get the source code Megaclusters/supercomputers You may alter the source code and recompile it You may distribute the altered source and binaries You may charge money for all this Linux is used throughout the world and in space You only may not change the license Linux is used by home users and by some of the largest companies So all your customers have the same rights as you in the world So you really cannot make money from selling the software alone IBM Boeing Other Open Source licenses (e.g. BSD) are also used NASA 1
  • 2. Unit 2 - Installing Linux Partitioning ToolsPreparing a System for Installation PartitionMagic Commercial program from PowerQuest Know your hardware Runs under MS-DOS and Windows CPU, memory, keyboard, mouse Can create/resize/move/delete partitions Hard disks, CD-ROM players Graphical adapters, monitor capabilities GNU parted Network adapters, IP addresses Can create/resize/move/delete partitions Printers QTParted is a graphical frontend for parted Is all your hardware supported? fdisk Linux Hardware-HOWTO Virtually every PC OS (Windows, OS/2, Linux) comeswith a tool Distributors Hardware Compatibility List "fdisk" to create partitions for that OS Hardware manufacturer If unsure, just try it! Disk Druid, YaST and others Partitioning programs integrated in install program Make space for Linux partitions Installing LinuxKnow your Hardware Boot system from bootable media Obtain information from All modern PCs can boot from CD-ROM directly Manuals o Must be enabled in BIOS Windows Control Panel Otherwise boot from floppy Some distributions require additional disks All disk images are usually stored on the CD-ROM After booting, install from: Local CD-ROM/DVD Local Hard Disk Network Installation Steps All installation programs need to perform essentially the same steps: 1. Choose language, keyboard type, mouse typePartitioning Theory 2. Create partitions 3. Set up a boot loader 4. Configure network Partitioning is necessary on Intel-based computers 5. Configure users and authentication Maximum of four primary partitions 6. Select package groups One primary partition may be an extended partition 7. Configure X An extended partition can hold an unlimited amount of logical 8. Install Packages partitions (but the OS may pose a limit anyway) 9. Create Boot Disk Order of steps may vary from distribution to distribution Other steps may also be included e.g. firewall, printers, sound 2
  • 3. Common Boot Loaders: LILO: Linux Loader text-based installationSelect Language, Keyboard, Mouse GRUB: GRand Unified Boot loader GUI installation Select the language to be used during installation process Configure Network Different distributions support different languages Most distributions configure your network adapter as part of the Select the keyboard layout installation process Different countries use different keyboard layouts Ethernet Dead (compose) keys allow you to input accented or special Token Ring characters such as , , and so forth. Need the following information: Select your mouse IP address A mouse can be connected using a PS/2, USB or serial Subnet mask connector Network address If your mouse has only 2 buttons, you can emulate the third Broadcast address (middle) button by clicking both buttons simultaneously Hostname Default router/gateway DNS server addressesInstall Class May also be configured to use DHCP Most distributions have default installation "classes" for typical users Workstation Laptop Configure Root and User Accounts Server root is the superuser of the system A "custom" class allows you to make all decisions yourself Can do anything Packages to be installed Needs a strong password Various configuration options Do not use your system as root unless you need to! Most distributions allow you to add user accounts during installationDisk Partitioning too: Linux installation requires you to create Linux partitions Create a user account for every individual user that is going to use At a minimum, create: / (root) 750MB; /swap 64 256MB the system Recommended: /boot 16MB Hint: You should enable shadow passwords and MD5 encryption if May need/want to create other partitions: /usr, /usr/local, /var, /tmp, available for additional security. /opt, and /home Select Package GroupsConfigure a Boot Loader Most distributions have grouped individual packages in package Boot Loader loads and starts the Linux kernel groups Can boot other operating systems as well OS/2, Windows, BeOS, Give each OS a unique label! Only select the package groups you need on your workstation Selecting individual software packages is usually still possible but Can be password protected tedious Prevents users from passing boot parameters to Linux or booting A typical distribution has over 1000 packages... any OS Configure X Should generally be configured in the MBR, unless another boot loader is used 3
  • 4. X (X Window System) is the graphical user interface of Linux Post-install Configuration Needs to be configured for your system Graphical adapter After installation has finished, your system will reboot to activate the Monitor newly installed kernel SUSE will perform the reboot during installation Most adapters and monitors can be auto detected If not auto detected, select manually or use a "Generic" adapter For almost all Linux distributions, this is the only reboot that is ever or monitor required Usually customization allowed: After reboot, some post-installation configuration may happen Resolution Configure graphics Refresh rate Configure sound card Color Depth Install documentation, updates, drivers Create user accounts Test settings if possible Registration If nothing works, skip X configuration Unit 3 - Using the SystemOther (Optional) Installation Screens Linux is Multi-user and Multitasking Some distributions offer additional installation screens: Linux is a multi-user, multitasking operating system Printer configuration Firewall configuration Multiple users can run multiple tasks simultaneously, Sound card configuration independent of each other Modem configuration Time Zone configuration Always need to "log in" before using the system Identify yourself with username and password Usually straightforward Multiple ways to log in to the system Console: Directly attached keyboard, mouse, monitorInstalling Packages Serial terminal Network connection Installing packages may take 5 mins to several hours Most distributions provide a progress bar and/or total time indication Virtual Terminals Some distributions provide some entertainment while installing In most Linux distributions, the console emulates a number of virtual While installation is going on, various virtual terminals provide terminals information on the progress Each virtual terminal can be seen as a separate, directly attached Switch between Virtual Terminals using Ctrl-Alt-F1 to F6 (console console based) and Ctrl-Alt-F7 (GUI) Different users can use different virtual terminals Feed additional CDs when asked for Typical setup: VT 1 through 6: text mode logins VT 7: graphical mode login prompt (if enabled)Create Boot Disk Switch between VTs with Alt-Fn (or Ctrl-Alt-Fn if in X) After installation, most distributions allow you to create a custom boot disk Used to boot the system in case of LILO/GRUB/kernel problems Linux Commands System specific to some degree Everything on a Linux system can be done by typing commands (Even browsing the World Wide Web...) 4
  • 5. $ who mu $ who -m u The Graphical User Interface (X Window System or X) is not needed for running a Linux system Some Basic Linux Commands But is sometimes more convenient In order to be able to type commands in X, you need to start a passwd - Change your password terminal emulator mkpasswd - Generate a random password date, cal - Find out todays date and displays a calendar who, finger - Find out who else is active on the system clear - Clear the screenCommand Prompt echo - Write a message to your own screen write - Write a message to other screens Command prompt indicates that the system is ready to accept wall - Write a message to all screens commands Can be configured yourself (will be covered later) talk - Talk to other users on the system Default depends on distribution mesg - Switch on/off reception of write, wall and talk messages Examples: Changing your Password [[email protected] dir]$ dir$ passwd command allows you to change your password $# $ passwd Changing password for tux1 dollar ($) usually means: "logged in as regular user" Old password: hash (#) usually means: "logged in as root" New password: Retype new password:Linux Command Syntax Passwords are important for security - choose a good password Minimum six characters Linux commands have the following format: Not a dictionary word, birth date, license plate, ... $ command option/s argument/s Examples: mkpasswd command generates a random password $ ls $ ls -l $ ls /dev The date Command $ ls -l /dev date shows the current date and timeCommand Format Examples $ date Fri Jun 6 11:15:10 CET 2003RIGHT WRONG The cal Command1. Separation$ mail -f personal $ mail - f personal cal shows a calendar Syntax: cal [Month] [Year]$ who u $ who-u $ cal 6 2003 June 20032. Order Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7$ mail -s test root $ mail test root -s 8 9 10 11 12 13 14$ who u $ -u who 15 16 17 18 19 20 21 22 23 24 25 26 27 283. Multiple options 29 30$ who -m u $ who -m-u Who is on the System? 5
  • 6. who shows who is logged onto the system $ talk john $ who root tty1 Mar 5 11:10 tux1 tty2 Mar 5 11:04 $ who am i host!tux1 tty2 Mar 5 11:04 $ whoami tux1 The mesg CommandFinding Information about Users mesg command controls whether other users can send messages tofinger command shows info about other users Syntax: finger you with the write, wall or talk command or through output[user][@host] redirection. $ finger Login Name Tty Idle Login Time tux1 Tux (1) 2 Mar 5 11:04 User tux1 at tty1: root root *1 7 Mar 5 11:10 $ mesg n $ finger tux1 User tux2 at tty2: Login: tux1 Name: Tux (1) Directory: /home/tux1 Shell: /bin/bash $ write tux1 tty1 On since Fri Mar 5 11:04 (CET) on tty2 write: tux1 has disabled messages on tty1 No mail. No plan. Keyboard TipsThe clear, echo, write and wall Commands or - Corrects mistakes clear command clears your screen - Terminates the current command and returns to the $ clear shell - End of transmission echo command writes messages to your own screen - Temporarily stops output to the screen - Resumes output $ echo Who wants to go to lunch? Who wants to go to lunch? - Erase last word - Erase the entire line write use to display a text message on a users terminal - Command or filename completion $ write tux2 - Previous command Message # terminate - Next command - One character to the left wall use to place a message on all logged in users displays - One character to the right $ wall - Look at the output of previous commands Im back - Look at the output of later commands; # terminate eventually gets you back to the command prompt - Search for a command in the command historyTalk with Another User If John wants to talk with Fred, John enters: Command History $ talk fred Command history also can be viewed with history command If Fred also wants to talk with John, Fred enters: $ history 5 99 clear 6
  • 7. 999 wc .bash_profile Logging Out 1000 wc .bash_profile 1001 wc .bash_profile 1000 history When finished working on a system, always log out Other people might misuse your accountRetrieving commands with "!" In a text mode terminal, use logout, exit or ctrl-d To retrieve commands, use !u In a graphical mode terminal, use appropriate menus $ wc .bash_profile 16 23 238 .bash_profile Unit 4 - Working with Files and Directories Redo previous: $ !-1 A file is: wc .bash_profile A collection of data 16 23 238 .bash_profile A stream of characters or a "byte stream" or: $ !! No structure is imposed on a file by the operating system Redo command No. 999: $ !999 File TypesConsole Mouse Tips On most distributions, the mouse also works in text mode Left mouse button o Click: Mark start of selection o Drag: Mark selection Middle mouse button o Click: Paste selection Right mouse button Linux Filenames o Click: Mark end of selection Should be descriptive of the content Should use only alphanumeric characters UPPERCASE, lowercase,Locking number, @, _ Should not include embedded blanks When temporary leaving a system alone, always lock your terminal Should not contain shell metacharacters: * ? > < / ; & ! [ ] | " ( ) { }Other people might misuse your account Should not begin with + or - sign Are case sensitive In a text mode terminal, use vlock (lock your terminal) or vlock Filenames starting with a . are hidden -a (lock the whole console) The maximum number of characters for a filename is 255 In a graphical mode terminal, use the menu, the "padlock" icon or Linux Pathnames xlock Most screensavers support automatic locking too Full pathnames: Start from / (the root directory) A locked terminal can only be unlocked with the users password Relative pathnames: Start from the present working directory 7
  • 8. Examples: working directory is /home/tux1 With the rmdir (remove directory) command: $ rmdir /home/tux1/doc/mon_report (full) dirname doc/mon_report (relative) ../tux3/pgms/suba (relative) $ pwd ./test (a file in the /home/tux1 current dir) $ rmdir doc test ~/test (same as above) rmdir: doc: Directory not empty Note: directory must be empty!Example Directory Structure Working with Multiple Directories Create and remove multiple directories simultaneously with the -p flag $ mkdir -p dir1/dir2/dir3 or $ mkdir dir1 dir2 dir3 $ rmdir -p dir1/dir2/dir3 or $ rmdir -r dir1 dir2 dir3Where Am I? List the Contents of Directories pwd command (print working directory) can be used to find out what your current working directory is: With the ls command: $ ls [dir/file] $ ls /home $ pwd tux1 tux2 tux3 /home/tux1 Important options:Change Current Directory -l long listing (more information) With the cd (change directory) command: $ cd dirname -a lists all files (including hidden) -t lists files sorted by change date $ cd doc (relative) $ cd /home/tux1/doc (full) -R lists contents recursively $ cd ~tux1/doc (home) $ cd (Go to your home directory) $ cd .. (Go one directory up) The touch Command $ cd - (Go to previous directory) touch command creates an empty file, or updates the modification time of an existing fileCreate Directories $ ls -l -rw-rw-r-- 1 tux1 penguins 512 Feb 24 11:10 docs With the mkdir (make directory) command: $ mkdir dirname $ touch docs $ ls -l -rw-rw-r-- 1 tux1 penguins 512 Mar 5 15:37 docs $ mkdir /home/tux1/doc (full pathname) $ touch new $ cd /home/tux1 $ ls -l -rw-rw-r-- 1 tux1 penguins 512 Mar 5 15:37 docs $ mkdir doc (relative pathname) -rw-rw-r-- 1 tux1 penguins 0 Mar 5 15:37 newRemoving Directories Copying Files 8
  • 9. cp command copies files: cp [source/s] [target] Of cabbage - and kings - And why the sea is boiling hot - And whether pigs have wings." Copying one file to another: $ cp .bashrc bashrc.old Displaying Files Page by Page Copying multiple files into a target directory: With the more or less commands: $ [less/more] $ cp doc/mon_report doc/walrus /tmp walrus "The time has come", the walrus said, cp can recursively copy directories with the -R flag "To talk of many things: Of shoes - and ships - and sealing wax - $ cp -R /home/tux1/doc /tmp Of cabbage - and kings - And why the sea is boiling hot - And whether pigs have wings."To prevent cp from overwriting existing files, use: /tmp/test/walrus 1-6/6 (END) $ cp -R -i /home/tux1/doc /tmp cp: overwrite `/tmp/doc/walrus? Displaying Binary FilesMoving and Renaming Files With the od command: With the mv command: $ mv [source/s] [target] To move a file do another directory: With the strings command: $ strings /usr/bin/passwd $ mv doc/walrus ../../tmp /lib/ld.so.1 __gmon_start__ To rename a file: __deregister_frame_info __register_frame_info $ mv doc documents ... Use the -i option to prevent mv from overwriting existing files Removing Files Moving and renaming files can be combined by mv: With the rm command: $ rm test/rob $ cd $ pwd $ ls test/rob /home/tux1 ls: rob: No such file or directory mv /tmp/walrus ./test/rob If unsure, use -i option To move a directory: $ rm -i test/rob $ mv ./test /tmp rm: remove `test/rob?Note: mv is recursive by default To remove files and directories recursively: $ rm -ir test/Listing File Contents Splitting Files With the cat (concatenate) command: $ cat file1 file2 ... You can split a file into smaller files with the split command $ $ cat walrus split -b file [prefix] "The time has come", the walrus said, $ ls -l "To talk of many things: -rw-r--r-- 1 root root 4194304 Feb 21 13:31 large Of shoes - and ships - and sealing wax - 9
  • 10. $ split -b 1024k large large.$ ls -l-rw-r--r-- 1 root root 4194304 Feb 21 13:31 large-rw-r--r-- 1 root root 1048576 Feb 21 13:33 large.aa-rw-r--r-- 1 root root 1048576 Feb 21 13:33 large.ab-rw-r--r-- 1 root root 1048576 Feb 21 13:33 large.ac-rw-r--r-- 1 root root 1048576 Feb 21 13:33 large.adFile Managers Linux also offers different graphical file managers Nautilus (GNOME) Konqueror (KDE) Required Permissions Unit 5 - File and Directory PermissionsPermissions Changing Permissions File permissions are assigned to: To change the permission of a file use the chmod command owner of a file members of the group the file is assigned to Syntax: chmod [MODE] [FILE/S] all other users Mode can be symbolic Permissions can only be changed by the owner and root $ chmod go-rx /home/tux1 $ ls -ld /home/tux1 drwx------ 4 tux1 users 512 Jan 5 12:43 /home/tux1Viewing Permissions Mode can be octal:To show the permissions of a file, use the ls command with the -loption $ chmod 700 /home/tux1 $ ls -ld /home/tux1 drwx------ 4 tux1 users 512 Jan 5 12:43 /home/tux1 Changing Permissions Calculating numeric (octal) mode:Permissions Notation 10
  • 11. umask The -k option of the man command or the apropos command prints out a description of all entries which match the given keyword New files should not be created with 666, to avoid this problem a permission mask exists. Syntax: $ umask 022 man Sections Manual pages are divided in 9 sections: 1. User commands 2. System calls 3. Libc calls 4. Devices 5. File formats and protocols Unit 6 - Linux Documentation 6. Games 7. Conventions, macro packages and so forth 8. System administrationThe man Command 9. Kernel Certain subjects appear in multiple sections With the man command you can read the manual page of commands. To select correct section, add section number: Manual pages are stored in /usr/share/man man 1 passwd (about the passwd command) The manual page consists of: man 5 passwd (about the passwd file) Name - The name of the command and a one-line description Synopsis - The syntax of the command The info Command Description - Explanation of how the command works and what it does Files - The files used by the command Sometimes a replacement for manual pages Widely used by the GNU project Bugs - Known bugs and errors See also - Other commands related to this one Information for info is stored in /usr/share/infoExample Manual Pages Some info commands: space next screen of text del or bs previous screen of text n next node p previous node q quit info The --help Option 11
  • 12. Another way of getting help about a command All Linux directories are contained in one, virtual, "unified file Help is built in the command itself (if supported) system" Physical devices are mounted on mount pointsHOWTO Documents Floppy disks Hard disk partitions Documents which describe in detail a certain aspect of configuring or CD-ROM drives using Linux. No drive letters like A:, C:, ... Detailed information about how to perform a given task Install PCMCIA support Kernel compilation Dual boot with other operating systems HOWTO documents are text files in /usr/share/doc/HOWTO Need to be installed manually /bin - contains executables for every user On the Internet: http://www.tldp.org/index.htmlOther Documentation /sbin - contains system administration executables Certain programs also offer other kinds of documentation /lib - contains libraries HTML PostScript Should always be available Plain Text At system boot In single user mode Usually stored in /usr/share/doc/ When booting from rescue disk /boot - contains kernel image and some other goodies /dev - Contains special files that represent hardware devicesOn the Internet Block special device, for example, a hard disk Character special device, for example, mouse and keyboard All Linux documentation is available on the Internet. Google: http://www.google.com/linux Each device has a major and minor number Other sites: Identification within the kernel http://www.tldp.org http://www.linux.org /etc - contains system-wide configuration files http://www.redhat.com http://www.suse.com http://www.xfree86.org Some subsystems have multiple files and therefore use a separate http://www.kernel.org directory http://lwn.net and many more /etc/X11 contains X Window System configuration /etc/skel contains default user configuration files Usenet news: comp.os.linux.* /etc/sysconfig contains system configuration Country-specific groups /home - home directories of users Unit 7 - Directory Structure /mnt - mount points for other file systems Note: SUSE uses /media instead of /mnt for floppy and cdrom mount points 12
  • 13. /proc - virtual file system that represents kernel and process information /root - home directory of the root user /tmp - temporary storage space for programs, users and usually automatic cleanup mechanism active /usr (UNIX System Resources) Contains all programs, libraries and so on which are not essential for system boot and emergency operations Typical File System Layout /usr/local intended for programs not in the distribution o locally developed o locally compiled /var Files of variable size o logfiles o lockfiles Directories with variable content o mail o scheduling o printing Temporary storage space, longer than /tmp The mount Command /lost+found Exists in every file system mount command mounts a file system Place where lost+found files are stored after a crash Makes it part of the unified file system structure recovery by fsck mount [-t type] [-o opts] device mountpntOther Directories in / # mount /dev/hda5 /usr /opt - used for some software from external providers Separate file system advisable Whatever you create yourself.Virtual and Unified File System The umount Command Linux does not use drive letters (A:, C:, D:) to identify drives/partitions, but creates a virtual, unified file system umount command unmounts a file system Different drivers/partitions are "mounted" on a "mount point" Takes it out of the unified file system structure File system should not be busy 13
  • 14. $ file /etc/passwd Syntax: umount {device|mountpnt} /etc/passwd: ASCII text $ file /usr/bin/passwd # umount /dev/hda5 or # umount /usr /usr/bin/passwd: ELF 32-bit LSB executableThe /etc/fstab File To edit text files, use an editor Non-text files can only be changed using the application that created them, or with a "hex editor" /etc/fstab lists all known filesystems on the systemSyntax: device mountpoint type options dump fsck But most configuration files under Linux are text files File systems with the noauto option are not mounted automatically The "vi" Text Editor but can be used as templates for mount Default editor in all UNIX operating systems Usually the only editor available in emergencies Relatively hard to learn, but really powerful As a Linux user, you should be able to use vi for basic editing tasks But its OK if you prefer another editor for daily work vi in Linux is usually vim (vi improved): Syntax highlighting Arrow keys, Del, BS work in insert modeNote: Some distributions use file system labels instead of device names. Multi level undo Mouse supportMounting and Unmounting Removable Media vi Modes Most distributions configure /etc/fstab so that the console user is allowed to mount removable media (floppy, cd) on a predetermined vi knows three modes of operation mountpoint and with predetermined options (for security) Command mode (for simple, one-letter commands) Edit mode (insert text) Always unmount media before ejecting ex mode (for complicated commands) GUI typically offers icons that perform the mount Can easily change between modes $ whoami tux1 $ mount /mnt/cdrom $ mount. /dev/cdrom on /mnt/cdrom type iso9660 (ro,nosuid,nodev,user=tux1) . $ ls /mnt/cdrom Cursor Movement in Command Mode . $ umount /mnt/cdrom Unit 8 - Editing FilesEditing Files Use file command to determine the content of a file 14
  • 15. Editing Text in Command Mode Searching for Patterns x - To delete a single character under cursor x X - To delete a single character left of cursor X /pattern - To search for a pattern (in command mode) r -To replace a single character r n - To repeat the previous search u - Undo the last change u . - To repeat last command J - To join two lines togetherSwitching to Edit Mode I - To insert text at begin of line i - To insert text before cursor a - To append text after cursor A - To append text at end of line Replacing Patterns - To go back to command mode Advanced search and replace can be done in ex mode :1,$s /old/new/g - To replace old with newAdding Text in Edit Mode Keystroke "i" switches vi to edit mode. New characters can be inserted at the current position of the cursor Cut, Copy and Paste dd - To cut a whole line into buffer yy - To copy a whole line into buffer dw - To cut a word from the current cursor position to its end p -To paste contents of buffers hereExiting the Edit Mode 3dd, 8yy - To cut, copy multiple lines, precede command by number Keystroke "ESC" leaves the edit mode. Cut and Paste 15
  • 16. Text mode editors pico (really simple) Original vi emacs (even more powerful and complicated than vi) Graphical mode editors kedit, kwrite geditvi Options Hex editors allow you to change non-text files if you know the internal structure Options entered in ex mode change the behavior of the vi editor: Khexedit :set all :set autoindent/noautoindent :set number/nonumber Unit 9 - Shell Basics :set list/nolist :set showmode/noshowmode The Shell :set tabstop=x :set ignorecase/noignorecase :set wrapmargin=x Shell is the user interface to Linux :set tx/notx :set hlsearch/nohlsearch :syntax on/off To make these options available to all vi sessions, put it into a .exrc or .vimrc file in your HOME-directoryExiting vi ZZ - To save and exit in command mode :w - To save in ex mode :w! - To forcefully save file in ex mode :q - To quit without saving in ex mode :q! - To forcefully exit in ex mode :wq - To save and exit in ex mode (recommended) Shell Features :x - To save and exit in ex mode, shorter When the user types a command, various things are done by the shell before the command is actually executed:vi Cheat Sheet Wildcard expansion *?[] Input/Output redirection < > >> 2> Command grouping { com1 ; com2; } Line continuation Shell variable expansion $VAR Alias expansion dir -> ls -l Shell scripting #!/bin/bashOther Editors For example, the ls *.doc command could be expanded to /bin/ls -- color=tty mydoc.doc user.doc before execution (depending on A typical Linux distribution comes with a large number of editors. settings and files present) Metacharacters and Reserved Words Examples: 16
  • 17. Metacharacters are characters that the shell interprets ashaving a Other descriptors are assigned by the program when it opens files special meaning. Reserved words are words that the shell interprets as special commands. Input Redirection Default Standard Input: $ cat AmsterdamBasic Wildcard Expansion Amsterdam Utrecht When the shell encounters a word which contains a wildcard, it tries Utrecht to expand this to all matching filenames in the given directory STDIN redirected from file: $ cat < cities Amsterdam Utrecht $ Output Redirection Default Standard Output: /dev/tty $ ls file1 file2 file3 Redirect output to a file:Advanced Wildcard Expansion $ ls > ls.out wildcards [, ], - and ! match inclusive lists: Redirect and append output to a file: $ ls >> ls.out Create a file with redirection: $ cat > new_file Save this line Error Redirection Default Standard Error: /dev/tty $ cat filea cat: filea: No such file or directoryFile Descriptors Redirect error output to a file: Every program has a number of file descriptors associated with it $ cat filea 2> error.file Three descriptors are assigned by the shell when the program starts $ cat error.file cat: filea: No such file or directory (STDIN, STDOUT and STDERR) 17
  • 18. Redirect and append errors to a file: sed - Allows string substitutions $ cat filea 2>> error.file awk - Pattern scanning and processing fmt - Insert line wraps so text looks pretty Discard error output: tac - Display lines in reverse order $ cat filea 2> /dev/null tr - Substitute characters grep - Only displays lines that match a patternCombined Redirection nl - Number lines pr - Format for printer sort - Sort the lines in the file Split Output tee command reads standard input and sends the data to both standard out and a file. $ ls | wc -l 3 $ ls | tee ls.save | wc -l 3 $ cat ls.save file1 file2 file3Pipes Command Substitution A sequence of two or more commands separated by a vertical bar (|) is called a pipe or pipeline Command Substitution allows you to use the output of a $ ls -l | wc -l command as arguments for another command. Use backticks (`) or $() notation: The standard output of command1 becomes the standard input of command2 $ rm -i `ls *.doc | grep tmp` $ echo There are $(ps ax | wc -l) processes running. Command Grouping Multiple commands can be entered on the same line, separated by a semicolon (;)Filters Commands can be grouped into one input/output stream by putting curly braces around them: Filter is a command that reads from standard in, transforms the input in some way and writes to standard Commands can be executed in a sub shell by putting out. round braces around them: They can, therefore, be used at intermediate points in a $ date ; pwd pipeline. $ ( echo Print date: ; date ; cat cities ) | lpr $ { echo Print date: ; date ; cat cities; } | lpr $ ls | grep .doc | wc -l 4 Shell VariablesCommon Filters Variables are part of the environment of a process expand, unexpand - Change tabs to spaces and vice A variable has a unique name versa The first character must not be a digit. 18
  • 19. To assign a value to a variable use: variable=value A command returns a value to the parent process. By convention, zero means success and a non-zero value $ VAR1="Hello class" $ VAR2=2 means an error occurred. A pipeline returns a single value to its parentReferencing Shell Variables The environment variable ? contains the return code of the previous command. To reference the value of a variable, use: $variable $ whoami $ echo $VAR1 tux1 Hello class $ echo $VAR2 $ echo $? 2 0 $ cat fileaExporting Shell Variables cat: filea: No such file or directory $ echo $? The export command is used to pass variables from a 1 parent to a child process. Quoting Metacharacters Changes made to variables in a child process do not affect the variables in its parent. When you want a metacharacter NOT to be interpreted $ export x=4 by the shell, you need to quote it $ bash Quoting a single character is done with the backslash () $ echo $x 4 Quoting a string is done with single () or double (") quotes $ x=100 $ echo $x Double quotes allow interpretation of $, `(backtick) and 100 $ echo The amount is US$ 5 $ exit The amount is US$ 5 $ echo $x $ amount=5 4 $ echo The amount is $amountStandard Shell Variables The amount is $amount $ echo "The amount is $amount" The shell uses several shell variables internally The amount is 5 These variables are always written in uppercase Quoting Non-Metacharacters Example: The backslash can also be used to give a special $ - PID of current shell meaning to a non-metacharacter (typically used in PATH - Path which is searched for executables regular expressions) PS1 - Primary shell prompt PS2 - Secondary shell prompt n = newline t = tab PWD - Current working directory b = bell HOME - Home directory of user LANG - Language of user A backslash followed directly by Enter is used for line continuation Overwriting these variables by accident can cause unexpected results The continued line is identified with the $PS2 prompt (default: >) Always use lowercase variables in your own shell scripts to avoid conflicts $ cat/home/john/mydir/mysudir/data/information/letter > /pictures/logo.jpgReturn Codes from Commands Aliases 19
  • 20. The alias command allows you to set up aliases for often-used commands Examples: $ alias ll=ls -l $ alias rm=rm -i To show all currently defined aliases: $ alias To delete an alias: Parents and Children $ unalias ll $ echo $$ $ ll 561 bash: ll: command not found $ bash $ echo $$ 675 Unit 10 - Working with Processes $ dateWhat is a Process? Thu Mar 25 22:28:21 CET 1999 $ A program is an executable file $ echo $$ A process is a program which is being executed 561 Each process has its own environment: Monitoring Processes The ps command displays process status information To see the PID of your current shell process type: $ echo $$ ps supports a large number of options - you typically use ps aux:Starting and Stopping a Process a - all processes attached to a terminal All processes are started by other processes x - all other processes Parent/Child relationship u - provides more columns $ ls l options are typed without a leading "-" ! Viewing Process Hierarchy pstree shows process hierarchy A process can be terminated because of two reasons: The process terminates itself when done The process is terminated by a signal from another processLogin Process Environment 20
  • 21. Controlling Processes Kill Signals Processes can be controlled in two ways: Several signals can be sent to a process From the shell that started it, using its job number From anywhere else on the system, using its PID Using keyboard interrupts (if foreground process) Using the kill commandActions that can be performed on a running process: Synopsis: kill -signal PID Terminate Kill Using the killall command to kill all named Stop/Continue apps Synopsis: killall -signal application These actions are performed by sending signals Most important signals:Starting Processes Foreground processes are invoked by simply typing a command at the command line. $ find / -name README Running Long Processes The nohup command will stop a process from being Background processes are invoked by putting an "&" killed if you log off the system before it completes, by at the end of the command line intercepting and ignoring the SIGHUP and SIGQUIT $ find / -name README & (hangup and quit) signals $ nohup find / -name README & nohup: appending output to `nohup.out $ logout Managing Process PrioritiesJob Control in the Bash Shell Processes are scheduled according to priority - suspends foreground task jobs - lists background or suspended jobs fg - resume suspended task in the foreground bg - resume suspended task in the backgroundSpecify a job number for bg, fg and kill using %job 21
  • 22. DaemonsThe nice Command The word "Daemon" refers to a never-ending process, usually a system process that controls a system The nice command is used to start a process with a resource such as the printer queue or performs a user defined priority network service $ nice [-n ] The renice CommandThe renice command is used to change the priority of a Unit 11 - Linux Utilitiescurrently running process The find Command $ renice Search one or more directory structures for files that meet certain specified criteria Display the names of matching files or execute commands against those files Syntax: $ find path expression Sample Directory StructureIntegrated Process Management Various integrated tools exist for process management top, gpm, kpm Availability depends on distribution 22
  • 23. Using find Generally, you want to search a directory structure for files with certain names and list the names found. $ cd /home/joe find Examples $ find . -name phone ./shape/phone ./phone On many other UNIX systems, with find you have to tell it specifically to print the names using -print $ find . -name phone -print ./shape/phone ./phoneExecuting Commands with find locate Command The -exec option executes a command on each of the file names found. locate allows you to quickly find a file on the system, $ find . -name b* -exec ls -i {} ; based on simple criteria 187787 ./color/blue 187788 ./color/brown $ locate passwd 187792 ./shape/box /usr/share/man/man1/passwd.1.gz 202083 ./size big /usr/share/man/man5/passwd.5.gz 132754 ./blues /etc/passwd /usr/bin/passwd " { } " is a placeholder for each filename. The backslash escapes the following semicolon. Requires that the superuser runs updatedb regularly Most distributions run updatedb automaticallyInteractive Command Execution SuSE does not install locate/updatedb by default The -ok option also causes command execution but on The cut Command an interactive basis: Pull selected columns or fields from one or more files. $ find . -name b* -ok rm {} ; < rm ... ./color/blue > ? y Syntax: cut -f(ields) -d(elimiter) file(s) < rm ... ./color/brown > ? y cut -c(haracters) file(s) < rm ... ./shape/box > ? y < rm ... ./size/big > ? y < rm ... ./blues > ? yAdditional find Options 23
  • 24. Patterns with metacharacters should be in single quotes ( ) so that the shell will leave it alone Valid metacharacters with grep: $ . * ^ [ - ] . - Any single character * - Zero or more occurrences of the preceding characterThe grep Command [a-f] - Any ONE of the characters in the range a through f Searches one or more files or standard input for lines ^a - Any line that starts with a matching pattern z$ - Any line that ends with z Simple match or Regular Expression grep OptionsSyntax: grep [options] pattern [file1 ...] -v - Print lines that do not match -c - Print only a count of matching lines -l - Print only the names of the files with matching linesgrep Sample Data Files -n - Number the matching lines -i - Ignore the case of letters when making comparisons -w - Do a whole word search -f - Read expressions from file instead of command line Other greps fgrep allows only fixed strings (no regular expressions) egrep allows for multiple (alternate) patterns $ egrep 20500|40599|50599 phone1 Judith 20500 intern Leo 40599 extern Nannie 50599 extern The sort Command The sort command sorts the lines in the file specifiedBasic grep and writes the result to standard output Syntax: sort -t(delimiter) +field -options file $ cat animals dog.2 cat.4 penguin.10 $ sort animals cat.4 dog.2 penguin.10 $ sort +0.1 animals cat.4 penguin.10 dog.2 $ sort -t. +1 animals penguin.10grep with Regular Expressions dog.2 24
  • 25. cat.4 /bin/ls: ELF 32-bit LSB executable, Intel 80386, version 1, dynamically linked, stripped $ sort -t. -n +1 animals /home/peter: directory dog.2 /tmp/fake.jpg: PDF document, version 1.3 cat.4 penguin.10 The gzip, gunzip and zcat CommandsOptions: To compress or uncompress files use gzip, gunzip or zcat-d - sorts in dictionary order. Only letters, digits and spacesare considered in comparisons-r - reverses the order of the specified sort-n - sorts numeric fields in arithmetic valueThe head and tail Commands The head command can be used to view the first few lines of a file or files. The command syntax is: Syntax: $ head [-lines] file(s) The tail command displays the last few lines of a file or files. The join and paste Commands The command syntax is: $ tail [{-lines|+lines|-f}] file(s) join and paste combine files $ head -5 myfile $ ls -l | head -12 $ tail -20 file $ tail +20 file $ tail -f fileThe type, which and whereis Commands To find out what the path to a command is, use type $ type find echo find is /usr/bin/find echo is a shell builtin To find out where the binary is located, use which The Linux Graphical User Interface $ whereis find echo The "X Window System" is the GUI of Linux find: /usr/bin/find /usr/man/man1/find.1 echo: /bin/echo /usr/man/man1/echo.1 Developed at MIT in 1984 Current standards body: X Consortium To locate the binary, source and manual page files of a Shortname: X command, use whereis X uses client-server model with network connections $ which find echo /usr/bin/find Highly flexible /bin/echo Easy exchange of components Supports networked applications and sessions,The file Command independent of the OS With the file command, you can find out what the type of data in the file is.$ file /etc/passwd /bin/ls /home/peter /tmp/fake.jpg/etc/passwd: ASCII text 25
  • 26. Mouse Graphical