Lock a file on Linux

Here is how to lock a file from write/delete in a dirty way .
this trick will work this file system ext2-4 XFS, ReiserFS, JFS and OCFS2.
and the trick is to use the command chattr for changing file extended attribute .
A file with the ‘i’ attribute cannot be modified: it cannot be deleted or renamed,
no link can be created to this file and no data can be written to the file.
Only the superuser or a process possessing the CAP_LINUX_IMMUTABLE capability can set or clear this attribute.

# echo "Test chattr" >/tmp/test.txt
# touch /tmp/test.txt
# lsattr /tmp/test.txt
--------------e---- /tmp/test.txt
# chattr +i /tmp/test.txt
# lsattr /tmp/test.txt
----i---------e---- /tmp/test.txt
# touch /tmp/test.txt
touch: cannot touch '/tmp/test.txt': Permission denied

Loop trick in bash

For loop in BASH can be written in many ways , it could use a numeric counter
or it could use an array . here are some example that may help :


1. Count up loop in BASH, from 0 to 100

for i in `seq 0 100` ; do echo  $i ;done

2. Countdown loop in BASH , from 100 to 0

for i in `seq 100 -1 0` ; do echo  $i ;done

3. Array variable as increment loop in BASH

ar="one two three four five six eight nine ten"
for i in $ar ;do echo $i ;done

4. Array as increment loop in BASH

for i in one two three four five ;do echo $i ;done

5. For loop without seq

for i in {1..100} ; do echo $i ;done

Overflow /tmp partition

Sometimes your /tmp directory may be mounted to a partition name overflow
and the size for this partition may be 1M ,while its a 100% full capacity.
This may happen if the root partition got full ,
and the system was in need for some space to operate.
to overcome this issue , you may just unmount the partition using

# unmount overflow

If you get an error saying the device is busy , you may also try unmounting the partition with the
Lazy unmount flag . Detach the file system from the file system hierarchy now,
and cleanup all references to the file system as soon as it is not busy anymore.

# unmount -l /tmp

Splitting Certificate chain

Here is how one can split a certificate chain into individual files .
sure this can be done manualy in any text editor , but there comes a time when … well
such simple task may consume a lot of time . here are 2 examples how you can do it :
Method 1 :

cat $file|awk 'split_after==1{n++;split_after=0} /-----END CERTIFICATE-----/ {split_after=1} {print > "mycert" n ".pem"}'

Method 2 :

csplit -k -f mycert $file '/END CERTIFICATE/+1' {*}

Clear all containers and images in docker

If you want to clear all containers and all images from docker
there is a set of commands you can run to do that .
Note that this will delete the containers and images with no way of recover.
Delete All Containers :

docker rm $(docker ps -a -q)

Delete All images :

docker rmi $(docker images -q)

Learn Python

I had a good “administrator” life with Bash and pearl , but I came to see the power of python .
learning python is very easy , all you needs to do is replace any script you are about to write in bash with python .
Yes I know its easier to just write it in Bash , but believe me after a very short period of time it would feel as if you have no need for Bash
of course python can do much more then just scripts , but¬†one step at a time ūüôā
 
 

Gnome 3 power button shutdown

If you work with the new gnome 3 as your desktop you probably noticed
that clicking the power button doesn’t shut the computer , in my case it just restart it .
in order to set the power of there is a small tweak to be made :
open dconf-editor and navigate to :  org -> gnome -> settingsdaemon -> plugins ->power

change power-button from none to shutdown
 
now when you press power button , the system will shut it self down

Redirect to www in php

Here is a simple way to redirect any hit to www using php .
and also keep the protocol ( http/https )

if($_SERVER[HTTPS])
{
	header('Location:' . "http://www.$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" ,true, 301);
}
else
{
	header('Location:' . "https://www.$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" ,true, 301);
}

install cx_Oracle on CentOS 7

Here is a simple way to install cx_Oracle on CentOS 7
in order to install , we will use Oracle instant client , and python pip

1. Download and install oracle client from Oracle

yum install libaio
rpm -Uvh oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm
rpm -Uvh oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm
rpm -Uvh oracle-instantclient12.1-sqlplus-12.1.0.2.0-1.x86_64.rpm

2. Add Oracle client to library path

echo "/usr/lib/oracle/12.1/client64/lib" >/etc/ld.so.conf.d/oracle.conf
ldconfig

3. install epel repo via yum extra

yum install epel-release

4. Install additional

yum install gcc python-pip.noarch python-devel

5. Install  cx_Oracle via pip

pip install cx_Oracle

 

symetric gpg encryption

Symetric encryption is an encryption that uses same key to decrypt.
here is a way to encrypt file with symetric key without user interact ( batch )

gpg --batch -q --passphrase '<password>' --cipher-algo AES256 -c <file>