Basic Bash

Author

David Gerard

Published

June 4, 2025

Learning Objectives

The Command Line

  • 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:

    • Moving around your file system.
    • Running git commands.
  • Other words for command line: shell, terminal, command line interface (cli), and console.

    • These terms are technically slightly different.
      • shell: a program that takes commands from keyboard and sends them to the operating system.
      • terminal: a program that opens up a window so that you can interact with the shell.
      • console: nowadays is mostly just a synonym for a terminal, but historically meant something like the keyboard and a screen.
      • command line interface: Any format where you interact with a computer by commands, rather than by graphics.
      • command line: Short for command line interface, or the space where the commands are entered.
  • 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.

    • R: f(x, arg = 1)
    • Bash: f x --arg=1
    • There are also arguments that are “flags” where its presence alters the behavior of the command. E.g., f x -g would incorporate the g flag and have a different behavior than f x which does not have the g flag.
    • Typically, two dashes are for longer named commands and one dash is for shorter (one-letter) commands, like --arg versus -a.
  • 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:

      Git Bash for Windows

    • Mac: On your Mac, do one of the following:

      • Type “Terminal” in the search field, then click Terminal.

      • In the Finder Mac Finder Icon, open the /Applications/Utilities folder, then double-click Terminal.

      • It should look like this:

        Zsh for Mac

    • 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:

        Bash for Ubuntu

  • 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”.

Useful Commands:

  • pwd: Print working directory. Show the current working directory. This is like getwd() in R.

    pwd
    /Users/dgerard/Library/CloudStorage/Dropbox/teaching/stat_413_613/lectures/01_git
  • ls: List the current files and folders in a directory.

    ls
    01_basic_bash.qmd
    01_basic_bash.rmarkdown
    01_figs
    01_git_github.qmd
    01_git_lfs.qmd
    01_git_setup.qmd
    blischak_etal_2016.PDF
    citation.bib
  • 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
    /Users/dgerard/Library/CloudStorage/Dropbox/teaching/stat_413_613/lectures
    • If you use cd without specifying a folder to move to, it will move the working directory to the home directory.

      cd
      pwd
      /Users/dgerard
    • OK, I’m going to move us back to the 01_git directory.

      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!

Other commands.

  • touch: Create an empty file.

    touch empty_file.txt
  • more: Open up a preview of a document. You can exit the preview by typing q.

    more 01_basic_bash.Rmd
  • cp: Copy a file.

    cp 01_basic_bash.Rmd hellobash.Rmd
    ls
    cp: 01_basic_bash.Rmd: No such file or directory
    01_basic_bash.qmd
    01_basic_bash.rmarkdown
    01_figs
    01_git_github.qmd
    01_git_lfs.qmd
    01_git_setup.qmd
    blischak_etal_2016.PDF
    citation.bib
  • mv: Move/rename a file.

    mv hellobash.Rmd goodbyebash.Rmd
    ls
    mv: rename hellobash.Rmd to goodbyebash.Rmd: No such file or directory
    01_basic_bash.qmd
    01_basic_bash.rmarkdown
    01_figs
    01_git_github.qmd
    01_git_lfs.qmd
    01_git_setup.qmd
    blischak_etal_2016.PDF
    citation.bib
  • rm: Remove a file.

    rm goodbyebash.Rmd
    ls
    rm: goodbyebash.Rmd: No such file or directory
    01_basic_bash.qmd
    01_basic_bash.rmarkdown
    01_figs
    01_git_github.qmd
    01_git_lfs.qmd
    01_git_setup.qmd
    blischak_etal_2016.PDF
    citation.bib
  • mkdir: Make a directory/folder.

    mkdir tempdir
    ls
    01_basic_bash.qmd
    01_basic_bash.rmarkdown
    01_figs
    01_git_github.qmd
    01_git_lfs.qmd
    01_git_setup.qmd
    blischak_etal_2016.PDF
    citation.bib
    tempdir
  • rmdir: Remove a directory/folder.

    rmdir tempdir
    ls
    01_basic_bash.qmd
    01_basic_bash.rmarkdown
    01_figs
    01_git_github.qmd
    01_git_lfs.qmd
    01_git_setup.qmd
    blischak_etal_2016.PDF
    citation.bib

String search with 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 pattern
  • pdfgrep (need to install separately) for searching text in PDFs.
grep -rnw -e "move"
./.Rhistory:2:grep -rnwe "move"
./01_basic_bash.qmd:104:  paths in R, using two periods mean "move back a folder".
./01_basic_bash.qmd:109:    -   If you use `cd` without specifying a folder to move to, it will move the
./01_basic_bash.qmd:115:    -   OK, I'm going to move us back to the 01_git directory.
./01_basic_bash.qmd:203:grep -rnw -e "move"
./01_basic_bash.rmarkdown:104:  paths in R, using two periods mean "move back a folder".
./01_basic_bash.rmarkdown:109:    -   If you use `cd` without specifying a folder to move to, it will move the
./01_basic_bash.rmarkdown:115:    -   OK, I'm going to move us back to the 01_git directory.
./01_basic_bash.rmarkdown:203:grep -rnw -e "move"
./01_git_github.qmd:64:- You can go back to previous versions of your code/text, then move forward to
./01_git_github.qmd:221:-   Then move into your new repo
grep -rnw -e "mov"
./01_basic_bash.qmd:207:grep -rnw -e "mov"
./01_basic_bash.qmd:211:grep -rn -e "mov"
./01_basic_bash.rmarkdown:207:grep -rnw -e "mov"
./01_basic_bash.rmarkdown:211:grep -rn -e "mov"
grep -rn -e "mov"
./.Rhistory:2:grep -rnwe "move"
./.Rhistory:9:conda_remove(envname = "r-reticulate")
./.Rhistory:11:conda_remove("bs4")
./01_basic_bash.qmd:104:  paths in R, using two periods mean "move back a folder".
./01_basic_bash.qmd:109:    -   If you use `cd` without specifying a folder to move to, it will move the
./01_basic_bash.qmd:115:    -   OK, I'm going to move us back to the 01_git directory.
./01_basic_bash.qmd:171:-   `rm`: Remove a file.
./01_basic_bash.qmd:185:-   `rmdir`: Remove a directory/folder.
./01_basic_bash.qmd:203:grep -rnw -e "move"
./01_basic_bash.qmd:207:grep -rnw -e "mov"
./01_basic_bash.qmd:211:grep -rn -e "mov"
./01_basic_bash.qmd:236:I'll remove that file now
./01_basic_bash.rmarkdown:104:  paths in R, using two periods mean "move back a folder".
./01_basic_bash.rmarkdown:109:    -   If you use `cd` without specifying a folder to move to, it will move the
./01_basic_bash.rmarkdown:115:    -   OK, I'm going to move us back to the 01_git directory.
./01_basic_bash.rmarkdown:171:-   `rm`: Remove a file.
./01_basic_bash.rmarkdown:185:-   `rmdir`: Remove a directory/folder.
./01_basic_bash.rmarkdown:203:grep -rnw -e "move"
./01_basic_bash.rmarkdown:207:grep -rnw -e "mov"
./01_basic_bash.rmarkdown:211:grep -rn -e "mov"
./01_basic_bash.rmarkdown:236:I'll remove that file now
./01_git_github.qmd:64:- You can go back to previous versions of your code/text, then move forward to
./01_git_github.qmd:221:-   Then move into your new repo
./01_git_github.qmd:394:- Lines after a "`+`" are being added. Lines after a "`-`" are being removed.

Download data with 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
--2025-06-04 14:06:24--  https://en.wikipedia.org/wiki/List_of_theological_demons
Resolving en.wikipedia.org (en.wikipedia.org)... 208.80.154.224
Connecting to en.wikipedia.org (en.wikipedia.org)|208.80.154.224|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 105005 (103K) [text/html]
Saving to: ‘List_of_theological_demons’

     0K .......... .......... .......... .......... .......... 48% 1.21M 0s
    50K .......... .......... .......... .......... .......... 97% 4.03M 0s
   100K ..                                                    100% 50.9K=0.05s

2025-06-04 14:06:24 (1.91 MB/s) - ‘List_of_theological_demons’ saved [105005/105005]
ls
01_basic_bash.qmd
01_basic_bash.rmarkdown
01_figs
01_git_github.qmd
01_git_lfs.qmd
01_git_setup.qmd
blischak_etal_2016.PDF
citation.bib
List_of_theological_demons

I’ll remove that file now

rm List_of_theological_demons

Exercises

Execute these commands in this order:

  1. Create a file called “foo.txt”
  2. Rename “foo.txt” to “bar.txt”
  3. Copy “bar.txt” to “foobar.txt”
  4. Create a new directory called “newdir”
  5. Move “foobar.txt” to “newdir”
  6. Delete “bar.txt”
  7. Change the working directory to “newdir”
  8. Delete “foobar.txt”
  9. Change the working directory up one level.
  10. Delete the “newdir” directory.