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