Table of Contents

Linux

Useful commands

User management

File/Process management

File copy hacks that track progress, from most GNU-compatible to least (i.e. need to install new libraries):

user:~$ curl -o [DST] file://[SRC]               # does not copy permissions
user:~$ dd if=[SRC] of=[DST] status=progress     # does not copy permissions
user:~$ rsync -ah --info=progress2 [SRC] [DST]
user:~$ pv [SRC] > [DST]                         # does not copy permissions
user:~$ cp [SRC] [DST]; progress -w              # monitors existing, but needs tty

BTRFS

On BTRFS partitions, need to be careful to avoid double-counting of disk space, since there is shared memory. Typically interface disk commands through btrfs utility.

For rescuing disk full problems, see here.

Networking

Webservice

Docker

Docker Compose

Git

Initial setup

Small editing and cleanups

Patching

Changing history

Signing

By default, GPG keys are used for signing commits (manually using -S flag). As of Git 2.34 (Nov 2021), SSH keys can also be used to sign commits.

Misc

Others

Create PDF pages:

# Create blank page using Ghostscript
# '/etc/ImageMagick-6/policy.xml' contains a policy setting rights
# to "none" for pattern "PDF" under the coder domain. This should be disabled
# (by commenting out) in order to use ghostscript for pdf. Note security concerns.
user:~$ convert xc:none -page A4 blank.pdf
user:~$ convert xc:none -page 842x595 blank.pdf  # horizontal
 
# Create blank page using 'ps2pdf'
user:~$ echo "" | ps2pdf -sPAPERSIZE=a4 - blank.pdf
 
# Append blank page, uses 'pdftk'
# Converts 'file.pdf' -> 'notes_file.pdf'
user:~$ export FILE=file.pdf
user:~$ pdftk A=$FILE B=blank.pdf cat A1-end B output notes_$FILE.pdf
 
# Interleave blank pages, uses 'pdftk'
user:~$ export FILE=file.pdf
user:~$ export NUM=`pdftk $FILE dump_data | grep NumberOfPages | awk '{print $2}'`
user:~$ pdftk blank.pdf cat $(printf '1 %.0s' $(seq $NUM)) output multiblank.pdf
user:~$ pdftk A=$FILE B=multiblank.pdf shuffle A B output notes_$FILE
> time dd if=/dev/zero of=/path/to/nfs/testfile bs=16k count=64k
65536+0 records in
65536+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 91.903 s, 11.7 MB/s

real	1m31.907s
user	0m0.035s
sys	0m1.307s


> time dd if=/path/to/nfs/testfile of=/dev/null bs=16k count=64k
65536+0 records in
65536+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 91.6337 s, 11.7 MB/s

real	1m31.639s
user	0m0.075s
sys	0m1.196s
/etc/systemd/system/{{NAME}}.service
[Unit]
Description={{DESCRIPTION HERE}}
Wants=network-online.target
After=network-online.target
 
[Service]
User=root
Group=root
Type=simple
ExecStart={{PROGRAM HERE}}
 
[Install]
WantedBy=multi-user.target

Keeping pipes open while writing:

mkfifo mypipe
exec 3>mypipe  # open file descriptor with writes piped to mypipe
echo "stuff" >> mypipe
echo "morestuff" >> mypipe
exec 3>&-  # close file descriptor, which closes mypipe

Bash

Use Ctrl-R to invoke command searching (as opposed to the bang-expansion method that immediately invokes the command). Other controls:

~/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
 
# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac
 
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
 
# append to the history file, don't overwrite it
shopt -s histappend
 
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=-1
 
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'
 
    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi
 
# colored GCC warnings and errors
export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
 
# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias ..='cd ..'
 
# Add timestamps to 'history' command
HISTTIMEFORMAT="%F %T  "
 
# Allow alias execution in sudo, e.g. "sudo ll"
# see https://askubuntu.com/a/22043
#   If the last character of the alias value is a space or tab character,
#   then the next command word following the alias is also checked for
#   alias expansion.
alias sudo='sudo '
 
# Go to latest directory
cdl() {
    cd  "$(\ls -1dt ./*/ | head -n 1)" && ls -rt
}
~/.inputrc
# Enables better *-wildcard completion
set show-all-if-ambiguous on
"\t": glob-complete-word

Vim

~/.vimrc
" Load internal configuration as well, if exists
" To see what was loaded, use :script
runtime defaults.vim
 
" Enable the FileType plugin
filetype plugin indent on
 
" Defaults for unknown files
set tabstop=4 " ts: show existing tab with 4 spaces width
set softtabstop=4 " sts: Sets the number of columns for a TAB
set shiftwidth=4 " sw: when indenting with '>', use 4 spaces width
set expandtab " On pressing tab, insert 4 spaces
" Use :retab to convert existing tabs to space
 
" EXCEPTIONS
" To see list of filetypes available, type ':setfiletype ' and Ctrl-D
" yaml, especially for docker-compose
autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab
 
" Enable relative line numbers
set relativenumber " rnu: add left-margin with line numbers relative to cursor
set number " replace cursor line number from 0 to absolute number
 
" Optional: Set vim colorscheme
colorscheme codedark
 
" Highlight git merge conflicts
" Adapted from: <https://vi.stackexchange.com/a/19760>
function! ConflictsHighlight() abort
    syn region conflictStart start=/^<<<<<<< .*$/ end=/^\ze\(=======$\||||||||\)/
    syn region conflictMiddle start=/^||||||| .*$/ end=/^\ze=======$/
    syn region conflictEnd start=/^\(=======$\||||||| |\)/ end=/^>>>>>>> .*$/
 
    highlight conflictStart ctermbg=red ctermfg=black
    highlight conflictMiddle ctermbg=blue ctermfg=black
    highlight conflictEnd ctermbg=green cterm=bold ctermfg=black
endfunction
 
augroup MyColors
    autocmd!
    autocmd BufEnter * call ConflictsHighlight()
augroup END

Others

For VSCode dark mode color scheme:

# User colorscheme
mkdir -p ~/.vim/pack/themes/start
git clone https://github.com/tomasiser/vim-code-dark ~/.vim/pack/themes/start/vim-code-dark

# System colorscheme
# Use the target location: "/usr/share/vim/vim{{VERSION}}/pack/themes/start"
sudo mkdir -p /usr/share/vim/vim82/pack/themes/start
git clone https://github.com/tomasiser/vim-code-dark /usr/share/vim/vim82/pack/themes/start/vim-code-dark

Other color schemes.

Rerun script