Category Archives

6 Articles

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
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.

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
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.

Guides/Bash

How to Search Contents of File in Linux

Posted by Kevin Kivi on

This is something you need to do often for one reason or another. Maybe you have a bunch of text files, which have been named horribly and you have no idea which file has the thing you are looking for, but you happen to remember a word in that file. Or maybe you need to find which file contains a certain variable to find the root cause of an error you are experiencing in your script. What ever the reason may be, this command lets you search a directory recursively (meaning all the directories in it as well) to find your file(s).

Below is the command.

grep -rnw '/give/your/path/' -e "pattern"

The command-line options are as the following:
-r, Recursive
-n, Gives you the line number
-w, Searches the whole word
-e, Searches the pattern you tell it.

Source: http://stackoverflow.com/questions/16956810/how-to-find-all-files-containing-specific-text-on-linux