Skip to content

Latest commit

 

History

History
256 lines (165 loc) · 7.24 KB

bash.md

File metadata and controls

256 lines (165 loc) · 7.24 KB

A Brief History Lesson

A Unix "shell" is a command-line interpreter that provides a traditional user interface for the Unix operating system and for Unix-like systems.

  • There are a bunch of different shells... Bourne shell, C shell, Korn shell, Z shell, etc.
  • The first major shell was the Bourne Shell, developed in the late 70's.
  • We're going to focus on the "Bourne again shell" or Bash (default shell for OSX and Linux)

Hint: You can install Bash bindings in Windows via Git for Windows

Why Bash?

  • A fully-capable scripting language
  • Vast adoption (approximately 70% of public servers are Unix or Unix-like)
  • Tons of command line features...
    • Tab completion
    • Pipes
    • Aliases
    • Environment variables
    • Startup scripts
    • ...and more!

Essential Commands

In this next part, we'll learn some basic commands that we can use to interact with our Bash shell.

The outline below is provided as a handy reference of the commands we'll cover in class. This tutorial contains alot of additional information if you're confused or just curious.  


pwd - Display your "present working directory".


ls - Display the contents of a directory specified by <path>.

Optional Flags:
Long listing (with details)... ls -l
List all files... ls -a

Optional Arguments:
Apply to files or directories... ls <path>

Hint:
You can use the wildcard character too... ls *.txt


man - Display documentation for a given command. "Man" is short for "manual".

Required Arguments:
The command to display documentation for... man <command>

Hints:
Exit a man page by pressing the 'q' key on your keyboard.


cd - Change to directory specified by <path>

Optional Arguments:
The location to move to... cd <path>

Special Characters:
Move to the parent directory... cd ..
Return to previous working directory... cd -
Root of filesystem... cd /
Your home directory... cd ~ or cd --


open - View directory or file specified by <path>.

Required Arguments:
Directory or file... open <path>

Special Characters:
A dot character refers to the current directory... open .

Note For Windows Users...
Use this command instead... explorer .


mkdir - Make a new directory.

Required Arguments:
The name of the new directory... mkdir <name>

Optional Flags:
Create intermediate directories as required. ... mkdir -p <path>


wget - Download a file.

Required Arguments:
File url... wget <url>

Hint:
Try executing this... wget http://www.gutenberg.org/files/2600/2600-0.txt

Note for Mac Users:
If you don't have wget try this... curl -O http://www.gutenberg.org/files/2600/2600-0.txt


cat - Concatenate and print files

Required Arguments:
The file to concatenate... cat <file>

Hint:
Path multiple paths to concatenate files... cat <file> <file>

Special Characters:
Send output to a new file... cat file1.txt file2.txt > combined.txt
Wildcards... cat *.txt > all-the-files.txt


head - Show the first ten lines of a file.

Required Arguments:
The file to display... head <file>

Optional Flags:
Specify n lines... head -n 25 `


tail - Show the last ten lines of a file.

Required Arguments:
The file to display... tail <file>

Optional Flags:
Specify n lines... tail -n 25 `


mv - Move (or rename) a file or directory

Required Arguments:
The target and destination... mv <target> <destination>


cp - Copy a file or directory

Required Arguments:
The target and destination... cp <target> <destination>

Optional flags:
Copy recursively (directories)... cp -R


rm - Remove files and directories

Required Arguments:
The file or directory to remove... rm <path>

Optional Flags:
Remove recursively (directories)... rm -r


history - Show command history

Special Characters:
Recall previous command... !!
Repeat command in your history... !<linenumber>


vim - Create and edit text files.

Optional Arguments:
The directory or file(s) you want edit... vim <path>


Editing Text Files with Vim

Vim (which stands for Vi-improved) is a heavy-duty text-editor alot of developers use full-time.

It is, admittedly, confusing as hell for new users because it relies solely on keyboard shortcuts to navigate the interface. But it's installed almost everywhere (and is generally the default editor on most Unix-esque systems) so its worth being familiar with.  

Entering Insert Mode

The trickiest part about using vim is understanding that there are multiple "modes". The current mode you're in determines what features are available. For example– if you want to edit an open file, you'll need to enter "insert mode" by pressing the a key or i key on your keyboard.

You can tell that you're in "insert mode" if you see -- INSERT -- displayed in the bottom left corner of your editor window. Once you're in "insert mode" you can type normally and move the cursor around the window with your arrow keys.  

Saving Your Edits

After you make your edits, you'll probably want to save them right? To do this, you first need to exit "insert mode" by pressing the esc key on your keyboard.

Next you issue a couple keystrokes in order to tell Vim you want to save your changes. Type the following characters and hit enter to "write" your changes to disk... :w (that is a colon followed by the letter w).  

Exiting Vim

Closing your open file is alot like saving your edits above. Type the following characters and hit enter to "quit" vim... :q (colon then the letter q).

You can also combine these two actions into one like so... :wq

 

Common Tasks

You can create a new file or open an existing file the same way. Just pass a filename as the first argument to the vim command...

$ vim some-filename

 

Here's list of common keyboard shortcuts, the vast majority of which are not applicable .

i or a - Enter insert mode
v - Enter visual mode
esc - Exit your current mode
 

Ctrl + f - Page down
Ctrl + b - Page up
gg - Go to the top of the file
G - Go to the bottom of the file
0 - (zero) Go to beginning of line
$ - Go to end of line
 

:w - Write changes to a file
:q - Quit vim
:wq - Write and quit
 

cw - Change word and enter insert mode
yy - Copy current line
dd - Delete current line
p - Paste
u - Undo
Ctrl + R - Redo
 

k or - Move cursor up
j or - Move cursor down
l or - Move cursor right
h or - Move cursor left