ssh-keygen -t rsa
Git undo last commit and keep working changes
git reset --soft HEAD~
Git squash last 2
commits
Will undo the last 2
commits and keep the changes staged and ready for another commit
git reset --soft HEAD~2
git commit -m "Some feature"
Rebase local branch with remote master
# while on the local branch
git fetch origin
git rebase origin/master
Replace messed up local branch with remote master
git reset --hard origin/master
Delete branches
# delete local branch
git branch -d <branchName>
# delete remote branch
git push origin --delete <branchName>
# git clone with submodules
git clone --recursive <submodule repository>
# update submodule from upstream
git submodule update --remote <submodule>
# To pull in changes from submodules after the root repository remote has been updated
git pull
git submodule update
Use rSync to sync files over ssh
# note the trailing slash at the end of `~/Desktop/` to indicate all contents
rsync -avzP ~/Desktop/ awesome@192.168.1.1:/home/awesome/files
Also can use scp
scp -r user@192.168.1.1:/home/data/ ~/Desktop/
while true; do npm start; done
SOME_VAR="Hello"
echo "${SOME_VAR}" # 'Hello'
echo '${SOME_VAR}' # '${SOME_VAR}'
echo `${SOME_VAR}` # '-bash: Hello: command not found'
- Immediately exits script on error
- Performs clean up on error
#!/bin/sh
# exit the shell script on error immediately
set -e
TEMP_DIR="SOME_TEMP_DIR"
function cleanUp() {
rm -rf "${TEMP_DIR}"
}
# Set up cleanUp to trigger on EXIT
trap cleanUp EXIT
mkdir -p "${TEMP_DIR}"
# Do things
# Error here will trigger `cleanUp()` and exit the script immediately
echo 'I am awesome'
# Done, remove the cleanup trap
trap - EXIT
# Manual Cleanup
cleanUp
source
runs the script in the current process, does not require the script to be an executable
$ source some_script
$ . some_script # same as the above
./some_script
spawns a new shell and executes the script, according to the shebang line
bash some_script
also spawns a new shell and executes the file with bash
A; B # Run A and then B, regardless of success of A
A && B # Run B if A succeeded
A || B # Run B if A failed
A & # Run A in background.
do_stuff > /dev/null 2>&1
Redirects stdout
to /dev/null
, redirects stderr
to stdout
Helpful snippet to test network connections that do not respond to cUrl
(echo > /dev/tcp/$HOST/$PORT) # returns a status code of whether the connection is successful
echo $? # prints the status code of the last command
lsof -i tcp:8002
Copy stdout to clipboard
$ echo 'Hello!' | pbcopy
Prevent OSX from sleeping
$ caffeinate
# Or prevent sleep until script has completed
$ caffeinate long_running_script.sh
Creates a HTTP server for files in the local directory. Either an index.html
file (if present) or the other files in the directory wil be served.
$ python -m SimpleHTTPServer 8080
Find files by filename or wildcard
$ find ./test -name "*.php"
$ wget -r -N http://example.com
Shortcut | Purpose |
---|---|
ctrl + a |
Go to the beginning of the line |
ctrl + e |
Go to the end of the line |
ctrl + u |
Clear everything before the cursor |
ctrl + w |
Delete the word before the cursor |
ctrl + l |
Clear the screen |
# show hex and ascii values
$ xxd some_file.mp3
# DNS address lookup
$ nslookup google.com
# show local cached IP-MAC address table
$ arp -a
# copy contents of one directory to another
$ cp directory_with_desired_contents/* ~/destination_directory
Shortcut | Purpose |
---|---|
⌘ + ⌃ + ⇧ + 4 | Select area and save to clipboard |
⌘ + ⇧ + 4 | Select area and save to desktop |
⌘ + ⌃ + ⇧ + 3 | Save screen to clipboard |
⌘ + ⇧ + 3 | Save screen to desktop |
While selecting area, press space
to select a window.
function createObject(objectParams) {
const prototype = {
instanceMethod(params) {
return params;
}
};
const instance = Object.create(prototype);
const instanceProps = {
data: objectParams.data
};
return Object.assign(instance, instanceProps);
}
See also:
python -i hello.py
def foo(*args):
def bar(*args):
return args
return bar(*args)
>>> foo(1,2,3) # (1, 2, 3)
MongoDB Authentication
# first login with admin credentials
use admin
db.auth('admin', 'password')
use myDatabase
# create a user local to `myDatabase`
db.createUser({ user: "myuser",
pwd: "password",
roles: [
"readWrite"
]
})
Connect to the database using the following:
MONGO_URL=mongodb://${DATABASE_USER}:${DATABASE_PASSWORD}@${MONGO_IP}/${DATABASE_NAME}
Rebuild and recreate all containers
docker-compose up -d --force-recreate --build
On host (Mac):
ipconfig getifaddr en0
Benchmark a command
perf stat -r 10 $command