CCIT4052 2022-102 PDF
Document Details
Uploaded by ExcitingRhodonite3899
HKU School of Professional and Continuing Education
Dr. Theo Raymond Chan
Tags
Related
- PCSII Depression/Anxiety/Strong Emotions 2024 Document
- A Concise History of the World: A New World of Connections (1500-1800)
- Human Bio Test PDF
- Vertebrate Pest Management PDF
- Lg 5 International Environmental Laws, Treaties, Protocols, and Conventions
- Fármacos Anticoncepcionais e Disfunção Erétil PDF
Summary
This document provides a concise introduction to Linux and Unix shells, covering features, examples, and common commands.
Full Transcript
*nix Basic Introduction to Shells & *nix Commands The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] What is UNIX? • Unix is a proprietary Ope...
*nix Basic Introduction to Shells & *nix Commands The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] What is UNIX? • Unix is a proprietary Operating System that was developed by Bell Labs research center in the 1970s by Ken Thompson, Dennis Ritchie, and a number of other developers. • UNIX is now a trademark of the Open Group • it is neither available for free nor is its source code open-source Ken Thompson • An OS must be certified to be called UNIX • Linux is not certified so it is UNIX-like Father Of C And UNIX, Dennis Ritchie The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] What is Linux? • Linux is the kernel – was developed by Linus Torvalds in 1991 based on the Unix OS • Kernel − The kernel is the heart of the operating system. It interacts with the hardware and most of the tasks like memory management, task scheduling and file management. • Shell − The shell is Command Line Interface (CLI), the utility that you enter commands into the system. When you type in a command at your terminal, the shell interprets the command and calls the program that you want. The shell uses standard syntax for all commands. C Shell, Bourne Shell and Korn Shell are the most famous shells which are available with most of the Unix variants. Linus Torvalds The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] History of *inx Shell The C shell (csh) was developed for Berkeley Software Distribution (BSD) UNIX systems by Bill Joy while he was a graduate student at the University of California, Berkeley, in 1978. Tenex introduced file name and command completion in addition to command-line editing features. The Tenex C shell (tcsh) remains backward-compatible with csh but improved its overall interactive features. The tcsh was developed by Ken Greer at Carnegie Mellon University. Bill Joy One of the key design objectives for the C shell was to create a scripting language that looked similar to the C language. The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] History of *inx Shell The Bourne-Again Shell, or Bash, is an open source GNU project intended to replace the Bourne shell (sh). Bash was developed by Brian Fox and has become one of the most ubiquitous shells available (appearing in Linux, Darwin, Windows®, Cygwin, Novell, Haiku, and more). As its name implies, Bash is a superset of the Bourne shell, and most Bourne scripts can be executed unchanged. it supports filename wildcarding, piping, here documents, command substitution, variables and control structures for condition-testing and iteration. Brian Fox The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] History of *inx Shell Source : http://www.ibm.com/developerworks/library/l-linux-shells/l-linux-shells-pdf.pdf The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] *inx Shell Features Purpose Example Command history ability to re-execute previous commands quickly Scripting create programs by placing shell commands in a file to run Alias create shortcuts to longer commands Variables store information that can be used to modify the functionality of the shell or commands Wildcard match filenames with expression ls DSC*.jp* Piping send command output to another command as input cat file1 | grep error Here Documents feed multiple lines into command tr a-z A-Z <<END_TEXT decrypted text in lower case END_TEXT Command Substitution replace command-output as part of command parameter echo `date` sh myHourlyJob.sh The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] *inx Shell Features sh csh tsh ksh bash Command History û ü ü ü ü Command Alias û ü ü ü ü Shell Scripts ü ü ü ü ü Filename Completion û ü* ü ü* ü Command Line Editing û û ü ü* ü Job Control û ü ü ü ü Source : http://unixhelp.ed.ac.uk/shell/oview1.1.html The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Command format • Basic format: • command [options] [arguments] • Options change the behavior of command • Vary based on command • option format: -a • Can be combined: -abc = -a -b -c • Arguments are used to provide additional information for a command The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Command History • View previous commands: history • Bring up previous commands: up arrow • Modify previous commands: left arrow, right arrow, etc. • Execute a previous command: !<num> The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Shell Variables • Temporary storage of data in memory • Assigning (no $): • FOO=“hello” • CWD=`pwd` • BAR=“Hello $NAME” • Using (need a $): • $FOO • echo “Hello $NAME” The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Shell Variables • Used to store system information • View with echo command: echo $HISTSIZE • Modify: HISTSIZE=500 • Changes are temporary • Place commands in ~/.bashrc to make permanent The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] PATH Variable • Determines where commands are executed from • May need to be modified for custom software • Example: The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Find command location • Hard to determine where a command is located • Instead of searching directories in the PATH variable manually, use the which command: – which cal – which date The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Aliases • Used to make shortcuts for longer commands • View aliases: alias • Create alias: alias name=command • Change are temporary • To make permanent, place alias command in ~/.bashrc file The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Globbing • • • • Used to match sets of files in a directory * = match zero or more of any characters ? = match exactly one of any character [ ] = match exactly one of a set of characters: – echo [abc]*.txt – echo [!abc]*.txt The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Quoting • Double quotes “x” – used to disable the meaning of some metacharacters, like glob characters • Single quotes ‘x’– used to disable the meaning of all metacharacters – Can use \ to disable next character only • Backquotes ` – used to execute a command within another command line The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Control Statements • The ; character - Used to separate commands on a command line • The && characters – Used to execute the second command if the first command succeeds • The || characters – Used to execute the second command if the first command fails The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Exercise 002 1. Read NDGLE Chapter 5 2. Complete Lab 5 “Command Line Skills” The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Command Manual The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Command Manual Page • Man pages are usually available for commands, functions or files • Man pages are available on the local system • To view a man page, first access a terminal or console and type man command Practice makes perfect! Just as you practice executing Linux commands, you should practice reading man pages to become a more capable user The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Controlling the man page display • The man page will be displayed by a pager program, either less or more (less is usually used). • Both pagers use h to view help, spacebar to move forward, / to start a search and q to quit. • Less movement commands: Command Function Command Function Return (or Enter) Go down one line 1G Go to beginning Space Go down one page G Go to end /term Search for term h Display help n Find next search item q Quit man page The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Man page sections • The word sections has two important meanings for man pages. • Each man page is broken down under different section headings like NAME, SYNOPSIS and DESCRIPTION. • Every man page is categorized into a particular section like user commands, file formats, or system administration. The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Common sections of a man page Section name Purpose NAME SYNOPSIS Provides the name of the command and a very brief description. Provides examples of how the command is executed. See below for more information. Provides a more detailed description of the command. DESCRIPTION OPTIONS FILES AUTHOR Lists the options for the command as well as a description of how they are used. Often this information will be found in the DESCRIPTION section and not in a separate OPTIONS section. Lists the file that are associated with the command as well as a description of how they are used. The name of the person who created the man page and (sometimes) how to contact the person. REPORTING BUGS Provides details on how to report problems with the command. COPYRIGHT Provides basic copyright information. SEE ALSO Provides you with an idea of where you can find additional information. This also will often include other commands that are related to this command. The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Man pages SYNOPSIS • One of the most important sections on a man page is the SYNOPSIS. • The SYNOPSIS provides a concise description of the way the command can be used. • The square brackets, [ and ], are used to indicate optional items. • The vertical bar (|) indicates an exclusive or. • The ellipses (…) indicates one or more. The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Searching within a man page • To initiate a search type / • Follow with the keyword to locate • Press Enter to view first match • Press n to view the next match • Press N to view the previous match The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Man page sections Man pages are placed into sections. There are nine standard sections: 1. 2. 3. 4. 5. 6. 7. Executable programs or shell commands System calls (functions provided by the kernel) Library calls (functions within program libraries) Special files (usually found in /dev) File formats and conventions, e.g. /etc/passwd Games Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7) 8. System administration commands (usually only for root) 9. Kernel routines [Non standard] The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Determining which section The section number of manual is enclosed in parentheses when viewing a man page. The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Searching the sections • To match man pages that have names that match a term, you can use the man command with the -f option. • The command whatis term is equivalent to the man -f term. The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Searching man pages by keyword • The man command has an option, -k, which takes a keyword as an argument. • Executing man -k keyword, will search all the man pages descriptions for the keyword. • The apropos command is equivalent to using the man command with the -k option. The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Specifying a section • If the command or file exists in more than one section of the manual, then it may be necessary to specify a manual section in order to view the correct man page. • For example, passwd is found in both sections 1 and 5. To view both passwd man pages, the following commands could be executed: • man 1 passwd • man 5 passwd The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Additional sources of help • The standard GNU option for documentation is --help. Use this to display basic command usage, which is similar to a man page. • Software often comes packaged with additional documentation that may be found under the /usr/doc or /usr/share/doc directory. These sub-directories often contain README files and other additional documentation. The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Finding commands and documentation • The whereis command will display the location of a command executable, as well as its source and documentation, if available. The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Find any file or directory • The locate command is able to find any file as long as the user has permission to access the containing directory by searching a database of filenames on the system. • The updatedb command is typically scheduled to update the locate database daily, but the root user can execute updatedb to immediately update the locate database. The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] The locate command • The locate command will match any part of a filename, so locate readme could match a file named abcreadme123 • The -c option provides a count of the number of matching files: locate -c readme The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Exercise 005 1. Read NDGLE Chapter 6 2. Complete Lab 6 “Getting Help” The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Files Manipulation The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Files and Directories • File contain data (text, graphics, etc) • Directories store filenames • Top level directory: / (AKA, root directory) • Example directory structure: The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Directory path • Directions to a specific file or directory • Directions given from / directory are called “absolute” paths • Directions given from the current directory are called “relative” paths The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] The home directory • Each user has a home directory • Typically /home/bob for a user named bob • Place to store your own files • Normally users can’t access the home directory of other users • The ~ character symbolizes the home directory The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] The current directory • The directory that your shell is currently in • Can be displayed with the pwd command • Might also be displayed in your prompt The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Changing directories • Use the cd command • With no arguments, takes you to your home directory • ~bob would refer to bob’s home directory The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Absolute vs relative pathnames • Absolute pathnames always provide directions from the root directory (/) • Relative pathnames always provide directions from the current directory. • To refer to one directory above current directory, use the .. characters • To refer to the current directory, use the . character The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Listing files in a directory • List files with the ls command • Different file types may be highlighted by colored filenames: • • • • plain file A file that isn't a special file type directory A directory file (contains other files) executableA file that can be run like a program symbolic link A file that points to another file • Display of filenames in color is the result of the -color option The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Common ls options • Many options to the ls command, including: • -a – display all files, including hidden files • -l – long display listing • -h – Give file sizes in human readable sizes • -R – Recursive listing • -S – Sort output based on file size • -t – Sort output based on modification time • -d – Don’t display directory contents The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Copying files • The cp command copies files: • cp [source] [destination] • Use –v option to display copy process • Use –i to prompt overwrites • Use –n to avoid overwrites • Use –r to copy directory structures The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Moving files • The mv command copies files: • mv [source] [destination] • The mv command also is used to rename files • Supported options that work the same as the cp command: -i, -n and –v The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Creating files • Editors can be used to create files (discussed later in this course) • Use the touch command to create an empty file • The touch command also updates the modification timestamp of a file The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Removing files • The rm command is used to delete files • File deletion is permanent! • Use -i to avoid accidental deletion when using globs • Delete directories with the –r option or rmdir if the directory is completely empty The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Making directories • Use the mkdir command to create directories The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected] Exercise 006 1. 2. 3. 4. Read NDGLE Chapter 7 Complete Lab 7 “Navigating the Filesystem” Read NDGLE Chapter 8 Complete Lab 8 “Managing Files and Directories” The contents of this document remain the property of and may not be reproduced in whole or in part without the express permission of Dr. Theo Raymond Chan. For information please contact with [email protected]