[APACHE] Password Protect a Directory with htaccess

If you like to password protect a directory on your web server, just create a .htaccess file into such a directory and put the following code:

AuthType Basic
AuthName "Restricted Area"
AuthUserFile /path/directory/protect/.htpasswd
require valid-user

Then, generate the corresponding .htpasswd file by executing the following command:

/path/directory/protect$ htpasswd -c .htpasswd admin
New password:
Re-type new password:
Adding password for user admin

Reload your web directory and enter the credentials you’ve just chosen.

[LINUX] aMule instance already running

If you are trying to open aMule but you receive the following error:

Initialising aMule
Checking if there is an instance already running...
There is an instance of aMule already running
Raising current running instance.

then, something went wrong in its last execution. So, you have to first remove the lock file:

~$ rm ~/.aMule/muleLock

If you have found this post useful, please visit the Contribute page

[LINUX] How to Monitor System Performance

You can use the vmstat command to monitor system performance in real-time. vmstat is a tool able to collect stats about system’s memory and processor resource utilization in real time. In order to have an always-active monitoring shell, prepend the watch command to the vmstat command:

~$ watch vmstat -a -S M
Every 2.0s: vmstat -a -S M

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free  inact active   si   so    bi    bo   in   cs us sy id wa st
 0  0      0   2961    268    358    0    0   145    38   75  119  0  0 97  2  0

Furthermore, if you’d like to specifically monitor disk reads/writes, you can execute the following command:

~$ watch vmstat -d
Every 2.0s: vmstat -d

disk- ------------reads------------ ------------writes----------- -----IO------
       total merged sectors      ms  total merged sectors      ms    cur    sec
sr0        0      0       0       0      0      0       0       0      0      0
sda    24616   4031  867336  196140   2424  10626  227728 1087716      0     54

[BASH] Best way to concat strings

There are many ways to concat strings in bash, but only one is the best. Let’s see the several methods:

x="debian"
y="linux"

z=$x$y		# does work		$z is "debianlinux"
z="$x$y"	# does work		$z is still "debianlinux"
z="$x9 $y"	# does not work		$z is just "linux"
z="${x}9 ${y}"	# does work (best)	$z is "debian9 linux"

So, in our opinion, the best way is the last one, always using the following syntax to get a variable value: ${VARIABLE}

[BASH] Get the source directory of a bash script

Often it’s very useful declaring a variable with the absolute path of the bash script. In doing so, you can prepend that variable to other sub-path variables (e.g., a sub-dir “logs”).

But, how can we dynamically get the source directory of a bash script? And why is that better than manually writing the static path? That’s better because of this simple reason: if you move the script or rename one of the parent directory where the script is within, you have to remember to update your code as well. Instead, using the following bash string, the absolute path is automatically updated:

$(dirname "${BASH_SOURCE[0]}")

Let’s see a common example where you can use the above code:

#!/bin/bash
TODAY=$(date +"%Y%m%d")
HOSTNAME=$(hostname)

ABSOLUTE_PATH=$(dirname "${BASH_SOURCE[0]}")
FILE_LOG="${ABSOLUTE_PATH}/logs/test.log"

echo "${TODAY} ${HOSTNAME}: Test QWERTY" >> ${FILE_LOG}

[LINUX] Output of the previous executed command

To simply view the output of the previous executed command, you can run the following echo:

~$ echo "$?"

Indeed, $? returns the output code or message of the last executed command. Below a common example:

~$ touch foobar
~$ rm foobar
~$ echo "$?"
0 ~$ rm foobar
rm: cannot remove 'foobar': No such file or directory
~$ echo "$?"
1

A value of 0 means “Success”, while a value of 1 means “Fail”.

[BASH] Check if a file or dir exists

Run the following code to test a file and dir checking.

#!/bin/bash
FILE="/root/test.txt"
DIRECTORY="/sbin"

# IF FILE EXISTS
if [ -f $FILE ]; then
    echo "File $FILE exists."
fi


# IF FILE DOES NOT EXIST
if [ ! -f $FILE ]; then
    echo "File $FILE does not exist."
fi


# IF DIR EXISTS
if [ -d "$DIRECTORY" ]; then
    echo "Dir $DIRECTORY exists."
fi


# IF DIR DOES NOT EXIST
if [ ! -d "$DIRECTORY" ]; then
    echo "Dir $DIRECTORY does not exist."
fi