Skip to content

Instantly share code, notes, and snippets.

@ggrrll
Last active April 17, 2024 09:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggrrll/b83e05eeb16c31204fdaffd42ecc365a to your computer and use it in GitHub Desktop.
Save ggrrll/b83e05eeb16c31204fdaffd42ecc365a to your computer and use it in GitHub Desktop.
programming TIPS

Bash / Unix admin

https://www.cheatsheet.wtf/Nano/

	hex2dec(){

        	hexnum="$1" 
	        echo $((16#$hexnum))
	}

networks

  • get host's IP dig +short myip.opendns.com @resolver1.opendns.com

useful bashrc / bash_profile

# some more ls aliases
alias ll='ls -larth -F'
alias la='ls -Al'
alias l='ls -CF'
alias nano='nano -L -N -c'
#alias less='less -N'
alias diff='diff --suppress-common-lines --color=always -y'

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 -n'
    alias fgrep='fgrep --color=auto -n'
    alias egrep='egrep --color=auto -n'
fi

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoredups

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=
HISTFILESIZE=

GPU monitornig

  • lspci | grep VGA
  • pip install gpustat (only NVIDIA)
  • apt-get install nvtop

file names

partitions and disks

disks sizes

cleaning up space

network monitoring

desktop env

users & groups

proper permissions -- chmod

  • home: 750
  • .ssh/authorized_keys: 644
  • private ssh key: 400

create new user

  • sudo useradd -m username
  • sudo passwd username
  • usermod -aG sudo username

check groups and members

  • groups [user]

  • members [group-name] or grep 'group-name-here' /etc/group

  • cat /etc/group | cut -d: -f1 -- list all groups

  • find /home -group test -- files owned by group 'test'

check open ports

from https://www.cyberciti.biz/faq/unix-linux-check-if-port-is-in-use-command/

  • sudo lsof -i -P -n | grep LISTEN -- less /etc/services
  • netstat -tulpn | grep LISTE

Security

Ubuntu / Mint

  • change config of progressive apps (browser apps), edit files in ~.local/share/applications

Python

parallelization

code analysis

(sub) plotting

debugging

  • ipdb has autocompletion, loads variables.. : ipdb3 my_script.py
  • reload(my_package) -- credits to this SO psot
  • pip update selected packages (bash pipe): e.g. pip freeze | grep jupy | cut -d'=' -f1 | cut -d":" -f2 | xargs pip install -U

miscellanea

VS code

Jupyter notebook

layout

%load_ext autoreload

this is problematic...many things tried (none worked):

  • %autoreload 1 + %aimport tiqcovid
  • %autoreload 2
  • removing the module from subfolder > put in external folder
  • sys.path.append('path_to_package')

debugging

  • %debug -- here is an intro

  • from IPython.core.debugger import set_trace -> better to use pdb.set_trace()

here are a description of commands + GUI

plotting

saving outputs

save cell to file :)

%%writefile example.txt

from interactive prompt

  • first capture the output:
%%capture my_var
my_command

like explained in the 2nd answer here

  • to save do:

%%store my_var.stdout >> my_file.txt

from script in terminal

mind the buffering! -.-

https://unix.stackexchange.com/questions/182537/write-python-stdout-to-file-immediately

print all attributes of python object

for attr in dir(obj):
    print "obj.%s = %s" % (attr, getattr(obj, attr))

shut down all notebooks servers kept open

  • list all servers with jupyter notebook list
  • try: jupyter notebook stop PORTID
  • else: deleted the related file in /Library/Jupyter/runtime
  • find the PIDs related to each port with ps aux | grep PORTID
  • kill the process with kill -9 PID

find kernel version

from sys import executable

notebook on remote server

on local host ssh -L localhost:8888:localhost:8889 username@your_remote_host_name (-N -f flags are not needed)

and on remote: jupyter notebook --no-browser --port=8889

refs: https://amber-md.github.io/pytraj/latest/tutorials/remote_jupyter_notebook https://towardsdatascience.com/running-jupyter-notebooks-on-remote-servers-603fbcc256b3

presentation

https://voila.readthedocs.io/en/stable/using.html is a nice tool to make nb nore suitable for presentations

tricks:

  • show code cells with --strip_sources=False

python packaging

generate static html

GitHub

actions

Docker

manage-docker-as-a-non-root-user

general good practises

https://docs.docker.com/develop/develop-images/instructions/

https://docs.docker.com/develop/develop-images/guidelines/

tips

docker on github

some useful commands

  • images

    • docker image ls
    • docker image rm [image_ID]
    • docker build --network=host -t [full_tab_incl_webhost] -f [docker_file] (build an image to be set on a remote host)
    • docker push [full_tag]
    • docker inspect [image_ID]
    • docker run -it [full_tag] (to run a container on that image)
    • docker exec -it <mycontainer> bash to log into a container shell
  • containers

Kubernetes

  • kubectl get pods -A
  • kubectl delete pod [pod_ID] -n [namespace]
  • kubectl describe pod [pod_ID] -n [namespace]
  • kubectl exec -it [pod_ID] -n [namespace] engine -- bash

Git

intro tutorials

nice cheatsheet https://www.quora.com/What-is-the-best-Git-cheat-sheet https://learngitbranching.js.org/.
https://gitimmersion.com/.
https://githowto.com/.

long list: https://medium.com/javarevisited/11-best-online-places-to-learn-git-for-beginners-in-2021-6dc2b7c6ef48

tips

  • to avoid messing up after fetching (merge conflicts), check the diff first!:

git diff origin/master..

'going back'

mind! revert VS reset VS checkout

https://www.w3docs.com/snippets/git/how-to-reset-or-revert-a-file-to-a-specific-revision.html

merge

git LFS

Android

flash a ROM with fastboot

do these steps, in this order, before restoring your stuff...

  1. OEM + UBS from dev options
  2. check adb devices
  3. adb reboot bootloader
  4. fastboot oem unlock
  5. sh flash_your_ROM.sh
  6. fastboot oem lock

source: https://forum.xda-developers.com/mi-a2/how-to/guide-official-stock-android-10-t4033835

adb sideload

TWRP issue

I got Failed to mount '/system' error while trying to wipe

OTA installation

https://www.androidsage.com/2017/11/10/how-to-install-ota-updates/

https://fossbytes.com/install-ota-updates-using-android-recovery-and-adb-sideload/

https://www.theandroidsoul.com/how-to-install-ota-updates-manually-using-recovery-and-adb-sideload/

SQL

remove annoying crtl + C:

use --sigint-ignore

credtits https://dba.stackexchange.com/questions/20566/mysql-console-ctrlc-driving-me-nuts

Regex

easy regex generators

https://www.autoregex.xyz/
https://simple-regex.com/
https://regex-generator.olafneumann.org

R

clean code

(thanks to this SO post )

Latex

tools to prettify latex code:

  • install latest texstudiousing these commands
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null ; brew install caskroom/cask/brew-cask 2> /dev/null

then

brew cask install texstudio