The command line is like the R command prompt: you insert code, hit enter, and then the computer executes your command.
However, instead of inserting R code, you insert Shell Script.
In this class, we will use the command line primarily for two things:
Other words for command line: shell, terminal, command line interface (cli), and console.
There are many types of shells, each with their own scripting language. We will use the bash scripting language for this class.
A huge difference between R and bash is how commands/functions are called.
f(x, y = 1)
f x --y=1
f x -g
would incorporate the g
flag.If you are using Linux or Mac, then you can keep going. If you are using Windows, you need to first download and install git (and thus git bash) from here: http://git-scm.com/download/win. You might need to restart R Studio if you are already running it.
Open up the terminal
Windows: Open up the Git Bash
app. It should look
like this:
Mac: On your Mac, do one of the following:
Ubuntu: Do one of the following
Open the dash and search for “terminal”. Open up the terminal.
Use keyboard shortcut: Ctrl+Alt+T
It should look like this:
All commands get placed after the dollar sign.
The path before the dollar sign is the working directory of the terminal, not R’s working directory. It’s where the shell will reference all files from.
The tilde “~
” is shorthand for the “home directory”.
Each computer has a home directory that is the “default
directory”.
pwd
: Print working directory. Show the current
working directory. This is like getwd()
in R.
pwd
/home/dgerard/Dropbox/teaching/stat_413_613/lectures/01_git
ls
: List the current files and folders in a
directory.
ls
01_basic_bash.Rmd
01_figs
01_git_branch.Rmd
01_git_github.Rmd
01_git_lfs.Rmd
01_git_setup.Rmd
blischak_etal_2016.PDF
citation.bib
graphics
cd
: Change directories. This is like
setwd()
in R. As when we specified paths in R, using two
periods mean “move back a folder”.
cd ../
pwd
/home/dgerard/Dropbox/teaching/stat_413_613/lectures
cd
without specifying a folder to move to,
it will move the working directory to the home directory.cd
pwd
/home/dgerard
cd ./Dropbox/teaching/data_496_696/lectures/01_git
man
: Read the manual of a command. Just like
help()
in R.
man ls
This will open up the man page of ls
. You can scroll
through this page using the up and down arrows. You can exit this page
by typing q
.
This won’t work for Git Bash (for Windows users). Instead, you’ll need to type
ls --help
Exercise: What is your home directory? What files/folders exist in your home directory? Navigate to it and then navigate back to your notes.
Exercise: Where does the following command take you? How does it work?
cd ~/../../..
Exercise: Read the manual page of
ls
. What does the a
flag do? Try it
out!
touch
: Create an empty file.
touch empty_file.txt
more
: Open up a preview of a document. You can exit
the preview by typeing q
.
more 01_basic_bash.Rmd
cp
: Copy a file.
cp 01_basic_bash.Rmd hellobash.Rmd
ls
01_basic_bash.Rmd
01_figs
01_git_branch.Rmd
01_git_github.Rmd
01_git_lfs.Rmd
01_git_setup.Rmd
blischak_etal_2016.PDF
citation.bib
graphics
hellobash.Rmd
mv
: Move/rename a file.
mv hellobash.Rmd goodbyebash.Rmd
ls
01_basic_bash.Rmd
01_figs
01_git_branch.Rmd
01_git_github.Rmd
01_git_lfs.Rmd
01_git_setup.Rmd
blischak_etal_2016.PDF
citation.bib
goodbyebash.Rmd
graphics
rm
: Remove a file.
rm goodbyebash.Rmd
ls
01_basic_bash.Rmd
01_figs
01_git_branch.Rmd
01_git_github.Rmd
01_git_lfs.Rmd
01_git_setup.Rmd
blischak_etal_2016.PDF
citation.bib
graphics
mkdir
: Make a directory/folder.
mkdir tempdir
ls
01_basic_bash.Rmd
01_figs
01_git_branch.Rmd
01_git_github.Rmd
01_git_lfs.Rmd
01_git_setup.Rmd
blischak_etal_2016.PDF
citation.bib
graphics
tempdir
rmdir
: Remove a directory/folder.
rmdir tempdir
ls
01_basic_bash.Rmd
01_figs
01_git_branch.Rmd
01_git_github.Rmd
01_git_lfs.Rmd
01_git_setup.Rmd
blischak_etal_2016.PDF
citation.bib
graphics
grep
Find any string in any file in the current working directory (or subdirectories or the current working directory).
-r
recursive-n
line number-w
whole word only-e
patternpdfgrep
(need to install separately) for searching text
in PDFs.grep -rnw -e "move"
01_basic_bash.Rmd:93: paths in R, using two periods mean "move back a folder".
01_basic_bash.Rmd:100: - If you use `cd` without specifying a folder to move to, it will move the
01_basic_bash.Rmd:108: - OK, I'm going to move us back to the 01_git directory.
01_basic_bash.Rmd:200:grep -rnw -e "move"
01_git_github.Rmd:69:- You can go back to previous versions of your code/text, then move forward to
01_git_github.Rmd:226:- Then move into your new repo
.Rhistory:2:grep -rnwe "move"
grep -rnw -e "mov"
01_basic_bash.Rmd:204:grep -rnw -e "mov"
01_basic_bash.Rmd:208:grep -rn -e "mov"
grep -rn -e "mov"
01_basic_bash.Rmd:93: paths in R, using two periods mean "move back a folder".
01_basic_bash.Rmd:100: - If you use `cd` without specifying a folder to move to, it will move the
01_basic_bash.Rmd:108: - OK, I'm going to move us back to the 01_git directory.
01_basic_bash.Rmd:168:- `rm`: Remove a file.
01_basic_bash.Rmd:182:- `rmdir`: Remove a directory/folder.
01_basic_bash.Rmd:200:grep -rnw -e "move"
01_basic_bash.Rmd:204:grep -rnw -e "mov"
01_basic_bash.Rmd:208:grep -rn -e "mov"
01_basic_bash.Rmd:233:I'll remove that file now
01_git_github.Rmd:69:- You can go back to previous versions of your code/text, then move forward to
01_git_github.Rmd:226:- Then move into your new repo
01_git_github.Rmd:398:- Lines after a "`+`" are being added. Lines after a "`-`" are being removed.
.Rhistory:2:grep -rnwe "move"
.Rhistory:9:conda_remove(envname = "r-reticulate")
.Rhistory:11:conda_remove("bs4")
wget
Non-interactive downloading of data.
Not available for Git Bash for Windows.
-nc
Don’t download new copies if already there.-nd
Put all files in current working directory.-P
Tell where to download the files. Default is current
working directory (.
)-r
Recursive downloading. Download all files in the
directory up to a certain level.l
Determine the level for recursive downloading.E.g. to download the HTML file that contains the Wikipedia list of theological demons, we can go
wget -nc -nd https://en.wikipedia.org/wiki/List_of_theological_demons
--2022-11-16 13:29:02-- https://en.wikipedia.org/wiki/List_of_theological_demons
Resolving en.wikipedia.org (en.wikipedia.org)... 208.80.154.224, 2620:0:861:ed1a::1
Connecting to en.wikipedia.org (en.wikipedia.org)|208.80.154.224|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 79328 (77K) [text/html]
Saving to: ‘List_of_theological_demons’
0K .......... .......... .......... .......... .......... 64% 15.2M 0s
50K .......... .......... ....... 100% 22.6M=0.004s
2022-11-16 13:29:02 (17.2 MB/s) - ‘List_of_theological_demons’ saved [79328/79328]
ls
01_basic_bash.Rmd
01_figs
01_git_branch.Rmd
01_git_github.Rmd
01_git_lfs.Rmd
01_git_setup.Rmd
blischak_etal_2016.PDF
citation.bib
graphics
List_of_theological_demons
I’ll remove that file now
rm List_of_theological_demons
reverse-i-search
ctrl
+r
to get search promptctrl
+r
to cycle through matchesenter
if you want to reuse the match. Hit
ctrl
+c
to exit out of the search.