command line - part 1 - stat 133 - gaston · pdf filecommand line - part 1 stat 133 ... i one...
TRANSCRIPT
Command Line - Part 1STAT 133
Gaston Sanchez
Department of Statistics, UC–Berkeley
gastonsanchez.com
github.com/gastonstat
Course web: gastonsanchez.com/stat133
GUIs
2
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
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
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
Command Line
6
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
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
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
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
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
Mac Terminal
I Go to Applications
I Go to Utilities
I Click Terminal
12
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
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
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
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
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
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
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
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
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
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
Logging Out
I exit logs you out
I q quits manual documentation
23
System Navigation
24
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
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
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
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
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
Changing Directories
I cd
I cd ..
I cd /
I cd ∼I cd ∼/Documents
30
Absolute Path Names
/
Ax
B C
D
x
x
directory
file
From the root directory to D:cd /A/B/D
31
Relative Path Names
/
Ax
B C
D
x
x
directory
file
Changing directories from D to C
cd ../../C
32
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
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
Listing Contents
/
Ax
B C
D
x
x
directory
file
Show contents in D from Cls ../B/D/
35
Inspecting Files
36
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
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
Type of File
Determine the type of a file:file filename
39
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
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
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
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
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
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
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
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
File Management
48
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
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
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
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
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
Deleting files
Deleting README.md and starwars2.csv
cd ~/Documents/summer2015
rm README.md
rm starwars2.csv
54
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
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
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
* 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
? 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
[] 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
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
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
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
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