Skip to content

Instantly share code, notes, and snippets.

@benneic
Last active October 16, 2019 01:38
Show Gist options
  • Save benneic/6a2c353f6a6228cdd5e99272a6879b0d to your computer and use it in GitHub Desktop.
Save benneic/6a2c353f6a6228cdd5e99272a6879b0d to your computer and use it in GitHub Desktop.

Revisions

  1. benneic revised this gist Oct 16, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bash.md
    Original file line number Diff line number Diff line change
    @@ -94,5 +94,5 @@ cat access.log | awk '$11 !~ /google|bing|yahoo|yandex|mywebsite.com/' | awk '{p

    ### list user agents
    ```bash
    cat access.log | awk '{print $12 $13}' | tr -d '"' | sort | uniq -c | sort -rn | head -1000
    cat access.log | awk '{print $14}' | tr -d '"' | sort | uniq -c | sort -rn | head -1000
    ```
  2. benneic revised this gist Oct 16, 2019. 1 changed file with 55 additions and 0 deletions.
    55 changes: 55 additions & 0 deletions bash.md
    Original file line number Diff line number Diff line change
    @@ -40,4 +40,59 @@ ps aux --sort=start_time | grep 'celery worker' | grep 'celery' | awk '{print $2
    ### Delete all git branches on remote which have been merged
    ```bash
    git branch -r --merged | grep -v master | sed 's/origin\///' | xargs -n 1 git push --delete origin
    ```

    ## Nginx (Apache format) log files

    ### top 20 URLs from the last 5000 hits
    ```bash
    tail -5000 access.log | awk '{print $7}' | sort | uniq -c | sort -rn | head -20
    tail -5000 access.log | awk '{freq[$7]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
    ```

    ### top 20 URLS excluding POST data from the last 5000 hits
    ```bash
    tail -5000 access.log | awk -F"[ ?]" '{print $7}' | sort | uniq -c | sort -rn | head -20
    tail -5000 access.log | awk -F"[ ?]" '{freq[$7]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
    ```

    ### top 20 IPs from the last 5000 hits
    ```bash
    tail -5000 access.log | awk '{print $1}' | sort | uniq -c | sort -rn | head -20
    tail -5000 access.log | awk '{freq[$1]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
    ```

    ### top 20 URLs requested from a certain ip from the last 5000 hits
    ```bash
    IP=1.2.3.4
    tail -5000 access.log | grep $IP | awk '{print $7}' | sort | uniq -c | sort -rn | head -20
    tail -5000 access.log | awk -v ip=$IP ' $1 ~ ip {freq[$7]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
    ```

    ### top 20 URLS requested from a certain ip excluding, excluding POST data, from the last 5000 hits
    ```bash
    IP=1.2.3.4
    tail -5000 access.log | fgrep $IP | awk -F "[ ?]" '{print $7}' | sort | uniq -c | sort -rn | head -20
    tail -5000 access.log | awk -F"[ ?]" -v ip=$IP ' $1 ~ ip {freq[$7]++} END {for (x in freq) {print freq[x], x}}' | sort -rn | head -20
    ```

    ### sum of data (in MB) transferred
    ```bash
    cat access.log | awk '{sum+=$10} END {print sum/1048576}'
    ```

    ### top 20 referrers from the last 5000 hits
    ```bash
    tail -5000 access.log | awk '{print $11}' | tr -d '"' | sort | uniq -c | sort -rn | head -20
    tail -5000 access.log | awk '{freq[$11]++} END {for (x in freq) {print freq[x], x}}' | tr -d '"' | sort -rn | head -20
    ```

    ### list of bad referers
    ```bash
    cat access.log | awk '$11 !~ /google|bing|yahoo|yandex|mywebsite.com/' | awk '{print $11}' | tr -d '"' | sort | uniq -c | sort -rn | head -1000
    ```

    ### list user agents
    ```bash
    cat access.log | awk '{print $12 $13}' | tr -d '"' | sort | uniq -c | sort -rn | head -1000
    ```
  3. benneic revised this gist Oct 1, 2019. 1 changed file with 10 additions and 3 deletions.
    13 changes: 10 additions & 3 deletions bash.md
    Original file line number Diff line number Diff line change
    @@ -13,13 +13,20 @@ The pattern is a glob pattern, so ${file%.*} would also work.
    The '%%' operator removes the largest matching suffix, and is interchangeable in the example above.
    As the pattern is fixed, ${file%%.*}.html would turn a.b.htm into a.html though.

    ### Delete files mathing a pattern recursively
    You know those .DS_Store files that people commit to your source tree, that annoys the crap out of me.
    This will delete all that it can find.
    ### Delete files matching a pattern recursively
    #### Them .DS_Store files
    You know those .DS_Store files that someone commited to a source tree instead of the .gitignore? (That annoys the $#!+ out of me). This will delete all that it can find.
    ```sh
    find . -type f -name .DS_Store -exec rm {} +
    ```

    #### Delete files older than X days, minutes etc
    Want to delete old log files after 7 days (`-ctime +7`) or perhaps some csv files uploaded to your ftp after 30 minutes (`-cmin +30`)? Well it is `find` to the rescue again.
    ```sh
    find /home/sftp -type f -name "*.csv" -ctime +2 -exec rm -f {} \;
    ```
    More info on find and time here: https://www.gnu.org/software/findutils/manual/html_mono/find.html#Time

    ## Sed, Awk & Xargs

    ### Want to kill a bunch of processes you dont like?
  4. benneic revised this gist Sep 19, 2019. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion bash.md
    Original file line number Diff line number Diff line change
    @@ -20,12 +20,17 @@ This will delete all that it can find.
    find . -type f -name .DS_Store -exec rm {} +
    ```

    ## Awk
    ## Sed, Awk & Xargs

    ### Want to kill a bunch of processes you dont like?

    Kill those pesky Python Celery process that just seems to go off on their own and never come back.
    Do it by using `grep` to get out what you are looking for from the `ps` command then grab the 2nd 'column' of values using `awk` and pipe it to the `kill` command using `xargs` which will run it for each value that was piped to it.
    ```
    ps aux --sort=start_time | grep 'celery worker' | grep 'celery' | awk '{print $2}' | xargs kill -TERM
    ```

    ### Delete all git branches on remote which have been merged
    ```bash
    git branch -r --merged | grep -v master | sed 's/origin\///' | xargs -n 1 git push --delete origin
    ```
  5. benneic revised this gist Aug 26, 2019. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions bash.md
    Original file line number Diff line number Diff line change
    @@ -18,4 +18,14 @@ You know those .DS_Store files that people commit to your source tree, that anno
    This will delete all that it can find.
    ```sh
    find . -type f -name .DS_Store -exec rm {} +
    ```

    ## Awk

    ### Want to kill a bunch of processes you dont like?

    Kill those pesky Python Celery process that just seems to go off on their own and never come back.
    Do it by using `grep` to get out what you are looking for from the `ps` command then grab the 2nd 'column' of values using `awk` and pipe it to the `kill` command using `xargs` which will run it for each value that was piped to it.
    ```
    ps aux --sort=start_time | grep 'celery worker' | grep 'celery' | awk '{print $2}' | xargs kill -TERM
    ```
  6. benneic revised this gist Aug 14, 2019. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions bash.md
    Original file line number Diff line number Diff line change
    @@ -12,3 +12,10 @@ The one used here, '%', removes the smallest matching suffix from the value of t
    The pattern is a glob pattern, so ${file%.*} would also work.
    The '%%' operator removes the largest matching suffix, and is interchangeable in the example above.
    As the pattern is fixed, ${file%%.*}.html would turn a.b.htm into a.html though.

    ### Delete files mathing a pattern recursively
    You know those .DS_Store files that people commit to your source tree, that annoys the crap out of me.
    This will delete all that it can find.
    ```sh
    find . -type f -name .DS_Store -exec rm {} +
    ```
  7. benneic revised this gist Jul 2, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Amazing-CLI-Archive.md
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    reverse-i-search: A command line history archive (because my memory just aint that good)
    My personal **reverse-i-search**: A command line history lookup (because my memory just aint that good)
  8. benneic revised this gist Jul 2, 2019. 6 changed files with 39 additions and 4 deletions.
    1 change: 1 addition & 0 deletions Amazing-CLI-Archive.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    reverse-i-search: A command line history archive (because my memory just aint that good)
    5 changes: 5 additions & 0 deletions AAA-bash-files.md → bash.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,9 @@
    # Bash

    ## Files

    ### Rename a group of files with one command

    ```sh
    for file in *.png; do mv "$file" "${file%.png}_512x512.png"; done
    ```
    7 changes: 5 additions & 2 deletions exiftool.md
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,15 @@
    # Using Exiftool on the command line cheat sheet
    # Exiftool
    Using Exiftool on the command line cheat sheet

    Install on a Mac with `brew install exiftool`

    ## Shift timestamps

    ### Shift from one point in time to another

    This adds 1 hour to all images in a directory - like say you forgot to adjust your camera for daylight savings or you went on a holiday to another timezone...
    ```

    ```sh
    exiftool -AllDates+=1 dir
    ```
    The AllDates flag shifts the values of DateTimeOriginal, CreateDate and ModifyDate.
    4 changes: 3 additions & 1 deletion image-magick.md → image-magik.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    # Using Image Magick on the command line cheat sheet
    # Image Magick
    Command line cheat sheet

    Install on a Mac with `brew install imagemagick`

    ### Resize one file by a percentage
    25 changes: 25 additions & 0 deletions networking.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    # Networking

    ## Forgot how to find listening ports again?

    ```
    netstat -an | grep -i "listen"
    ```
    List all listening ports and their processes IDs.

    ```
    lsof -iTCP -sTCP:LISTEN
    ```
    This will return every TCP connection with the status LISTEN. This reveals all the open TCP ports on your box. It also lists the processes associated with those open ports. This is a significant upgrade over netstat, which lists PIDs at most.

    ```
    sudo lsof -i -u^$(whoami)
    ```
    Returns all connections not owned by the currently logged-in user. The caret ^ is used for negation. Anything matching the text after the caret will be removed from the results.

    ```
    lsof -nP -iTCP@localhost:513
    ```
    Lists all the TCP connections with the hostname localhost and the port 513. It will also run lsof without connecting names to IP addresses and ports, making the command run noticeably faster.


    1 change: 0 additions & 1 deletion reverse-i-search
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    One day I will write something here.
  9. benneic renamed this gist Jul 2, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  10. benneic created this gist Jul 1, 2019.
    9 changes: 9 additions & 0 deletions bash-files.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    ### Rename a group of files with one command
    ```sh
    for file in *.png; do mv "$file" "${file%.png}_512x512.png"; done
    ```
    Bash has an extensive set of variable expansion options.
    The one used here, '%', removes the smallest matching suffix from the value of the variable.
    The pattern is a glob pattern, so ${file%.*} would also work.
    The '%%' operator removes the largest matching suffix, and is interchangeable in the example above.
    As the pattern is fixed, ${file%%.*}.html would turn a.b.htm into a.html though.
    35 changes: 35 additions & 0 deletions exiftool.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    # Using Exiftool on the command line cheat sheet
    Install on a Mac with `brew install exiftool`

    ## Shift timestamps

    ### Shift from one point in time to another

    This adds 1 hour to all images in a directory - like say you forgot to adjust your camera for daylight savings or you went on a holiday to another timezone...
    ```
    exiftool -AllDates+=1 dir
    ```
    The AllDates flag shifts the values of DateTimeOriginal, CreateDate and ModifyDate.

    ```
    exiftool -AllDates+=1:30 -if '$make eq "Canon"' dir
    ```
    Shifts forward by 1 hour and 30 minutes for all Canon images in a directory.

    ### Shifting by timezone
    Change timezone of all timestamps to Australian Eastern Standard Time
    ```
    ```

    More info on timeshifts here: https://sno.phy.queensu.ca/~phil/exiftool/Shift.html

    ## Geo Coordinates

    ### Add GPS Position
    Add latitude and longitude coordinates (for somewhere in Sydney) to all images in a directory
    ```
    exiftool -XMP:GPSLongitude=151.271305 -XMP:GPSLatitude=-33.891035 -GPSLongitudeRef=East -GPSLatitudeRef=South .
    ```


    12 changes: 12 additions & 0 deletions image-magick.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    # Using Image Magick on the command line cheat sheet
    Install on a Mac with `brew install imagemagick`

    ### Resize one file by a percentage
    ```
    convert airline.png -resize 50% airline_half.png
    ```

    ### Generate new thumbnail files with new suffix in filename
    ```
    convert orig-*.png -thumbnail 20x20 -set filename:fname '%t_20x20' +adjoin '%[filename:fname].png'
    ```
    1 change: 1 addition & 0 deletions reverse-i-search
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    One day I will write something here.