Quantcast
Channel: Gigarocket Forum - All Forums
Viewing all articles
Browse latest Browse all 1923

Easy accurate timestamps and a few other scripts

$
0
0
Since I recommended this section being created, I figured I'd give out a few tiny snippet gems I use all the time.

Note that these functions/aliases are for Bash; thus, they may need to be altered to meet your needs. The reason why I didn't include guards (e.g., OS/env checks) was because adding them to these scripts will only confuse people who don't know how to program. If, however, someone asks (a) how to implement these checks and incorporate them into some script; and/or (b) how to port a Bash script to, say, Zsh; and/or, © how to convert an inline Perl script to, say, Python, then, I'll be happy to guide them in the right direction. (N.B., Homework questions DO NOT count. Do not ask me how to fix your assignment, as I will report you to the moderators.)


This function will accept a valid IPV4 address (i.e., numbers, do not confuse with domain name) and query ipinfo.io to see where the IP address is located.
Code:
geoip_location() {
    # EXAMPLE: << curl ipinfo.io/23.66.166.151 >>
    echo "";
    echo "Acquiring location of $1 ..."
    echo "";
    echo "[------------------------------------------------------------------------------]";
    echo "[------------------------------------------------------------------------------]";
    curl ipinfo.io/$1
    echo "";
    echo "[------------------------------------------------------------------------------]";
    echo "[------------------------------------------------------------------------------]";
    echo "";
}
An example output of Google.com (to get the IP address, you can issue ping <host>):

Acquiring location of 216.58.193.206 ...

[------------------------------------------------------------------------------]
[------------------------------------------------------------------------------]
{
  "ip": "216.58.193.206",
  "hostname": "lax02s23-in-f206.1e100.net",
  "city": "Mountain View",
  "region": "California",
  "country": "US",
  "loc": "37.4192,-122.0574",
  "org": "AS15169 Google Inc.",
  "postal": "94043"
}
[------------------------------------------------------------------------------]
[------------------------------------------------------------------------------]
 



This little beauty gives a fairly accurate timestamp by using an inline Perl script:
 
Code:
now() {
    perl -e 'use Time::HiRes qw(time); print time';
}

Example: mkdir "Backup folder [`now`]" will produce the directory: <current working directory>/Backup\ folder\ [1476339747.56261]/

This is fairly useful, like, when doing backups. Having the Unix timestamp makes it very easy to tell when the directory was created when you aren't able to see the creation date via the terminal or a GUI (for whatever reason it may be).
 



This inline script, like the one above, performs an equivalent action, though with Python.
 
Code:
alias unixtime='python -c "from time import time as time; print(time())"'

Example: vim "tmp_file [`unixtime`].txt"
This creates the file, "tmp_file [1476340315.181648].txt".
 



This script will recursively search directories and list unique file types (and/or directories) found
Code:
# recursive search and listing of unique file types in a directory
lsuf() {
    if [ -z $1 ]; then
        printf "\nIncorrect use.\n> $0 <PATH>\n";
    else
        find $1 -type f | sed 's/.*\.//' | sort | uniq -c;
    fi
}

Example:
moi $ lsuf .
    1 /README
    1 /xar/ChangeLog
    1 /xar/INSTALL
    1 /xar/LICENSE
    1 /xar/TODO
    1 /xar/configure
    1 /xar/install-sh
    1 1
    1 ac
    26 c
    5 diff
    1 git/HEAD
    1 git/config
    1 git/description
    1 git/index
    1 git/info/exclude
    1 git/logs/HEAD
    1 git/logs/refs/heads/master
    1 git/logs/refs/remotes/origin/HEAD
    1 git/packed-refs
    1 git/refs/heads/master
    1 git/refs/remotes/origin/HEAD
    1 guess
    25 h
    1 idx
    9 in
    1 pack
    1 pbxproj
    3 plist
    8 py
    9 sample
    1 sh
    1 status
    1 sub
    1 txt
    9 xcscheme
    1 xcuserstate
    1 xcworkspacedata
    2 xsl


Enjoy!

Viewing all articles
Browse latest Browse all 1923

Trending Articles