Skip to content

Instantly share code, notes, and snippets.

@winnab
Created December 10, 2014 20:58
Show Gist options
  • Save winnab/a50c829cc15fa54a1ee6 to your computer and use it in GitHub Desktop.
Save winnab/a50c829cc15fa54a1ee6 to your computer and use it in GitHub Desktop.

Command line basics

Your hard drive is a folder. Learn to move around it using the Terminal.

Reading

Open Terminal

  • From Spotlight: type cmd + space and then start typing Terminal in the input. When Terminal is highlighted, hit enter.
  • From Finder: go to Applications > Utilities and double click Terminal.app

Misc Commands

$ .  # represents the current directory
$ ..  # represents the parent directory
$ pwd # gives the current directory (where you currently are)
$ ls  # lists the items in the current directory
$ ls -l # lists items with more detail
$ ls -a  # lists all files in a directory, including hidden files
$ cd # moves you into your home directory. The home directory is the current user directory, it is represented in shell by the character ~   
$ cd foo # moves you into your “foo” directory if it exists. 
$ history # lists your entire command history
$ grep # global regular expression parser
$ history | grep "test"
# example using grep...it finds words in the output of another command (history)
$ mkdir foo # creates the directory foo
$ touch foo.txt  # creates the file foo.txt
$ echo "string" > foo.txt  # saves "string" > in foo.txt
$ mv test_folderpwd another_folder # moves the test_folder to directory another folder

$ which # shows where the specified command is located $ rm foo # removes the “foo” file if it exists $ rm -r foo # removes the “foo” folder if it exists; -r stands for “recursive” $ ps aux # shows all the processes currently running on your computer, along with their unique IDs $ kill -9 # will end the process associated with the specified ID $ echo “something” will print “something” in the console $ history will print a list of previous shell commands $ cat filename shows the file contents in the console;

  • can be used with grep - example: $ cat filename | grep “word” will show you the lines with the word “word” $ whoami will give you your username… just in case you forgot it! $ top provides an ongoing look at processor activity in real time

Shortcuts

keypress action

|Ctrl + A| Go to the beginning of the line you are currently typing on |Ctrl + E| Go to the end of the line you are currently typing on |Ctrl + L| Clears the Screen, similar to the clear command |Ctrl + U| Clears the line before the cursor position. If you are at the end of the line, clears the entire line. |Ctrl + K| Clear the line after the cursor |Ctrl + H| Same as backspace |Ctrl + R| Let’s you search through previously used commands |Ctrl + C| Kill whatever you are running; also, exit from irb |Ctrl + D| Exit the current shell |Ctrl + Z| Puts whatever you are running into a suspended background process. fg restores it. |Ctrl + W| Delete the word before the cursor |Ctrl + T| Swap the last two characters before the cursor |Esc + T | Swap the last two words before the cursor |Alt + left cursor key | Move cursor forward one word on the current line |Alt + right cursor key | Move cursor backward one word on the current line |Tab | Auto-complete files and folder names

Aliases

You're gonna use a lot of repetitive commands , ie: cd into/my/rails/applications/folder. In order to avoid having to always rewrite the same command, you can use something called “aliases”. Aliases are a mask to a command; you can choose a command, and you can assign it an alias.

In order to do that:

  • open the .zshrc file in subl → $ subl ~/.zshrc
  • Example: let's say we want to create alias for ls -la, named “listlong”; on a new line , type alias listlong=”ls -la”, save the file and go back to the terminal
  • we need to reload the zshrc file, because the command line need to register the alias. To do that: paste this command in the terminal → $ source ~/.zshrc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment