symlinks in virtualbox

If you are using Windows in Virtualbox ontop of Linux based (i.e: Ubuntu) machine, you might have seen this problem that accessing symlinks in windows does not work. Instead of showing contents after resolving Linux symlinks, you will end up with “Protocol error”.

This error can be fixed using vBoxManage by setting a boolean property (SharedFoldersEnableSymlinksCreate) against each folder shared with VirtualBox based machine.

VBoxManage setextradata <VirtualMachineName> VBoxInternal2/SharedFoldersEnableSymlinksCreate/<SharedFolderName> 1

After executing above command, verify your change with following:

VBoxManage getextradata <VirtualMachineName> enumerate

Ubuntu: Bash history management

# To see top commands
history | awk '{print $2}' | sort | uniq -c | sort -rn | head
# To remove a commands from history
# Replace YOUR_COMMAND with the initials of the command you want to remove from bash history
sed -i '/^YOUR_COMMAND/d' ~/.bash_history

Setup: FTP server

# Setup FTP server
# Instructions are tested on: Ubuntu 18.04.3 LTS
apt install -y atftpd
sudo gedit /etc/default/atftpd
# – set USE_INETD=FALSE
# – Optional: To change directory to host FTP contents, change /srv/tftp in OPTIONS line
# – Optional: To have maximum log: change –verbose=7 and append –logfile /var/log/atftpd.log
sudo mkdir -p /srv/tftp
sudo chmod -R 777 /srv/tftp
sudo chown -R nobody /srv/tftp
# Vanilla contents
# USE_INETD=true
# # OPTIONS below are used only with init script
# OPTIONS="–tftpd-timeout 300 –retry-timeout 5 –mcast-port 1758 –mcast-addr 239.239.239.0-255 –mcast-ttl 1 –maxthread 100 –verbose=5 /srv/tftp"
# After changes
# USE_INETD=false
# # OPTIONS below are used only with init script
# OPTIONS="–tftpd-timeout 300 –retry-timeout 5 –mcast-port 1758 –mcast-addr 239.239.239.0-255 –mcast-ttl 1 –maxthread 100 –verbose=7 –logfile /var/log/atftpd.log ~/tftp"
# Run the service
sudo invoke-rc.d atftpd start
# Verify atftpd is running
netstat -lnp | grep ":69 "
# Use atftp FTP client to verify atftpd
apt install -y atftp
atftp localhost
# Make sure /etc/hosts is having following mapping otherwise localhost above command will not work
# 127.0.0.1 localhost
# Download an file from FTP server
get file_name_which_is_hosted_on_ftp

rename git branches

Let’s rename a git branch locally & remotely.

Let’s assume that you got git repository with multiple branches and you want to mark a branch as a master whereas existing master to something else.

  • Make note of the branch names and their respective remote
git branch -vv
  • Goto server repo and switch default branch
git symbolic-ref HEAD refs/heads/new-master
  • Goto local repo, checkout master
git checkout master
  • Rename it locally
git branch -mv master something
  • Remove remote branch
git push origin --delete master
  • Set new remote
git branch --unset-upstream
git push --set-upstream origin something
  • Goto server repo and switch default branch
git symbolic-ref HEAD refs/heads/something
  • Switch to branch which is going to be new master
git checkout new-master
  • Rename this branch
git branch -mv new-master master
  • Remove remote branch
git push origin --delete new-master
  • Set new remote
git branch --unset-upstream
git push --set-upstream origin master
  • Goto server repo and switch default branch to master
git symbolic-ref HEAD refs/heads/master

Merge PDF files

Merge pdf files placed in a folder:

#!/bin/bash
# This script merge all pds found in a folder
# Where $1 is folder name
FOLDER="$1"
FILE_COUNT="$(ls $FOLDER/*.pdf | wc -l)"
NEW_NAME="merged-pdfs-$FILE_COUNT$(cat /dev/urandom | tr -dc '0-9a-zA-Z' | fold -w 16 | head -n 1).pdf"
CMD="gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=$NEW_NAME"
# Get all files and append to command
for file in "$FOLDER"/*.pdf; do CMD="$CMD $file"; done
echo "Folder: $1"
echo "File Count: $FILE_COUNT"
echo "Pdf: $NEW_NAME"
# echo "Cmd: $CMD"
echo ""
echo "Merge in progress…"
ret="$($CMD)"
echo "*** $NEW_NAME is ready."

view raw
Merge pdf files
hosted with ❤ by GitHub

GCC – Dump default compile macros

Use following commands if you want to see default macros passed by the compiler  during compilation:

# Compiler default macros in standard build
gcc -dM -E - < /dev/null

# Compiler default macros with optimization level 0
gcc -dM -E -O0 - < /dev/null

# Compiler list warning enabled using following flags
gcc -Wall -Wextra -Q --help=warning # Compiler default macros in case of c++11 g++ -dM -E -x c++ -std=c++11 - < /dev/null

Multiple Python installation

#
# Commands are tested on Ubuntu 14.04
#
# Install the python3 as Ubuntu 14.04 comes with Python2
sudo apt-get update -y && \
sudo apt-get install python-pip python3 python3-pip
# Let's manage two versions
# Ubuntu is already having pip2 as /usr/bin/pip. We need to remove it otherwise update-alternatives can't create symlinks
sudo rm /usr/bin/pip
sudo update-alternatives –install /usr/bin/python python /usr/bin/python2.7 10 –slave /usr/bin/pip pip /usr/bin/pip2
sudo update-alternatives –install /usr/bin/python python /usr/bin/python3.5 20 –slave /usr/bin/pip pip /usr/bin/pip3.5
# Machine is configured for python3 at this point, let's see
python –version
— OR —
pip –version
# To switch b/w different versions
sudo update-alternatives –config python
# To see available versions
sudo update-alternatives –list python
# To remove link
sudo update-alternatives –remove gcc /usr/bin/python3.5

Multiple GCC installation

#
# Commands are tested on Ubuntu 14.04
#
# Issue following command to verify which GCC version is installed
gcc –version
— OR —
g++ –version
gcc (Ubuntu 4.8.5-4ubuntu8~14.04.2) 4.8.5
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# Install the GCC-7
sudo apt-get update -y && \
sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test && \
sudo apt-get update -y && \
sudo apt-get install gcc-7 g++-7 -y
# Let's manage two versions
sudo update-alternatives –install /usr/bin/gcc gcc /usr/bin/gcc-4.8 10 –slave /usr/bin/g++ g++ /usr/bin/g++-4.8
sudo update-alternatives –install /usr/bin/gcc gcc /usr/bin/gcc-7 20 –slave /usr/bin/g++ g++ /usr/bin/g++-7
# Machine is configured for GCC-7 at this point, let's see
gcc –version
— OR —
g++ –version
gcc (Ubuntu 7.3.0-21ubuntu1~14.04) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# To switch b/w different versions
sudo update-alternatives –config gcc
# To see available versions
sudo update-alternatives –list gcc
# To remove link
sudo update-alternatives –remove gcc /usr/bin/gcc-7