170 likes | 288 Views
History Command. UNIX records all of the commands executed Up arrow displays the previous command Successive up arrows step through the previous commands The variable $HISTFILE contains a record of all commands >history > commands creates a file with a record of past executed commands.
E N D
History Command • UNIX records all of the commands executed • Up arrow displays the previous command • Successive up arrows step through the previous commands • The variable $HISTFILE contains a record of all commands • >history > commands creates a file with a record of past executed commands
The script command • The script command is handy >script OR >script scriptFile • It logs all your input and system output to a file • If you do not specify a file, the log goes to a file called typescript • Type >exit to terminate the logging
Escaping special characters • Problem: we might want to override special characters and have UNIX interpret them literally • Example >echo $HOST ~ echoes the host computer plus the home directory name >echo \$HOST \~ echoes the literal string >echo '$HOST ~' echoes everything in quotes literally >echo "$HOST ~" echoes everything except for \ and $
The tar command • Problem: want to copy a bunch of files at once • Solution: tar glues a series of files together • Note: this is not compression • tar commands • Glue files together: >tar –cf tarfile fileList • Verbose output: tar -cvf tarFile fileList • List files in a tarFile: tar -tf tarFile • Extract files from tarFile: >tar -xvf tarFile • Note: The dash is not needed in the above commands
The compress utilities • Compression greatly reduces the size of the files being transferred • Compress a file and create a .gz extension>gzip fileName • Uncompress a file to its original name>gunzip fileName.gz or >gzip –d fileNameOr > tar zxovf fileName.tar.gz • Full procedure • First tar, then gzip • First, extract, then untar
Communicating with users >talk [username] starts a chat program >mail initiates a basic mail program >finger username displays the content of a file called ~/.plan Procedure • Suppose we are user harley • echo whatever > ~/.plan • Other users can type: >finger harley • whatever will display on their terminal
UNIX Toolbox Philosophy • Create powerful command line tools • Each does one thing • Each does it very well • Design these tools to work well together • The tools handle streams • Filters are commands whose inputs and outputs are stream • Job is to transform the input • input is either from stdin or a file • The tools are normally C programs
Streams and Redirection • Stream: a flow of data from source to sink • C programs have default stream sources or sinks • stdin: standard input (keyboard) • stdout: standard output (monitor) • stderr: error output (often same as stdout) • Redirection: alter the default source or sink • Examples • redirect stdout to a file >ls ~ > fileName • redirect a file list and append to a file>ls ~ >> filename • redirect input from a file instead of stdinmail username <message • redirect error messages: grep harley ~ 2> file • redirect both stdout and stderr grep harley ~ >& • redirect, append both stdout and stderr: grep harley ~>>&
Concatenating files • Method one (cp command) >cp file1 file2 file3 destination • Method two (cat command with redirection) cat jnk.txt grades >both.txt Note: cat is the command to type the contents of a file • Method three (concatenation redirection) • cat jnk.txt >both.txt • cat grades >>both.txt
Pipes • Send the output of one command to the input of the next (cmd1 | cmd2) • The pipe character: | • Example: ls ~ | grep a* | sort –u • Example: ls –al | more
Editors: History • ed: a line editor (Ken Thompson) • ex: An improved version of ed (Bill Joy) • sed: stream editor, can apply editing commands from the command line using streams • vi: Screen oriented text editor (We will use this one) • Standard UNIX editor • available on all UNIX systems • smaller and faster than emacs • minimal keystrokes to do tasks, hard to learn • emacs: (Richard Stallman) • powerful, cumbersome, part of the GNU IDE • Others: these work more like those on windows, but they are not guaranteed to be on every UNIX system
Enter and exit the program To enter: >vi fileName To exit: colon and then wq (write and quit) or q! (quit without write) Modes insert mode: input text into a file command mode: keyboard command key last line mode: longer commands vi editor input command key esc command : or / esc last-line
File Pointer • All commands are relative to a single file pointer • Central to using vi is navigating around the file • Navigation commands: • arrows work as you would expect on most vi • Go to line 1: 1G • Go to line 50: 50G • Go forward one word: w • Go to the end of the line: $ • Go to the beginning of the line:^
Delete, Cut, Copy, Paste • delete one word: dw • delete 4 words: 4dw or d4w • delete (cut) 50 lines 50dd • yank (copy the current line: y • yank (copy) 50 lines: 50y • yank (copy) into one of 26 buffers buffer c: "cy • paste cut/copied text after the current line: P • paste cut/copied text before the current line: p • paste from buffer c: "cp • delete one character: x • delete 50 characters: 50x
More vi Commands • Undo command after a mistake: u • Undo all recent commands: U • join this line and the next: j • joint four lines: 4j • replace a single character: r • Save and exit: ZZ • Change the tab stops: set tabstop=4 • Entering unix commands: sh
vi Insert Mode Examples • change rest of a line until <esc>: c$ • Change a word until <esc>: cw • Change line until <esc>: cc • Insert before file pointer until you <esc>: i • Append after file pointer until <esc>: a • Insert below until <esc>: o • Insert above until <esc>: O • Replace until <esc>: R
VI Last-Line examples • Search for the word cat: /cat • Search backwords for cat: ?cat • Replace all this line: cat by dog: s/cat/dog/g • Replace once: cat by dog: s/cat/dog/ • Replace once on every line: %s/cat/dog/ • Replace all every line: %s/cat/dog/g • Save the file: w • Save the file as: w fileName • Save and quit: wq • Merge a file into the current place: r fileName • Display line numbers: set number • Don't display line numbers: set nonumber