Week2_P2_ShellCommands_FilesAndEditors.pdf

Full Transcript

CS 131.01 Processing Big Data: Tools and Techniques 2. Dealing with Files San José State University Slides are adapted from slides created by Dr. Andreopoulos 1 Optional readings 5th Internet edition available online: https://linuxcommand.org/tlcl.php/ Ch. 1-3, 6-8, 11-12 Ch. 1-4 Ch. 1-5 2 ...

CS 131.01 Processing Big Data: Tools and Techniques 2. Dealing with Files San José State University Slides are adapted from slides created by Dr. Andreopoulos 1 Optional readings 5th Internet edition available online: https://linuxcommand.org/tlcl.php/ Ch. 1-3, 6-8, 11-12 Ch. 1-4 Ch. 1-5 2 Bash shortcut tip of the day Pressing <Tab> auto-completes your line 3 Viewingand Editing Files ● ● Different ways to display the contents of files. How to use the nano and vi editor. 4 Displaying the Contents of Files cat file more file less file head file Display the contents of file. Pager: Browse through a text file. Pager, uses less memory. Output the beginning (or top) portion of file. tail file Output the ending (or bottom) portion of file. 5 Head and Tail ● ● Displays only 10 lines by default Change this behavior with head -n ○ ○ n = number of lines tail -15 file.txt 6 Polling question What does this do? head -n 10 filename A) B) C) Print the first 10 lines of filename Print the last 10 lines of filename None of these 7 Polling question What does this do? tail -100 filename A) B) C) Print the first 100 lines of filename Print the last 100 lines of filename None of these 8 Viewing Files in Real Time tail -f file Follow the file. Displays data as it is being written to the file. 9 Demo 10 Nano Editor ● ● ● ● Nano is a simple editor. Easy to to learn. Not as advanced as vi or emacs. If nano isn't available, look for pico. 11 Demo - Nano 12 Summary ● ● There are various commands that display the contents of files. The Nano editor is easy to use and learn. 13 Editing Fileswith Vi 14 What You Will Learn ● How to use the vi editor. 15 The Vi Editor ● ● ● ● Has advanced and powerful features Not intuitive Harder to learn than nano Requires a time investment 16 The Vi Editor vi [file] Edit file. vim [file] Same as vi, but more features. view [file] Starts vim in read-only mode. 17 Vi Command Mode and Navigation k j h l w b Up one line. Down one line. Left one character. Right one character. Right one word. ^ $ Left one word. Go to the beginning of the line. Go to the end of the line. 19 Vi Navigation Keys 20 Vi Insert Mode (to exit you type Escape) i Insert at the cursor position. I Insert at the beginning of the line. a Append after the cursor position. A Append at the end of the line. o Insert right after the current line O Insert right before the current line 21 Vi Line Mode :w :w! :q :q! :wq :x Writes (saves) the file. Forces the file to be saved. Quit and display message if file modified. Quit without saving changes. Write and quit. Same as :wq. 22 Vi Modes : default is navigation/command mode Mode Key When you are in Command mode (Esc): Command Esc Insert iIaAoO Line (enter cmd) : Search / or ? ‘n’ is find next 24 Polling question / searches forward, ? searches backward in vi A) B) True False 25 Polling question / searches forward, ? searches backward in vi A) B) True False Find next is ‘n’ (same as / without parameters) 26 Vi - Deleting Text x dw dd D Delete a character. Delete a word. Delete a line. Delete from the current position. 27 Vi - Copying and Pasting yy Yank (copy) the current line. p Paste the most recent deleted or yanked text below the current line. P Paste above the current line. <num>yy Yanks that many lines from the cursor position. 28 vi - Undo / Redo Undo u Redo Ctrl-R 29 Vi - Searching /<pattern> ?<pattern> Start a forward search. Start a reverse search. 30 Vi - Search and replace :%s/<pattern>/replace/g :%s/ctrl+Vctrl+M//g Global search/replace. Remove newlines leftover from Windows. 31 Summary ● ● More advanced than nano Vi has four modes 32