Skip to content

Instantly share code, notes, and snippets.

@dohaivu
Last active April 20, 2023 12:06
Show Gist options
  • Save dohaivu/96b1a7e0ad8787987891030722dc0370 to your computer and use it in GitHub Desktop.
Save dohaivu/96b1a7e0ad8787987891030722dc0370 to your computer and use it in GitHub Desktop.
[bash command] common bash command #bash

Links

Bash by Examples
Linux Commands In Structured Order with Detailed Reference
Find Locate

APT-GET

Keyboard

Hold Option + Click: to move cursor to destination position
CTRL+a jump to the beginning of line
CTRL+e jump to to the end of line
CTRL+w will delete a word backwards
CTRL+z pause a command
CTRL+l clear the screen
CTRL+d exit current shell
CTRL+R search

CTRL + F: Forward
CTRL + B: Backward
CTRL + N: Next line
CTRL + P: Prev line
CTRL + H: Backspace
CTRL + D: Delete
CTRL + A: Begining of line
CTRL + E: End of line
CTRL + K: Kill line

Reload

source ~/.bash_profile
. ~/.bash_profile
hash -r python # to tell bash to reset the "cached" location to the python executable

Apt

sudo apt-get update
sudo apt-get upgrade

Users

cut -d: -f1 /etc/passwd # list all users
sudo useradd -m myuser # add user
sudo passwd myuser # change password
sudo usermod -s /bin/bash myuser
sudo userdel myuser #delete user

sudo chown -R node:node /home/node && \
    chmod -R 777 /home/node

Process

ps aux | grep java # print all process
ps axjf # a tree
top

kill -STOP PID # pause the process
kill -CONT PID # resume the process

File

find / -name file.look # file a file

// symbol link
ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl

ls | tee file # output both screen and file

Network

# download file
wget http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.csv -O earthquakes.csv

curl -O https://storage.googleapis.com/cloud-training/archinfra/Example-billing
-export.csv // download

Commands

jps | grep Launcher # find process id

# Date
date -u +"%Y-%m-%dT%H:%M:%SZ" # iso date
date -u +"%Y%m%dT%H%M%SZ"

# Logs
tail -f catalina.out &

# Find ports in use
lsof -i tcp:3000

cat /proc/cpuinfo # os info

SSH

ssh root@70.32.86.175
ssh-add -K ~/.ssh/id_rsa # add key to agent

//copy files
scp -i shizzle-dev-eu.pem /Users/haivu/projects/shizzle/apn-dev-cert.pem ec2-user@demo.shizzle.co:/home/ec2-user

Encryption

# generate hash
echo -n "foobar" | openssl dgst -sha256
#-md4, -md5, -ripemd160, -sha, -sha1, -sha224, -sha384, -sha512 or -whirlpool

openssl x509 -in keytool_crt.pem -inform pem -noout -text # show certificate

Execute a script

sh -x script.sh

Extract

tar -zxvf example.tgz
unzip -n JMeterPlugins-Standard-1.2.0.zip -d apache-jmeter-2.12

tar jxf yjp-2015-build-15062-linux.tar.bz2

Iptables

http://www.cyberciti.biz/tips/linux-iptables-examples.html

# Clear
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

# Add
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
iptables -t nat -I OUTPUT -p tcp -d 127.0.0.1 --dport 80 -j REDIRECT --to-ports 8080

# Allow ip
iptables -A INPUT -p udp -m udp -s 175.45.118.9 -j ACCEPT
iptables -A OUTPUT -p udp -m udp -d 175.45.118.9 -j ACCEPT

# list
sudo iptables -t nat --line-numbers -n -L

# delete
sudo iptables -t nat -D PREROUTING 2

Tools

netstat -s #
nicstat l
pidstat -t 1
awk
sed
screen -S <name> # start a new screen session with session name
screen -ls # list running sessions/screens
screen -x # attach to a running session
screen -r <name> # attach to session name

C-a d #detach
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment