------------------------- Week 03 Notes for NET2003 ------------------------- -Ian! D. Allen - idallen@idallen.ca Remember - knowing how to find out an answer is more important than memorizing the answer. Learn to fish! RTFM! (Read The Fine Manual) Review: - you know how to work around the Knoppix 5.1.0 desktop bug - you can answer questions about Internet governance - you can display any file all-at-once or one-page-at-a-time - you can erase your typing by character, word, or line - you can quickly and easily re-execute commands typed to the shell - you can have the shell auto-complete file names for you (less typing) - you can search for keywords in manual pages - you recognize absolute and relative pathnames - you know about these commands: cat cp dir echo exit ifconfig less ls man passwd scp sort ssh stty su vim wget Q: From the given current directory of /tmp/idallen, which of the following command lines is the same as "cp file1 file2"? $ pwd /tmp/idallen $ cp ./file1 ./file2 $ cp ././././././file1 ././././././file2 $ cp file1 ../idallen/file2 $ cp ../idallen/file1 file2 $ cp ../idallen/file1 ../idallen/file2 $ cp ../../tmp/idallen/file1 file2 $ cp file1 ../../tmp/idallen/file2 $ cp ../../tmp/idallen/file1 ../../tmp/idallen/file2 $ cp ./././././../../tmp/idallen/file1 ./././././../../tmp/idallen/file2 $ cp /tmp/idallen/file1 /tmp/idallen/file2 $ cp /tmp/../tmp/idallen/file1 /tmp/idallen/../idallen/file2 $ cp file1 /tmp/idallen/../../tmp/idallen/file2 $ cp /file1 /file2 $ cp file1/. file2/. $ cp idallen/file1 idallen/file2 $ cp idallen/../file1 idallen/../file2 $ cp ./idallen/file1 ./idallen/file2 $ cp tmp/idallen/file1 tmp/idallen/file2 $ cp tmp/../idallen/file1 tmp/../idallen/file2 $ cp ./tmp/idallen/file1 ./tmp/idallen/file2 Q: What is the ssh command line to connect from Knoppix to your account on the Course Linux Server? Q: What does "echo */." output? Q: What does "echo ./*" output? Q: What is the output on your screen of "sort foo foo" if foo contains: 2 22 11 1 Q: How do you set your backspace key to be ^H ? Q: How do you fetch the file at URL http://tc.ca/ into your current directory? Linux basics - continued ------------ Another terminal control character: ^Z - suspends/stops (does not kill) the current process - allows you to suspend VIM (or something else) so you can do other work - use the built-in shell command fg to resume the process again - use the built-in shell command jobs to see suspended processes Q: what does the ^Z keyboard signal do to a process? Q: T/F the ^Z keyboard signal terminates a process, similar to ^C Unix is an O/S designed by programmers for programmers - command-line driven (programmers didn't use the GUI) - things work silently (no confirmation) - messages appear only when things fail - most command names are cryptic abbreviations! - like vim, the commands are hard to learn but easy to use Q: T/F most Unix/Linux commands ask for confirmation of serious actions The Unix Shell (e.g. "bash" - Bourne Again SHell) - Notes: shell_basics.txt - The Unix/Linux Shell - purpose of a shell is: to find and run commands - with some support for programming (scripts) - can separate multiple commands using ";" $ date ; date ; date - in shell scripts, avoid ";" - put each command on its own line - shell is an unprivileged program - standard Linux shell is named "bash"; you can start up multiple copies of bash by typing its name as a command - type "exit" to cause a shell to terminate - shell prompts when it reads from your keyboard - shell does not prompt if reading input from a file! - most other Unix programs do *not* prompt when they read your keyboard - Notes: shell_prompt.txt - Setting the BASH shell prompt - shell splits your command line on blanks (white space) - first non-redirection word is the command name - the rest of the arguments are passed to the command - Notes: arguments_and_options.txt - Options and Arguments on Command Lines - shell can expand file GLOB patterns (similar to DOS wildcard) - shell helps with wildcards (GLOB) - * matches zero or more characters - ? matches a single character (any character, even a space) - [abc] matches a single character from the list abc - like ls, GLOB patterns never match names that start with a period (hidden pathnames), unless you explicitly put a real period at the start - echo * - does not match hidden files - echo ?* - does not match hidden files - echo [.]* - does not match hidden files - echo .* - matches only hidden files (pattern starts with a real period) - Notes: glob_patterns.txt - GLOB patterns (wildcard pathname matching) - shell can expand variables, both local and environment - e.g. you can see a $variable as in: echo "$SHELL" - shell does the expansion, not the command! - you can use ";" to separate commands, e.g.: sleep 4 ; echo BOO Q: what is the main purpose of a shell program/ Q: T/F any program that wants to read from your keyboard will prompt you for input Q: T/F the number of extra blanks between arguments on the shell command line doesn't matter - one blank is the same as twenty Q: T/F shell GLOB patterns *never* match leading periods in names Q: T/F "echo [.]*" matches hidden file names Q: T/F In the command "echo *" it is the "echo" command that expands the "*". Q: T/F In the command "ls *" it is the "ls" command that expands the "*". New command names and options (add these to your list): - see Notes file unix_command_list.txt - keep a list of commands and their common uses - "pwd" (shell built-in) shows the current directory of the shell - "cd" (shell built-in) changes the current directory of the shell - with no arguments, changes to your home (login) directory - "mkdir" creates a new directory (but does not change to it) - "rmdir" deletes (removes) an empty directory - use "rm -r" to remove recursively a (non-empty) directory - "touch" creates a new empty file or updates its modify time to now - "mv" renames (moves) a pathname - "rm" removes files (not directories, unless using -r) - "man -k" or "apropos" looks up a keyword in manual page title lines - "date" shows (or sets) the current date/time - "hostname" tells (or sets) the name of this computer - "cal" shows an ASCII calendar (try: cal 9 1752) - "du" shows disk block usage under the current directory - "sum" shows a quick checksum of file contents (see also "md5sum") - two files with the same checksum *probably* have identical content - "file" tells what kind of file a pathname is - helpful if the file is a compressed or executable file - "find" lists recursively all the pathnames under the current directory - contrast it with "ls" that shows only the current directory - find has lots of other options to find things by name, size, etc. - "grep" searches for a pattern inside (text) files - "head" shows lines at the head (top) of a file - "tail" shows lines at the end (tail) of a file - useful to see the end of a system log file - "wc" counts the lines, words, chars in a file - "who" shows who is logged in to this machine - "w" shows who is logged in to this machine - "bash" starts a new copy of the shell, until you "exit" - shell scripts are executed by a new copy of the shell - to make a (script) file executable: chmod +x filename Q: give a one-line summary of what any of the above commands do Directory tutorial: - http://www.ee.surrey.ac.uk/Teaching/Unix/unix1.html Commands: see Notes file unix_command_list.txt - keep a list of commands and their common uses - use "rm -r dir" to remove an entire directory tree and all files - know how to read man pages (man page syntax meaning) - don't send binary (executable machine code) files to your screen - check the contents with the "file" command first - grep pattern file - crude Data Mining: selecting content by looking for lines - using grep on course notes: grep ssh ~idallen/public_html/teaching/net2003/07w/notes/*.txt - grep -v finds lines that do *not* contain the pattern - head(-5) tail(-5) apropos(man -k) - wc(-lwc) ls(-ld) grep(-v) sort(-nr) rm(-r) - "sort" sorts alphabetically (usually ASCII collating order) - need to use -n option to sort by leading number - other options change sort direction, etc. - "ls -ld" is needed to see the attributes of a directory (instead of seeing the attributes of the directory contents) Q: "ls -l dir" will show the *contents* of a directory in long form; how do I make ls show me the information about the directory itself? Q: How do I remove a directory and everything inside it? See Notes file miscellaneous.txt - learn various ways of getting out of programs - Unix line ends are \n characters (not \r or \r\n) - "echo a | wc -c" counts 2 characters, not just one! - in vim: set fileformat=unix or dos or mac Q: How many characters are counted here: echo abc | wc -c Q: If a Unix text file contains 10 lines, each with one letter on it, what is the overall size of the file (in bytes)? Q: If a Windows/DOS text file contains 10 lines, each with one letter on it, what is the overall size of the file (in bytes)?