Skip to content

Instantly share code, notes, and snippets.

@gohjiaying
Last active May 7, 2024 03:53
Show Gist options
  • Save gohjiaying/9de3aaffa353c78a77ddfca3c748a26e to your computer and use it in GitHub Desktop.
Save gohjiaying/9de3aaffa353c78a77ddfca3c748a26e to your computer and use it in GitHub Desktop.

Useful Commands

Docker

Code Description
docker rm $(docker ps -a -q -f status=exited) To remove all the containers that have exited
EC2_REGION=wget -q -O - http://169.254.169.254/latest/meta-data/placement/availability-zone|sed s/.$//
aws ecr get-login --region $EC2_REGION --no-include-email
To login into ECR
docker pull amazon/amazon-ecs-sample Just a ecs sample

VS Code Shortcut

Shortcut Description
shift + opt + down arrow key Duplicate current line
cmd + k + 0 fold all region
cmd + k + j unfold all region
cmd + opt + [ fold single region
cmd + opt + ] unfold fold single region

Python

Description Code Remarks
Create a virtual environment called venv virtualenv venv
Activate the virtual environment . venv/bin/activate
See installed pip freeze
Create a requirements file pip freeze > requirements.txt
Deactivate the virtual environement deactivate
Remove the virtual environment rm -r venv
To load pip file pip install -r requirements.txt
Jupyter notebook kernel within the virtual env python -m ipykernel install --user --name=<kernel name> YT Vid

Git CLI

image Reference: https://marklodato.github.io/visual-git-guide/index-en.html

Description Code
View local commits git log origin/main..HEAD
View git commits in graph git log --oneline --graph --decorate
Undo 1 commit on current branch git reset --soft HEAD~1
Merge main into current branch git checkout <branch>, git merge main

EC2 Configuration

Description Code
To know details about your virtual instances curl http://<instance number>/latest/meta-data/
To know details about virtual instance user permission curl http://<instance number>/latest/meta-data/iam/security-credentials/MyAdmin
To know what is your user name in your policy, you can use variable "Resource": ["arn:aws:s3:::mybucket/${aws:username}/*"]

BASH CLI

Description Code
History Ctrl R
Next History Ctrl R
Cut Ctrl U
Paste Ctrl Y

VIM commands

Description Code
Set number :set number
Go to line :[linenumber]
Go to end of line $
Undo Ctrl U
Indent next 3 lines 3>>
Unindent next 3 lines 3<<
Delete inside word diw
Delete word to end of line dw
Enter a new line and go into insert mode o

Script Commands

Description Code Remarks
Check if file exist test -f test.py; echo $? -f flag checks if file exists. If returns 0 = true

Network troubleshooting

Description Code Remarks
Check tcp traffic tcpdump dst port 443 dump traffic on port 443, result format = (incoming traffic) > (outgoing traffic)

S3 Copy Commands

Description Code Remarks
S3 copy local folder to bucket aws s3 cp ./scripts s3://<bucket-name>/scripts --recursive copy from local script folder, into s3 bucket with prefix scripts

Python manipulation

Description Code Remarks
From list to dict
def Convert(lst): 
res_dct = {lst[i]: lst[i + 1] for i in range(0, len(lst), 2)}
return res_dct
lst = ['a', 1, 'b', 2, 'c', 3]
print(Convert(lst))
https://www.geeksforgeeks.org/python-convert-a-list-to-dictionary/
From dict retrieve elements for array subnets = [{subnet['SubnetId'] for subnet in resp}]
From dict retrieve single element as key subnets = [{subnet['SubnetId']:subnet for subnet in resp}]
Create a new-dict with desired key e.g. k = [key_a,key_b] from dict newdict = {k: testdict[k] for k in keep} https://stackoverflow.com/questions/32727294/remove-keys-from-object-not-in-a-list-in-python

Create Windows Program Bin and add environment path

Description Code Remarks
From powershell create directory New-Item -Path "$env:UserProfile\bin" -ItemType Directory
Add folder to path bin reg add HKEY_CURRENT_USER\Environment /v Path /t REG_EXPAND_SZ /d "$env:Path;$env:UserProfile\bin"
Check path is added $env:Path

Check OS Version

Description Code Remarks
Windows systeminfo
Linux cat /etc/os-release
Mac system_profiler SPSoftwareDataType
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment