command line - part 1 - stat 133 - gaston · pdf filecommand line - part 1 stat 133 ... i one...
TRANSCRIPT
![Page 1: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/1.jpg)
Command Line - Part 1STAT 133
Gaston Sanchez
Department of Statistics, UC–Berkeley
gastonsanchez.com
github.com/gastonstat
Course web: gastonsanchez.com/stat133
![Page 2: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/2.jpg)
GUIs
2
![Page 3: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/3.jpg)
Graphical User Interfaces
I Windows and Mac use a Graphical User Interface (GUI) foryou to interact with the OS.
I GUIs are easy to learn
I GUIs rely on visual displays
I GUIs can be extremely useful
I GUIs have improved the friendliness and usability ofcomputers
3
![Page 4: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/4.jpg)
GUIs or Command Line?
I However, GUIs come with trade-offs
I They don’t allow you to have more control over what yourcomputer can do
I Some operations are labor intensive and repetitive
I You organize things by clicking and dragging with thecursor (which reduces reproducibility)
4
![Page 5: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/5.jpg)
GUI Disadvantages
I Lack of repeatability
I Lack of reproducibility
I Some tasks may be labor intensive using a GUI
I GUIs limit analyses on a cluster of computers
5
![Page 6: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/6.jpg)
Command Line
6
![Page 7: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/7.jpg)
Command Line
I Instead of using a GUI, we can use a command lineprogram
I The command line program is known as the shell
I By typing commands we perform tasks on the computer(without using a mouse)
7
![Page 8: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/8.jpg)
Shell
I You’re working with a program called the shell
I The shell interprets the commands you enter
I It runs the program you’ve asked for
I It coordinates what happens between you and theoperating system
I There are various kinds or flavors of shells: e.g. Bourne(BASH), Korn, C shell
8
![Page 9: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/9.jpg)
Command Line
I To interact with the shell we need a terminal emulator
I In Unix-like systems (e.g. Mac) the terminal is usuallyknown as “terminal”
I Windows does not really provide a terminal; instead itprovides the command prompt
9
![Page 10: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/10.jpg)
Command Prompt in Windows
Finding MS Windows command promptI Click the Start button
I Click All Programs
I Click Accessories
I Click Command Prompt
Windows command prompt is not a UNIX shell
10
![Page 11: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/11.jpg)
Shells for Windows
I Instead of using the command prompt you can use ad-hocshell environments for Windows
I e.g. Git-Bash, PowerShell, Cygwin
I Git for Windows provides a BASH emulation
I PowerShell is part of Windows Management Framework 4.0
I Cygwin is large collection of GNU and Open Source tools
11
![Page 12: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/12.jpg)
Mac Terminal
I Go to Applications
I Go to Utilities
I Click Terminal
12
![Page 13: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/13.jpg)
Try Some Commands
I date (current time and date)
I cal (calendar of current month)
I df (amount of free space in your disk drives)
I who (logged in users)
I echo ‘Hello’
13
![Page 14: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/14.jpg)
Shell
I Shells run in terminal emulators, or terminals
I In Mac OS X, the default reminal program is calledTerminal
I The command line is displayed within the terminal window
I The program behind the terminal is the shell
I There are many different shell programs
14
![Page 15: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/15.jpg)
BASH
The most common type of shell is BASH
I BASH: Bourne Again SHell
I BASH is the default shell for Linux
I BASH is usually the default shell on Mac
I type echo $SHELL to see your shell
I type bash to get a bash shell
15
![Page 16: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/16.jpg)
BASH
I A shell does much more than simply run commands
I It has wildcards for matching filenames
I It has a command history to recall previous commandsquickly
I It has pipes for making the output of one commandbecome the input of another
I It has variables for storing values for use by the shell
16
![Page 17: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/17.jpg)
Command who
I who displays a list of users that are currently logged in
I who am i (whoami) tells you the current user name
17
![Page 18: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/18.jpg)
Shell Command Syntax
command -options arg1 arg2
I Blanks and "-" are delimiters
I The number of arguments may vary
I An argument comes at the end of the command line
I It’s usually the name of a file or some text
I Many commands have default arguments
18
![Page 19: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/19.jpg)
Date and Calendar
I date
I cal (current calendar year)
I cal july 2015 (July 2015)
I cal jan 2000
I ncal -w july 2015 (week number)
19
![Page 20: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/20.jpg)
Options
command -options arg1 arg2
I Options come between the command and the arguments
I They tell the command to do something other than itsdefault
I They are usually prefaced with one or two hyphens
I e.g. ncal -w july 2015
20
![Page 21: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/21.jpg)
Some Control Sequences
keys descriptionCtrl + l clear screenCtrl + c stop current commandCtrl + z suspend current commandCtrl + k kill to end of lineCtrl + r search historyCtrl + n next history itemCtrl + p previous history item
21
![Page 22: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/22.jpg)
Manual Documentation
I To see the help documentation of a command use manfollowed by the name of the command:
– man cal
– man date
– man who
I q quits manual documentation
22
![Page 23: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/23.jpg)
Logging Out
I exit logs you out
I q quits manual documentation
23
![Page 24: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/24.jpg)
System Navigation
24
![Page 25: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/25.jpg)
Filesystem Reminder
I The nested hierarchy of folders and files on your computeris called the filesystem
I The filesystem follows a tree-like structure
I The root directory is the most includive folder on thesystem
I The root directory serves as the container ofr all other filesand folders
I A Unix-based system (e.g. OS X) has a single rootdirectoyr
I Windows users usually have multiple roots (C:, D:, etc)
25
![Page 26: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/26.jpg)
Paths
I Each file and directory has a unique name in the filesystem
I Such unique name is called a path
I A path can be absolute or relative
I An absolute path is a complete and unambiguousdescription of where something is in relation to the root
I A relative describes where a folder or file is in relation toanother folder
26
![Page 27: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/27.jpg)
Paths
I There are two special relative paths: . and ..
I The single period . refers to your current directory
I The two periods means your parent directory, one levelabove
27
![Page 28: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/28.jpg)
Home Directory
I User’s personal files are found in the /Users directory
I A user directory is the home directory
I cd (with no other arguments) returns you to your homedirectory
I echo $HOME prints your home directory
I cd ∼ takes you to your home directory
28
![Page 29: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/29.jpg)
Working Directory
I Another special type of directory is the so-called workingdirectory
I The working directory is the current directory where youperform any task
I pwd prints the working directory
29
![Page 30: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/30.jpg)
Changing Directories
I cd
I cd ..
I cd /
I cd ∼I cd ∼/Documents
30
![Page 31: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/31.jpg)
Absolute Path Names
/
Ax
B C
D
x
x
directory
file
From the root directory to D:cd /A/B/D
31
![Page 32: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/32.jpg)
Relative Path Names
/
Ax
B C
D
x
x
directory
file
Changing directories from D to C
cd ../../C
32
![Page 33: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/33.jpg)
Listing Contents in a Directory
I ls
I ls -1 (one entry per line)
I ls -l (list in long format)
I ls -a (show files starting with a dot)
I man ls (manual documentation)
33
![Page 34: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/34.jpg)
Listing Contents in a Directory
I ls / (specify root directory)
I ls /usr (specifying a directory)
I ls ∼ (home directory)
I ls -lt (long format, sorted by modification time)
34
![Page 35: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/35.jpg)
Listing Contents
/
Ax
B C
D
x
x
directory
file
Show contents in D from Cls ../B/D/
35
![Page 36: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/36.jpg)
Inspecting Files
36
![Page 37: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/37.jpg)
File Permissions
I run the command: ls -l
I directories may be displayed as: drwxr-xr-x
I files may be displayed as: -rw-r--r--
I file permissions are the 10 most left characters
I r means reads
I w means write
I x means execute
37
![Page 38: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/38.jpg)
File Permissions
Read from left to right the permissions mean
position description1 File type. A dash - means a plain file
and d means a directory.There are other less common options.
2-4 Owner permissions: read, write, and executepermissions for the file’s owner.
5-7 Group permissions: read, write, and executepermissions for the file’s group.
8-10 World permissions: read, write, and executepermissions for all other users.
38
![Page 39: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/39.jpg)
Type of File
Determine the type of a file:file filename
39
![Page 40: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/40.jpg)
Some commands for inspecting text files
I wc filename
I cat filename
I head filename
I tail filename
I more filename
I less filename
40
![Page 41: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/41.jpg)
Viewing file contents with less
I There are several commands that display the contents oftext files
I The most commonly used file viewer is less
I less presents the contents of that file on the screen onepage at a time
I There are various keyboard surtcuts to navigate in less
41
![Page 42: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/42.jpg)
Viewing file contents with less
key description
Page Up or b scroll back one pagePage Down or scroll forward one pagespaceUp Arrow scroll up one lineDown Arrow scroll down one lineG move to the end of text file1G or g move to the beginning of the text file/hello search forward to next occurrence of hellon search for the next search occurrenceh display help screenq quit less
42
![Page 43: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/43.jpg)
Quoting Files
If you want a word to contain whitespace (e.g. a filename witha space in it), surround it with single or double quotes to makethe shell treat it as a unit:ls "My file"
43
![Page 44: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/44.jpg)
Exploring a file
I cd into a given directory
I List the directory contents with ls -l
I Determine the contents of a file with file
I If it looks like it might be text, try viewing it with less
44
![Page 45: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/45.jpg)
Editing text files at the command line
I Sometimes it is more convenient to create or modify a fileright at the command line
I Although less is a convenient file viewer, it does not allowyou to edit the contents
I Depending on your operating system and shell tool, youmay have one or more command-line text editors:
I e.g. vi, nano, gedit
45
![Page 46: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/46.jpg)
Editing text files at the command line
I One common text editor is vi (there’s also vim)
I It should be available in Mac, and also in Git-Bash(Windows)
I Depending on your operating system and shell tool, youmay have one or more command-line text editors:
I Type which vi to fing out if you have it
46
![Page 47: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/47.jpg)
Editing text files with vi
I To create and start editing a file simply type vi followed bythe name of the new file:
vi newfile.txt
I Press the I key to start editing content
I When you’re done, press the ESC key
I Then type :wq to save and quit
I You can reopen it again with: vi newfile.txt
Google vi cheat sheet to find more information
47
![Page 48: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/48.jpg)
File Management
48
![Page 49: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/49.jpg)
Managing Files
Common actions
I creating a directory
I creating a file
I copying a file
I moving a file
I deleting a file
I searching a file
49
![Page 50: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/50.jpg)
Managing Files
Common actions
I creating a directory: mkdir
I creating a file: usually through a text editor
I copying a file: cp
I moving a file: mv
I deleting a file: rm
I searching a file: ?
50
![Page 51: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/51.jpg)
Creating Directories and Files
Create a directory "summer2015" in my Documents
cd ~/Documents
mkdir summer2015
Create an empty file "README.md" in summer2015
cd summer2015
touch README.md
51
![Page 52: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/52.jpg)
Copying Files
I cp is the command to copy files
I cp can be used in two ways:
I cp file1 file2 copies file1 into file2
I cp file1 directory copies file1 into a directory(directory must already exists)
52
![Page 53: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/53.jpg)
Copying Files
Copying functions.R from Documents to HW6
cp ~/Documents/functions.R ~/Desktop/HW6/
Copying starwars.csv to current directory
cp ~/Documents/starwars.csv .
53
![Page 54: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/54.jpg)
Deleting files
Deleting README.md and starwars2.csv
cd ~/Documents/summer2015
rm README.md
rm starwars2.csv
54
![Page 55: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/55.jpg)
Wildcards
I the shell provides special characters to specify filenames
I these special characters are called wildcards
I using wildcards allow you to select filenames based onpatterns of characters
I these wildcards are similar to some regular expressioncharacters
55
![Page 56: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/56.jpg)
Wildcards
wildcard description
* matches any characters? matches any single character[characters] matches any character that is
a member of the set characetrs[!characters] matches any character that is not
a member of the set characters[[:class:]] matches any character that is
a member of the specified class
56
![Page 57: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/57.jpg)
Example
Create a directory dummy, cd to it, and then create empty files:
$ mkdir dummy
$ cd dummy
$ touch AGing.txt Bing.xt Gagging.text Going.nxt ing.ext
$ ls
57
![Page 58: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/58.jpg)
* Wildcard
Use * to refer to multiple files at once; it stands for anything
$ ls
AGing.txt Bing.xt
Gagging.text Going.nxt ing.ext
$ ls G*
Gagging.txt Going.nxt
$ ls *.xt
Bing.xt
58
![Page 59: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/59.jpg)
? Wildcard
The question mark ? represents a single character
$ ls
AGing.txt Bing.xt
Gagging.text Going.nxt ing.ext
$ ls ?ing.xt
Bing.xt
59
![Page 60: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/60.jpg)
[] Wildcard
Brackets [] can be replaced by whatever characters are withinthose characters:
$ ls
AGing.txt Bing.xt
Gagging.text Going.nxt ing.ext
$ ls [B]ing.*
Bing.xt
$ ls [A-G]ing.*
Bing.xt
60
![Page 61: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/61.jpg)
Combining Wildcards
Wildcards can be combined:
$ ls
AGing.txt Bing.xt
Gagging.text Going.nxt ing.ext
$ ls *G*
AGing.txt Gagging.txt Going.nxt
$ ls *i*.*e*
Gagging.text ing.ext
61
![Page 62: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/62.jpg)
Test Yourself
AGing.txt Bing.xt
Gagging.text Going.nxt ing.ext
What command produces the output above:
A) ls *ing.*xt
B) ls ?ing.*xt
C) ls ?ing.?xt
D) ls ?ing.xt
E) ls *ing.?xt
62
![Page 63: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/63.jpg)
Test Yourself
AGing.txt Going.nxt ing.ext
What command produces the output above:
A) ls *ing.*xt
B) ls ?ing.*xt
C) ls ?ing.?xt
D) ls ?ing.xt
E) ls *ing.?xt
63
![Page 64: Command Line - Part 1 - STAT 133 - Gaston · PDF fileCommand Line - Part 1 STAT 133 ... I One common text editor is vi (there’s also vim) ... I cp file1 file2 copies file1 into file2](https://reader031.vdocuments.us/reader031/viewer/2022022501/5aa6f6217f8b9ad31c8b568d/html5/thumbnails/64.jpg)
Wildcard Examples
Pattern Matches
* all filesa* any file beginning with “a”*.txt any file ending with .txt
b*.txt any file beginning with “b” followedby any characters and ending with “.txt”
[gst]* any file beginning with eithera “g”, and “s”, or a “t”
[[:digit:]]* any file beginning with a number[[:upper:]]* any file beginning with an uppercase letter
64