Skip to content

Instantly share code, notes, and snippets.

@s-taylor
Last active August 29, 2015 13:56
Show Gist options
  • Save s-taylor/9179126 to your computer and use it in GitHub Desktop.
Save s-taylor/9179126 to your computer and use it in GitHub Desktop.
Command Line (Terminal) Notes

Current as at 18-MAR-2014

#Command Line Crash Course

##Command Listing

  • pwd - print working directory
  • hostname - my computer's network name
  • mkdir - make directory
  • cd - change directory
  • ls - list directory
  • rmdir - remove directory
  • pushd - push directory
  • popd - pop directory
  • cp - copy a file or directory
  • mv - move a file or directory
  • less - page through a file
  • cat - print the whole file
  • xargs - execute arguments
  • find - find files
  • grep - find things inside files
  • man - read a manual page
  • apropos - find what man page is appropriate
  • env - look at your environment
  • echo - print some arguments
  • export - export/set a new environment variable
  • exit - exit the shell
  • sudo - DANGER! become super user root DANGER!
  • chmod - change permission modifiers
  • chown - change ownership

##My Notes:-

  • To create a directory with spaces use quotations
    mkdir “have fun”

  • Moving back a directory you must use cd.. and not cd..
    cd ..

  • You can move back multiple directories at a time using...
    cd ../../.. (to move back 3 directories)

  • You can change multiple directories at a time using...
    cd temp/stuff/things

  • Move back to root using cd/
    cd /s

  • List subdirectories
    ls -R
    note: this is case sensitive!

  • List details
    ls -l

  • Remove directory
    rmdir [directoryname]

  • pushd and popd
    pushd - this essentially changes to a new directory AND stores your current directory in the stack
    popd - reverts to the directory in the stack AND remove the directory from the stack
    e.g. where your current directory is /users/nizmox/temp and you type "pushd i/like/icecream" this will change to the directory /users/nizmox/temp/i/like/icecream. If you then type "popd" this will then take you back to /users/nizmox/temp

  • Make a directory and subdirectory at the same time
    mkdir -p temp/awesome/stuff

  • Create a file
    touch <filename.ext>
    touch also allows you to re-save an existing file and updated the modified date.

  • List files in a subdirectory
    ls /subdirectory

  • Copy file cp /
    It is recommended to put a / at the end of the command so that if it is not a true directory you'll receive an error message

  • Copy file to new name in the same folder
    cp <filename.ext>

  • Move file / folder
    mv <filename / foldername> <desired new filename / desired location>
    e.g.
    mv mytext.txt newtext.txt
    mv mytext.txt myfolder/

  • Return to previous file path
    cd -

  • Open finder window at current path
    open .

  • Less / More (same thing)
    This allows you to view contents of a text file in the terminal
    less <filename.ext>
    You can then use down arrow to scroll or space to scroll by window

  • Cat
    This writes one or more text files to the terminal by concatenating the contents
    cat <filename.ext> <filename2.ext> <filename3.ext> etc...

  • Remove File (delete) <filename.ext>
    Useful switches
    -r = remove directories, recursively
    -f = force, no prompts
    e.g. to remove a folder and all its contents
    rm -rf [folder_name/]

  • Wild card searching
    Use * in your ls search
    e.g. ls *.txt

  • Searching for files
    find [path] -name "filename" -print
    Note: if you want to search the current folder you should use
    find . -name "file.ext" -print
    You can also combine this with | less to allow you to scroll the output

  • Opening files
    open filename.ext

  • View the Manual for a Command
    man commandname
    e.g. man ls

  • List hidden files
    ls -a
    note: This will display the .git folder

##REDIRECTION

  • $|$ = Pass Output of Command to next Command
    The | takes the output from the command on the left, and "pipes" it to the command on the right. In line 1 you see me do that.

  • $&lt;$ = Input a text file (on the right) contents to a Command (on the left)
    The < will take and send the input from the file on the right to the program on the left. You see me do that in line 2. This does not work in PowerShell. $&gt;$

  • $&gt;$ = The > takes the output of the command on the left, then writes it to the file on the right.

  • $&gt;&gt;$ = The >> takes the output of the command on the left, then appends it to the file on the right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment