MISC TUTORIALS


Home About me

LINUX CODE SNIPPETS


CRON

Restart cron service:

sudo service cron restart
  

Check status of cron:

systemctl status cron.service 
  

Add cronjob with crontab:

crontab -e 
  

List devices connected to local hotspot


In shell:

arp -a  
  

Shell directory jump

I found this trick while using 'Bash on Ubuntu on Windows'. The directory where I saved my local repositories is in another drive. The command folder navigation was very annoying.

In order to create a directory jump shortcut in the shell, modify the ~/.bashrc file and add the following line:

~/.bin/cd-rep
  

cd-rep will be my new command to change directory to my local repositories directory.

In the ~/.bin directory, create a new file named cd-rep

Insert the following bash script:

#!/bin/bash
function cd-rep(){
    cd /mnt/e/Repositories
}
  

In my case, my repository folder is located in /mnt/e/Repositories.

Now, by only typing cd-rep, I can jump directly to my local repository folder.

Mounting NTFS partition as read only in a folder

First created a folder in the /mnt/ directory. Next, mount a partition, in this case, sda3 with the following commands:

sudo mount -o ro /dev/sda3 /mnt/win_hd
  

To see list of devices use the following command:

sudo fdisk -l 
  

See where the devices are mounted with this command:

sudo df -HT
  

Changing brightness

I spilled some water on my laptop and my brightness buttons stopped working. The following command is a work-around to change the brightness through the command line. In the example below, brightness is set to 70%.

xbacklight -set 70

Thinkfan

Thinkfan is a software to control the speed of the fan of thinkpads running linux. In order to install using the apt-get manager in ubuntu, run the following command:

sudo apt-get install thinkfan
  

Modify /etc/thinkfan.conf to adjust fan speed, my configuration is the following:

 (0,	0,	42)
(1,	40,	47)
(2,	45,	52)
(3,	50,	57)
(4,	55,	62)
(5,	60,	66)
(127,	63,	32767)
  

To set up thinkfan to start automatically, edit etc / default / thinkfan / and replace the following line by "yes"

START = yes
  

TLP BATTERY MANAGEMENT

TLP brings advanced power management to linux, it has special support for advanced thinkpad battery management.

The official ppa can be found here.

It can be installed in an ubuntu-based distribution as follows:

sudo add-apt-repository ppa:linrunner/tlp
sudo apt-get update 
sudo apt-get install tlp tlp-rdw
sudo apt-get install tp-smapi-dkms acpi-call-dkms //Additional package for thinkpads.
  

Battery threshold can extend the battery life, I configure my thinkpad according to Lenovo's advice, a lower and upper threshold of 85% and 90%, respectively

In order to check the status and default settings, the following command is useful:

sudo tlp-stat  
  

Charge thresholds of 85% lower and 90% upper, can be set with the following command:

sudo tlp setcharge 85 90 BAT0
  

Imagemagick

mogrify -format jpg *.png //Allows to convert .png files to .jpg files, preserving the filenames.

mogrify -resize 60x60% -quality 60 -format jpg *.jpg //!careful as this will overwrite files. It will resize by 60% and reduce quality to 60%.
// save in another path; path must exist
mogrify -path ./mogrified/ -resize 60x60% -quality 80 -format jpg *.jpg
  

REMOVE DOS/WIndows style line endings (i.e. ^M)


Type in VIM:

:%s/^M// 
  

Or, use the dos2unix program in a unix shell.

Size of directories in current location


Type:

du -h --max-depth=1 | sort -hr 
  

ncdu is also useful for visualizing folders.

Install in an Ubuntu based distribution, type:

sudo apt-get install ncdu 
  

PURGE APPLICATION


Remove an application by purging for example.

sudo apt-get autoremove eclipse --purge
  

FORCEFULLY REMOVE DIRECTORY


This will forcefully wipe a directory:

sudo rm -rf /path/to/directory/
  

CHANGE OWNERSHIP EXECUTION RECURSIVELY


chmod +x -R 
  

LIST ONLY DIRECTORIES


ll -d */ 
  

More info here.

BATCH ADD PREFIX


Change 01.foo, 02.foo, etc to prefix01.foo, prefix02.foo, etc.

for file in *; do mv "$file" "prefix$file"; done 
  

HTML


MATHJAX SCRIPT

Add the following script to the html file:

<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<script type="text/javascript" async
src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_CHTML">
</script>
  

SYNTAX HIGHLIGHTING

Google prettify does sintax highlighting on text. Add this to your script to enable it:

<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"> </script>
  

VIM


paste without indenting:

:set paste 
  

show line numbers:

:set numbers 
  

Toggle gutter-sign column in VIM (if it has the git-gutter plugin installed.

:GitGutterToggle 
  

Add the following line to ~/.vimrc to disable gitgutter by default:

let g:gitgutter_enabled = 0 
  

How to get toggle vertical indenting guidelines from indentLine module:

:IndentLinesToggle 
// to disable by default
let g:indentLine_enabled = 0
  

If accidentally pressing C-s (i.e. control+s), it will enable scroll lock. To disable use C-q.

MAKEFILES


To let shell print each command as they are about to be executed, add this to the makefile (page 52 of the GNU Make book by John Graham-Cumming:

SHELL += -x 
  

Some special make-variables are:

all: library.cpp main.cpp
---------------------------------------------
$@		- evaluates to all
$<		- evaluates to foolib.cpp
$^		- evaluates to foolib.cpp and foo.cpp
  

Other rules can be found here.

RCLONE


Nice tutorial here

LATEX


Text document production system for ubuntu:

sudo apt-get install texlive-latex-extra
  

Installing texlive also includes minted latex package which can be used to format source code.

Example of use of minted:

\documentclass{article}
\usepackage{minted}
\begin{document}
\begin{minted}{c}
int main() {
printf("hello, world");
return 0;
}
\end{minted}
\end{document}
  

This can be compiled into a pdf by invoking shell escape command:

$ pdflatex -shell-escape minimal 
  

To compile in TexStudio:

The following user command must be added in "Options/configure_tex_studio/build" - in the user command panel:

arara %.tex
  

The following line must also be added to the .tex document:

% arara: pdflatex: {shell: true} 
  

To convert latex to html, install:

sudo apt-get install tex4ht 
  

Run in the shell the following command to create a HTML file:

htlatex mydoc.tex 
  

The content of this page is licensed under Attribution-NonCommercial 4.0 International CC BY-NC 4.0. Any source code, if otherwise unspecified, is licensed under the MIT License