Getting Comfortable on the Command Line

Learning objectives

  • The structure of bash commands
  • Referencing directories and files
  • Reading text files
  • Moving and copying files
  • Server Files
  • Writing files

bash Commands

  • The general structure: <command> <flags + flag args> <command args>
    • command: What to do
    • flags + flag args: Options for the command and options for those options
    • command args: arguments to pass to the command

Listing files in the current directory

We want to list files in the specified directory. The bash command to list files is ls.

R

  • command: list.files()
  • flags + flag args: all.files = TRUE
  • command args: ".github"
list.files(".github", all.files = TRUE)

bash

  • command: ls
  • flags + flag args: -a
  • command args: .github
ls -a .github

Long Commands

  • Long commands are split onto multiple lines with a trailing backslash
this is a \
  really really \
  long command

Directories and Files

Where am I? Reference
Where was I born? /
What town do I live in?
Where do I live? ~
Where am I now? .
Where was I just before where I am now? ..

*There’s no easy way to access your current mount point

What if you were a file?

Let’s say you are this file: /r4ds/do4ds/cohort1/chapter8.Rmd

File System Root Drive Home Folder Folder (Your Room) File
/ r4ds do4ds cohort1 chapter8.Rmd

Revisiting the “Where am I? table”

Where am I? Reference
Where was I born? /
What town do I live in? r4ds
Where do I live? do4ds
Where am I now? cohort1
Where was I just before where I am now? do4ds

What if you have a sudden urge to see the world?

The bash command to change directories is cd . In this case, you start at /r4ds/do4ds/cohort1 which is the directory for your (chapter8.Rmd) bedroom.

Quest Directions
Visiting your cousin in cohort2 cd ../cohort2
Visiting your second cousin in the advr/cohort1 bookclub cd ../../advr/cohort1
You’ve gotten lost and don’t know where you are and need directions to your second cousin in the advr/cohort1 bookclub cd /r4ds/advr/cohort1
Stopping by the hospital where you were born because they have great grilled cheese sandwiches in the cafeteria cd /
Going back to your room cd ~/cohort1

Any path with .. is a relative path and only works from your current directory.

Reading Text Files

Let’s take a look at pr_check.yml in our .github/workflows folder:

cat .github/workflows/pr_check.yml

The Pipe

  • Linux offers a pipe, |, similar to that of R and other functional languages
    • take the output of this | and give it to this as an input

To find the workflow branch:

cat .github/workflows/pr_check.yml | grep branches

To show just the first six items in our current folder:

ls -a | head -n 6

Notice that bash returns the current folder and the parent folder as items.

Moving and Copying Files

Let’s say you are this file: /r4ds/do4ds/cohort1/chapter8.Rmd

File System Root Drive Home Folder Folder (Your Room) File
/ r4ds do4ds cohort1 chapter8.Rmd

What if you do some remodeling at home?

The rm command will remove something, with the options to do so recursively (-r) or to force it (-f). You can also copy (cp), move (mv), or make or remove directories (mkdir and rmdir).

Construction Phase Command
Move everything out of the kitchen into a spare room mv ~/kitchen/* ~/spare_room
Remove the kitchen appliances rm ~/kitchen/stove.app ~/kitchen/fridge.app
Demolish the kitchen rmdir ~/kitchen
Make a new kitchen mkdir ~/kitchen
Move everything into the new kitchen mv ~/spare_room/* ~/kitchen

You mv or cp from_here to_here

Server Files

  • Moving things as one big file has much better throughput than many small files
    • Imagine sending someone a care package. You could send them individual items or one big box. The one big box is much easier to manage.
  • tar packs and unpacks tarball files
    • Pack (create): tar -czf <archive name> <file(s)>
    • Unpack (extract): tar -xfv <archive name>
  • scp stands for “secure copy” and is a combination of ssh and cp

Writing Files

With the Command Line

  • touch creates a file
  • > is similar to pipe, but it will write to a file
  • >> is also similar to pipe, but it wall append to a file
touch file.txt
echo "this goes in the file" > file.txt
echo "this also goes in the file" >> file.txt

With Command Line Text Editors

  • There are several text editors such as vi, vim, and nano that work directly in the command line
    • Some are easier to use than others

To exit vim: 1. Hit the escape key on your keyboard 2. Type one of these, making sure to include the : - To write AND quit: :wq - To quit without having made changes: :q - To quit and not save changes: :q!

It’s okay if you don’t remember.

Memes About Exiting Vim

Meeting Videos

Cohort 1