Bash snippets

Alias

Alias comunes

alias ..="cd .."
alias ...="cd ../.."
alias ll="exa -l"
alias la="exa -la"
alias cdc="cd '$HOME/code'"

alias gaa="git add -A"
alias gc="git commit"
alias gs="git status -sb"
alias gf="git fetch --all -p"
alias gps="git push"
alias gpsf="git push --force"
alias gpl="git pull --rebase --autostash"
alias gb="git branch"

Alias más usados

function zsh-stats() { 
 fc -l 1 | awk '{CMD[$2]++;count++;} END { for (a in CMD)	print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n25; 
}

Case statement

#!/bin/bash

echo -n "Enter the name of a country: "
read COUNTRY

echo -n "The official language of $COUNTRY is "

case $COUNTRY in

  Lithuania)
    echo -n "Lithuanian"
    ;;

  Romania | Moldova)
    echo -n "Romanian"
    ;;

  Italy | "San Marino" | Switzerland | "Vatican City")
    echo -n "Italian"
    ;;

  *)
    echo -n "unknown"
    ;;
esac

Remove last login message

Para remover el mensaje Last login: Sat Apr 22 15:10:36 on ttys006 que aparece por defecto al abrir una terminal solo basta con crear el archivo .hushlogin en HOME.

cd $HOME
touch .hushlogin

Create file with unique id

# scripts.sh 
#!/bin/bash
timestamp(){
	date +%Y%m%d%H%M%S
}

note(){
  timestamp | xargs -I % sh -c 'nvim ./docs/%.md'
}

count_notes(){
  find ./docs -name "*.md" | wc -l
}

$*

Usando el script para timestamp puedo crear una nota

./scripts.sh timestamp | xargs -I % sh -c 'touch ./docs/%_note.md'

Git scripts

#!/bin/bash

is_clean_work_tree () {
	echo "Saving current changes ..."
	if git status --porcelain ; then
	  # changes
		echo "Exist changes"
    commit
	else 
	  # No changes
		echo "No changes"
	fi
}

commit () {
  msg="Code from $(date +%Y-%m-%d)"
  git add .
  git commit -m "$msg"
}

push_changes (){
  echo "Pushing changes ..."
  add_ssh_key
  git push
}

add_ssh_key() {
  eval $(ssh-agent -s)
  ssh-add ~/.ssh/name_key
}
$*

Tmux

Create new session and set directory

tmux new -s name -c ~/Documents

Create new session passing path from ENV variable

tmux new -s repo -c $GITHUB

Move pane to a new window

https://superuser.com/questions/600286/move-pane-to-a-new-window-in-tmux

bind-key: ctrl a

bind-key !

Resize panes

# :resize-pane - $DIRECTION $cell-size

:resize-pane -L 20 # Resizes the current pane Left by 20 cells
:resize-pane -R 20 # Resizes the current pane Right by 20 cells
:resize-pane -D 20 # Resizes the current pane Down by 20 cells
:resize-pane -U 20 # Resizes the current pane Upward by 20 cells

SSH

Crear key

Investigar para que eran los flags

ssh-keygen -t ed25519 -a 100 -f ~/.ssh/name_file -C "test@gmail.com"

Copiar archivos

scp username@ip_address:/home/username

Copiar archivos

scp -r source_dir username@ip_address:/home/username

Git

Saber cual es el URL al que apunta origin

git remote get-url origin

Cambiar URL de origin en repositorio local

git remote set-url origin NEW_URL

Zip

Zip a folder

zip -r filename.zip  ./*

Zip without root folder

(cd directory && zip -r ../out.zip .)

Unzip folder

unzip filename.zip -d /path/to/dir

FZF

Search file and open in neovim

fzf | xargs nvim

Check ports in use

lsof -i -P