Skip to content

Latest commit

 

History

History
243 lines (168 loc) · 3.72 KB

bash_ex.md

File metadata and controls

243 lines (168 loc) · 3.72 KB

General commands for BASH

Get all OS updates -- use with caution!!

sudo -- sh -c 'apt-get update; apt-get upgrade -y; apt-get dist-upgrade -y; apt-get autoremove -y; apt-get autoclean -y';sudo reboot

Create alias

alias log="cd /var/log/snowstorm/"
alias db="cd /opt/snowstorm/NO/SNOMEDCT-NO"
alias snow="cd /opt/snowstorm/"

Apply alias for all users

sudo vi /etc/profile.d/alias.sh

Reboot machine

sudo /sbin/shutdown -r +5 >/dev/null 2>&1

Bashrc at start up

sudo vi /etc/bash.bashrc

Find lastest file in directory

unset -v latest
dir=$1
for file in "$dir"/\*; do
[[$file -nt $latest]] && latest=$file
done

Get diff

diff -u conceptIds_export conceptIds | grep '^\+' | sed -E 's/^\+//' > diff

Get similarities

fgrep -xf Concept_procedures_5.txt Concept_delta_25-5.txt

Get column 1 from spreadsheet

cut -f1 mpf_mp_multiple.csv
or
awk '{ print $1 }' mpf_mp_multiple.csv

Read column 1 starting from row 2

awk 'NR!=1 { print $1 }' Concept_procedures_5.txt

Filter uniques values X in column Y

awk -F'\t' '!seen[$6]++ { print $1,"\011"$28 }' "file.txt"

VIM - search and replace all - backslah to escape char

:%s/[old]/[new]/g

Create symbolic link

sudo ln -fs /etc/nginx/sites-available/[server_fqdn] /etc/nginx/sites-enabled/

Regex hitta alla meningar som INTE har 2021

^(?!._2021)._$

Regex för UUID

egrep '[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}' -o

Delete/ta bort lines rader i vim

:1,$d

Syntax to save to var

var=$(awk '{ print $1 }' file.txt)

Print tab-separated or comma-separated files

awk -F'\;' '{ print $1 }'
awk -F'\t' '{ print $1 }'

Line count

Wc -l fil.txt
wc -l | grep -Eo '[0-9]{1,9}' >> out.txt
wc -l file.txt | awk '{print ($0+0)}'

Curl with cookie

curl --cookie-jar cookies.txt -H 'Accept: application/json' -H "Content-Type: application/json" https:[url]/api/authenticate --data '{"login":"","password":"","rememberMe":"false"}'

Add num of cols to same row, num of cols=num of rows

paste -d "," - - - - - < proc\*.txt << tags.txt

Find exact matches (-x), print only num of rows(-c)

grep -cxf out.txt test.txt

Find all files with .zip

find /opt/snowstorm/ -name '\*.zip'

If condition for files with certain name

if [ -f 2022-01-05* ]; then
fi

Logga in med ssh på azure

ssh -i [key_loc] [user]@[server_fqdn]

copy files

scp -i [key_loc] [user]@[server_fqdn] /Users/oskar/Documents/

Mvn skip tests

mvn clean install -DskipTests=true
mvn clean package -DskipTests=true

VIM - search and replace all - backslash to escape char

:%s/[old]/[new]/g

Search and replace slash med - i strängar

cat branches.txt | sed -e 's/\#-/g' > out2.txt

Count uniques

uniq -c

While loop syntax - read file

while read p; do
  echo "$p"
done <peptides.txt

Remove files that don't have a certain file type/extension

ls "spring.log."\* |grep -v .gz|while read f; do sudo rm "$f"; done

Save all output including errors until the bash session is ended

<cmd> <args> > <file> 2>&1

Copy contents to clipboard

sudo apt install xclip xsel
xclip -selection clipboard < file.txt

Build multiline email, e.g. for statuses

cat << EOF > results/$(date +"%d-%m-%Y")/error_log.txt
To: [email protected]
From: [email protected]
Subject: Notification

Hello World
EOF

See all mails

mailq

Empty inbox

sudo postsuper -d ALL