Review of Week 3 Day 3 Learning Objectives

  1. Given a folder structure diagram, a list of ‘cd (path)’ commands and target files, match the paths to the target files.

Given a sample filesystem:

/
|----/usr
|--------/bin
|--------/local
|------------/bin
|----/var
|--------/log
|--------/lib
|----/home
|--------/idbentley
|------------/development
|----------------/appacademy

And knowing:

$ pwd
/home/idbentley

I can navigate to the appacademy directory with the command:

cd development/appacademy
  1. Create, rename, and move folders using unix command line tools.
$ pwd
/home/idbentley/development
$ ls -a
. ..

Knowing that we are in an empty development directory, I want to create a new directory for my project ChewbaccaCat. I know I can use the mkdir tool, so I try

$ mkdir ChewbaccoCat
$ ls -a
. .. ChewbaccoCat

Dern, typos! I can use mv (i.e move) to rename a file or folder, however.

$ mv ChewbaccoCat ChewbaccaCat
$ cd ChewbaccaCat
$ pwd
/home/idbentley/development/ChewbaccaCat
$ git init
  1. Use grep and | to count matches of a pattern in a sample text file and save result to another file.

One option using grep with the -c (count) opt

$ grep -c 'pattern' infile.txt > countResults.txt

Or we can pipe | the output of grep into the word count command wc -l to count the number of matched lines

 $ grep "pattern" infile | wc -l > countResults.txt
  1. Find what -c, -r, and -b flags do in grep by reading the manual.
  1. Identify the difference in two different files using diff.
$ diff file1 file2

Easy as pie

  1. Open and close nano with and without saving a file.

To open nano, we point the nano command at the file we wish to open

$ nano countResults.txt

Nano uses Ctrl + <command> keyboard shortcuts to utilize it’s functions.

We use Ctrl + o to save a file - Nano will prompt you for the buffer name (Nano calls files “buffers” for historical reasons).

To exit Nano without saving, you can use Ctrl + x.

  1. Use ‘curl’ to download a file.
$ curl https://www.google.com/robots.txt

If we want to save the file, we can use the the -o option.

$ curl https://www.google.com/robots.txt -o robit.txt
  1. Read the variables of $PATH.

To read the current value of path, use the echo command

$ echo $PATH

The $PATH variable is split on : characters, resulting in an array of paths, which should be read from left to right. When your OS is looking for a command to run, it will walk through this array, checking each path in turn.

  1. Explain the difference between .bash_profile and .bashrc.

Bash Profile is run whenever we run a login shell.

bashrc is run for every shell

  1. Create a new alias by editing the .bash_profile.

For example:

alias la='ls -a'
alias gl='git log --oneline'
  1. Given a list of common scenarios, identify when it is appropriate and safe to use sudo, and when it is a dangerous mistake.
$ sudo rm <path>

Any use of sudo rm is potentially a dangerous mistake!

$ sudo apt-get install <package>

Install system packages is a safe and necessary use of sudo

$ sudo npm install -g mocha

Installing npm packages with sudo is almost always gonna mess up your env.

  1. Write a shell script that greets a user by their $USER name using echo.

We write our simple script to whoami.sh

#!/bin/zsh
USR=`whoami`
echo "Hello " $USR
  1. Use chmod to make a shell script executable.

Now we want to run whoami.sh

$ ./whoami.sh
zsh: permission denied: ./whoami.sh
$ chmod u+x whoami.sh
$ ./whoami.sh
Hello idbentley