Bash/Linux/OSX

Linux & OSX: Get file encoding

Posted by Kevin Kivi on

Sometime you need to know what’s your file encoding? Is it UTF-8, ISO 8859-1, ASCII or Windows 1252? You can find this out by using the file Unix command.

Linux: file -i <filename>

Mac OSX: file -I <filename>

Example usage:

username@server ~$ file -i somefile.php
somefile.php: text/x-php; charset=us-ascii
username@server ~$ file -i myutf8file.txt
myutf8file.txt: text/plain; charset=utf-8
username@server ~$ file -i username.tar.bz2
username.tar.bz2: application/x-bzip2; charset=binary
username@server ~$

If you want only the encoding. You can do file -i filename.txt | sed "s/.*charset=\(.*\)/\1/" E.g.

username@server ~$ file -i myutf8file.txt | sed "s/.*charset=\(.*\)/\1/"
utf-8
Git

GIT: Get remote repository URL

Posted by Kevin Kivi on

So you need the remote repository URL. Maybe because you are trying to git add remote.
You can type the below text to get the full info (needs to be done inside the folder you are working in):

git remote show origin

OR

git config --get remote.origin.url

After you know this, if you want you can create a new remote branch

git add remote <name you want for your new branch> <URL you just got>
git add remote newbranch git@hostedgit.yourdomain.com:name/project.git
PHP

PHP: Curl vs file_get_contents benchmark

Posted by Kevin Kivi on

I benchmarked curl vs file_get_contents in getting getting headers only and returning the HTTP Status Code.

Here are the results:

kevinkivi@server:~/my/secret/directory$ php curlvsfgctest.php
Testing curl speed

Domain: http://google.com
Status: 301
Domain: http://yahoo.com
Status: 301
Domain: http://nytimes.com
Status: 301
Domain: http://theguardian.com
Status: 301
Domain: http://wikipedia.org
Status: 301

Curl speed was 0.35739207267761


Testing file_get_contents speed

Domain: http://google.com
Status: HTTP/1.0 301 Moved Permanently
Domain: http://yahoo.com
Status: HTTP/1.0 301 Moved Permanently
Domain: http://nytimes.com
Status: HTTP/1.1 301 Moved Permanently
Domain: http://theguardian.com
Status: HTTP/1.1 301
Domain: http://wikipedia.org
Status: HTTP/1.1 301 TLS Redirect

file_get_contents was 1.7153549194336

Below is the source code:

Closing statement: Curl is almost 5 times faster. Use Curl unless you cant.

Javascript

Javascript: Cronometer ratio changer

Posted by Kevin Kivi on

I created a script (with the help of my wife) which can change the ratio of foods.
E.g.To make new food in Cronometer such as date paste, export “Dates” JSON from Cronometer.
Then import the file. If the date paste is 15% water and 85% dates, then make the multiplier 0.85 and name it as you want.
Click “Result” below to use the script.

Command-line/Bash/Linux

cPanel: Listing all domains

Posted by Kevin Kivi on

I made a bash script listing all main domains and addon domains (for certain user by username or domain or for all users) in cPanel.

Usage: lsdom [OPTION] [INPUT]
Example: lsdom [cPanel username]
Lists domains for certain user by username or domain or for all users

Options:
  -d [domain]      Displays all domains of the user of the input domain.
  -a, --all        Lists all domains.
  -v, --version    Displays version.
  -h, --help       This help page.

Ubuntu/Desktop/Camera

Ubuntu: Loading Photos from Nikon D3300 DSLR Camera

Posted by Kevin Kivi on

I had some trouble loading photos from my Nikon D3300 camera. I had the USB cable, which came with and connected my PC to it. The camera was on.

I use Xubuntu. My file manager is Thunar. The camera would appear in the file manager. Whenever I would try to browse the files in the camera, Thunar would freeze. gvfsd-ghoto2 would use up a huge chunk of the CPU. I would then have to close Thunar and try again. This happened everytime. If you are experiencing the same problems. Mys suggestion is to not use Thunar, but to download gtkam. It’s a GUI for gphoto2. You can of course simply use the gphoto2 CLI if you want.

Photo by Javler Martínez

How to install gtkam:

sudo apt-get update
sudo apt-get install gtkam

This solved all my freezing issues. You can of course simply use and SD Card reader if you happen to have one.

Command-line/Bash/Linux

cPanel: Listing all non-self-signed certificates

Posted by Kevin Kivi on

I made a bash script listing all non-self-signed certificates (for certain user by username or domain or for all users) in cPanel.

Usage: lrcert [OPTION] [INPUT]
Example: lrcert [cPanel username]

Options:
  -d [domain]      Displays all certificates of the owner of the domain.
  -a, --all        Lists all certificates of all cPanel users
  -v, --version    Displays version.
  -h, --help       This help page.

GitHub: https://github.com/nake89/lrcert/

Command-line/Bash/Linux/Terminal

Echoing multiline in linux terminal

Posted by Kevin Kivi on

There are at least three fun ways to echo multiline to a file. We are going to look at doing the output twice, the heredoc -method and writing multiline using double quotes.

1. Output twice
I think this is the simplest and most intuitive method if you are familiar with linux output redirection.

user@server:~/projects/blog_content$ echo "This file is" >> multiline.txt
user@server:~/projects/blog_content$ echo "multiline" >> multiline.txt
user@server:~/projects/blog_content$ cat multiline.txt
This file is
multiline

2. Heredoc -method
You can replace EOF with your choice of characters. It denotes the ending of your input.

user@server:~/projects/blog_content$ cat <<EOF >> sorcery.txt
> This is
> SORCERY!
> EOF
user@server:~/projects/blog_content$ cat sorcery.txt
This is
SORCERY!

3. Write multiline
Leaving the double quote open you can press enter and start a new line. This is my favorite method.

user@server:~/projects/blog_content$ echo "what is this
> magic" > magic.txt
user@server:~/projects/blog_content$ cat magic.txt
what is this
magic
Code/PHP

PHP: Namecheap.com API Class

Posted by Kevin Kivi on

I built a simple to use class in PHP for the namecheap.com API. It supports all of Namecheap’s API methods. I released the code on GitHub.

Example of usage:

<?php
require 'namecheap.class.php';
$username = 'YOUR USERNAME';
$apiKey = 'YOUR API KEY';
$clientIp = 'IP WHERE THIS YOUR SCRIPT IS HOSTED';
$namecheap = new Namecheap ($username, $apiKey, $clientIp) ;
$data["Command"] = "namecheap.ssl.getList";
$returned = $namecheap->request($data);
print_r($returned)
 ?>

GitHub release: https://github.com/nake89/namecheap

Command-line/Bash

End Bash Script Loop

Posted by Kevin Kivi on

Sometimes when bash scripting you might want the ability to cancel your script’s loop with CTRL-C. Below is an exampl simple script which loops through a file of domains separated by line break and it digs the A record (IP address) of the domain. Read further to learn how to force quit this script.

#!/bin/bash
while read p; do
    dig $p A +short
done <listofdomains.txt

If your list of domains is large your and you want to quit this script, you cant. Pressing CTRL-C will not work. You need to add trap "echo Script ended; exit;" SIGINT SIGTERM to the beginning of your script. E.g.

#!/bin/bash
trap "echo Script ended; exit;" SIGINT SIGTERM
while read p; do
    dig $p A +short
done <listofdomains.txt

Pressing CTRL-C will now print “Script ended” to your terminal and exit the script.