Unix Commands Cheatsheet

Just taking notes from the ‘Learn Enough Command Line to be Dangerous’. It’s helpful and also fun to read.

  • Basics

    Command Description Example
    echo Print string to screen $ echo hello
    man Display manual page for command $ man echo
    ⌃C Get out of trouble $ tail^C
    ⌃A Move to beginning of line
    ⌃E Move to end of line
    ⌃U Delete to beginning of line
    Option-click Move cursor to location clicked
    Up & down arrow Scroll through previous commands
    clear or ⌃L Clear screen $ clear
    exit or ⌃D Exit terminal $ exit
  • Manipulation files

    Command Description Example
    > Redirect output to filename $ echo foo > foo.txt
    >> Append output to filename $ echo bar >> foo.txt
    cat <file> Print contents of file to screen $ cat hello.txt
    diff <f1> <f2> Diff files 1 & 2 $ diff foo.txt bar.txt
    ls List directory or file $ ls hello.txt
    ls -l List long form $ ls -l hello.txt
    ls -rtl Long by reverse modification time $ ls -rtl
    ls -a List all (including hidden) $ ls -a
    touch <file> Create an empty file $ touch foo
    mv <old> <new> Rename (move) from old to new $ mv foo bar
    cp <old> <new> Copy old to new $ cp foo bar
    rm <file> Remove (delete) file $ rm foo
    rm -f <file> Force-remove file $ rm -f bar
  • Inspecting files

    Command Description Example
    curl Interact with URLs $ curl -O example.com
    which Locate a program on the path $ which curl
    head <file> Display first part of file $ head foo
    tail <file> Display last part of file $ tail bar
    wc <file> Count lines, words, bytes $ wc foo
    cmd1 | cmd2 Pipe cmd1 to cmd2 $ head foo | wc
    ping <url> Ping a server URL $ ping google.com
    less <file> View file contents interactively $ less foo
    grep <string> <file> Find string in file $ grep foo bar.txt
    grep -i <string> <file> Find case-insensitively $ grep -i foo bar.txt
    ps Show processes $ ps aux
    top Show processes (sorted) $ top
    kill -<level> <pid> Kill a process $ kill -15 24601
    pkill -<level> -f <name> Kill matching processes $ pkill -15 -f spring
  • Directories

    Command Description Example
    mkdir <name> Make directory with name $ mkdir foo
    pwd Print working directory $ pwd
    cd <dir> Change to <dir> $ cd foo/
    cd ~/<dir> cd relative to home $ cd ~/foo/
    cd Change to home directory $ cd
    cd - Change to previous directory $ cd && pwd && cd -
    . The current directory $ cp ~/foo.txt .
    .. One directory up $ cd ..
    find Find files & directories $ find . -name foo*.*
    cp -r <old> <new> Copy recursively $ cp -r ~/foo .
    rmdir <dir> Remove (empty) dir $ rmdir foo/
    rm -rf <dir> Remove dir & contents $ rm -rf foo/
    grep -ri <string> <dir> Grep recursively (case-insensitive) $ grep -ri foo bar/

Tutorial of Using Google Client Libraries for BigQuery Data Streaming in PHP

Google BigQuery is good alternative as a big data warehouse. According to Google, it’s suitable for analytics / transaction data store. If you are still unsure about which platform to use for your data warehousing, here are some comparisons:

Once you decided that BigQuery is your choice, in most cases, you will use its API to automate your data store process. This tutorial intends to give you a quick start of using BigQuery API in PHP. Following the steps you would be able to create tables and insert data into BigQuery.

Continue reading

A Complete Example of Using Google Analytics Reporting API V4 in PHP to Get Custom Report

In the Google Analytics web console, user can create custom reports by specifying dimensions and metrics. There are many reasons you may want to use the Google Analytics Reporting API to programmatically create custom reports. For example,

  1. There are too many entries in the generated custom reports. The web console allows you to select only 5000 rows at a time and export.
  2. You want to combine more dimensions in a report. The web console allows you to add at most 5 dimensions, while the API allows 7.
  3. You want to automate the data exporting process. Say you would like to have a report automatically generated every day and stores in your database.

This example supposes that you have the following prepared:

  1. Knowledge about Google Analytics custom report, dimensions, metrics, etc.
  2. Have the Google API client library installed. If not, please refer to this document.
  3. Set up OAuth 2.0 for authorization. If not, please refer to my previous post.

Continue reading

Set Up Service Account for Google Analytics Reporting API – a PHP Example

Google Analytics Reporting API V4 requires using OAuth 2.0 for authorization. There are basically 2 ways to work with it, based on the type of your application and your needs.

  1. Client-side authorization. This is when you are building a function to allow users to access their own Analytics data through your web application. When a user gets to the function for the first time, it redirects user to a sign-in page (the same page as you sign into your Google account) to get a token. The token lasts for some time and after that it requires user to re-login.
  2. Service account authorization. This is when you are actually using your own analytics data, and especially when you want it to be automatic or offline. An example is that you want to pull data from Google Analytics, say once a day, and store it in your data warehouse. Using a service account allows you to set up a cron event so that it runs automatically every day.

Continue reading