# Convert your sitelinks to https with sed

I’ve wrote before the small post on how to find and substitute the string in all files with sed command in GNU/Linux. Now I’d like to show the real use case.

You’ve bought a new SSL certificate and configured your web server. After you fire it, you can make an unfortunate discover when you open the browser on your brand new https://example.com, that it says “Some parts of this page are not secure”.

So what is insecure? Simply you may have used the insecure contents like images or extenal JS libraries loaded from CDN.

To help with this, Linux has a small and powerful command to find and substitute the old_phrase with the new_phrase in all files and directories recursively - it is sed command on GNU/Linux

find . -type f -print0 | xargs -0 sed -i 's/old_phrase/new_phrase/g'

Attention! The previous command finds files also in the hidden folders and if you’re working with Subversion or GIT you’d like to skip them. The following keys -not -path '*/\.*' makes the trick

find . -not -path '*/\.*' -type f -print0 | xargs -0 sed -i 's/http:\/\//https:\/\//g'

<img src="http://example.com/dot.png">

will be converted to

<img src="https://example.com/dot.png">

Happy coding!

# Find and substitute the string in all files with sed command on GNU/Linux

Small and powerful command to find and substitute the old_phrase with the new_phrase in all files and directories recursively with sed command on GNU/Linux

find . -type f -print0 | xargs -0 sed -i 's/old_phrase/new_phrase/g'

Attention! The previous command finds files also in the hidden folders and if you’re working with Subversion or GIT you’d like to skip them. The following keys -not -path '*/\.*' makes the trick

find . -not -path '*/\.*' -type f -print0 | xargs -0 sed -i 's/old_phrase/new_phrase/g'

# Oracle fixed length CHAR field in Django

When you’re developing the Django app based on the legacy Oracle database you’ll find this type of model

from django.db import models

class Product(models.Model):
type_product = models.CharField(max_length=3)
code_product = models.CharField(max_length=15)
price = models.DecimalField(max_digits=8, decimal_places=2)

class Meta:
unique_together = (('type_product', 'code_product'),)

where the primary key is made by the tuple ('type_product', 'code_product'). The field code_product usually is filled by some characters and the rest is padded with spaces to respect the Oracle’s CHAR(15) type. You may wish to trim those spaces in your REST web-service, but still wish to filter lists and to join on foreign keys.

Here is my hack of this problem, the CharFieldPadding class register the new character field in Django that call handy ljust function to pad the string with spaces and respect the max_length parameter.

from django.db import models

def __init__(self, max_length, *args, **kwargs):
kwargs['max_length'] = max_length

def get_prep_value(self, value):
return value.ljust(self.max_length, ' ')

class Product(models.Model):
type_product = models.CharField(max_length=3)
price = models.DecimalField(max_digits=8, decimal_places=2)

class Meta:
unique_together = (('type_product', 'code_product'),)

Everything works smoothly in Django Rest Framework and you have nice looking URLs.

Happy coding!

# Show git branch in the bash command prompt

When you install git on your computer, you may find new variables available in the environment, it is $(__git_ps1). This variable contains the branch name of the current repository. The only thing you need to edit ~/.bashrc and add$(__git_ps1) to the PS1 definition in this way

if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}$\033[01;32m$\u@\h$\033[00m$:$\033[01;34m$\w$\033[00m$$(__git_ps1)\$' else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(__git_ps1)\$' fi N.B. the$(__git_ps1) is available by default for Ubuntu >= 14.04, you may check if it works by just going to any git repository and run echo $(__git_ps1) ~/Work/moiseevigor.github.io$ echo $(__git_ps1); (master) If you see an empty string, so just source it from /etc/bash_completion.d/git, and in this case the ~/.bashrc will look like source /etc/bash_completion.d/git if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}$\033[01;32m$\u@\h$\033[00m$:$\033[01;34m$\w$\033[00m$$(__git_ps1)\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(__git_ps1)\$ '
fi

This is it, now you have a gorgeous prompt

:~/moiseevigor.github.io (master)$Have a nice branching! # Remove old kernels in Ubuntu To remove old linux kernels and leave the current one dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/$$.*$$-$$[^0-9]\+$$/\1/")"'/d;s/^[^ ]* [^ ]* $$[^ ]*$$.*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge

Be aware, the command uses uname -r to get the current kernel.