Skip to content

Instantly share code, notes, and snippets.

@ridhwaans
Last active April 13, 2023 18:36
Show Gist options
  • Save ridhwaans/243a72c398685f3c64ffdb8a6341a719 to your computer and use it in GitHub Desktop.
Save ridhwaans/243a72c398685f3c64ffdb8a6341a719 to your computer and use it in GitHub Desktop.
terminal-notes
# cheat_sheet.org.sh
# The contents of this file are released under the GNU General Public License. Feel free to reuse the contents of this work, as long as the resultant works give proper attribution and are made publicly available under the GNU General Public License.
# Best viewed in emacs org-mode.
# Alternately, one can keep this cheat sheet handy by adding the following line to ~/.bashrc:
#
# alias cheatsheet="less ~/path_to_cheat_sheet.org.sh"
* Reference:
** Basics:
*** Getting help:
# View the manual for target command
man command
# Get help with a target command (probably the same as above, but not always):
command -h
# In case you forget the name of a command, print possible commands relating to any given word:
apropos word
# View index of help pages:
info
*** Command Line Utilities:
**** Basic File and Directory Operations:
# Print current working directory:
pwd
# Show files in current directory:
ls
# Show maximum information about all files, including hidden:
ls -a
# Recurse into subdirectories and list those as well:
ls -R
# List files by modification time, most recent first.
ls -lt
# Move/rename a file or directory (be careful that you don't move the source over a destination with the same name):
mv source destination
# Delete target forever (be very careful), use -r recursive flag for directories:
rm target
# Copy file or directory:
cp source destination
# Mount filesytem:
mount /dev/device_name /media/device_name
# Unmount:
umount /media/device_name
# Forensically clone filesystems and do other low-level operations on files. Be careful with this one. Can be destructive:
dd
# Work with disk partitions:
parted
# Filesystem creation tool:
mkfs
**** System Administration:
# Execute command as an administrator (can be destructive/insecure. Use only for system administration tasks):
sudo command
# Become system administrator:
sudo -s
# Quit system administration:
exit
# Forgot to type sudo in front of a command and already hit enter? Repeat the last command using sudo:
sudo !!
***** Installing software from a .tgz (also known as a tarball):
# First, unzip the tarball (see section on tar, below)
# Next, move into unzipped directory:
cd software_directory
# Always read README first if it is provided, in case there are any modifications to the procedure outlined below:
cat README
# Automatically check for appropriate configurations and generate a MAKE file in the directory:
./configure
# Compile software. May require sudo:
make
# Move files into their appropriate locations. May also require sudo:
make install
# Clean up files in directory, in case make command fails, or just to remove unnecessary cruft:
make clean
***** Ubuntu/Debian Software repositories:
# Check distro repositories for software updates:
sudo apt-get update
# Download and install updates (update first):
sudo apt-get upgrade
# Search for package in the repositories:
apt-cache search keyword
# Get more detail on one specific package:
apt-cache show package_name
# Download and install a package:
sudo apt-get install package_name
# View the output of a command in a more convenient format:
command | less
**** Working With Files:
# Print a file in terminal:
cat file
# Find files matching filename:
locate filename
# See the version of a program or the location of the program
which appname
# Search through filename for matches to phrase:
grep phrase filename
# Search through output of a command for phrase:
command | grep phrase
**** Working With Processes:
# List all running processes:
ps -e
# Standard system monitor showing a more extensive view of all processes and system resources:
top
# Like top, but with a better, cleaner interface:
htop
# Stop a process from using all system resources and lagging computer:
renice process_name
# Kill misbehaving process (use sparingly, last resort, try 'renice' command first):
pkill process name
# Start a process in the background
command &
# Start a process in the background and have it keep running after you log off
nohup command &
**** Compression and Encryption:
# Make a simple compressed backup of files or directories:
tar -cvzf backup_output.tgz target_files_or_directories
# Open a compressed .tgz or .tar.gz file:
tar -xvf target.tgz
# Encrypt a file:
gpg -o outputfilename.gpg -c target_file
# Decrypt a file:
gpg -o outputfilename -d target.gpg
# Zip and encrypt a directory simultaneously:
gpg-zip -o encrypted_filename.tgz.gpg -c -s file_to_be_encrypted
*** The Bash shell:
**** File Name expansions:
# Current user's home directory:
~/
# Current directory:
./
# Parent directory:
../
# Or even (Two parent directories down):
../../
# All files in target directory. (Be very careful.):
/*
**** Output Redirects:
# Redirect output of one command into the input of another with a pipe:
command_1 | command_2
# Or even:
command_1 | command_2 | command_3
# Redirect output to a file:
command > file
# Or:
file > file
# Or even, to redirect in a different direction:
file < file
# Append output rather than writing over the target file:
file_or_command >> file
# Works like |, but it writes output to both target and terminal:
tee target
# Redirect standard output and error to /dev/null, where it is deleted.
command > /dev/null 2>&1
**** Controlling Execution:
# Wait until command 1 is finished to execute command 2
command_1 ; command_2
# Or even:
command_1 ; command_2 ; command_3
# && acts like ; but only executes command_2 if command_1 indicates that it succeeded without error by returning 0.
command_1 && command_2
# || acts like && but only executes command_2 if command_1 indicates an error by returning 1.
command_1 || command_2
**** Bash Wildcards:
# Zero or more characters:
*
# Matches "phrase" and any number of trailing characters:
phrase*
# Matches any incidences of "phrase" with any trailing or leading chars:
*phrase*
# Matches any one char:
?
# Matches any of the characters listed inside brackets:
[chars]
# Matches a range of chars between a-z:
[a-z]
** Advanced:
*** Command Line Utilities, Continued:
**** Networking:
# Configure network interfaces:
ifconfig
# Configure wireless network interfaces:
iwconfig
# Connect to a remote server.
ssh username@ip_address
# Forward X from target to current machine (Get a remote desktop. Somewhat obscure, but very useful):
ssh -X username@ip_address
# Copy files/directory over the network from one machine to another recursively:
scp -r source_filename:username@ip_address target_filename:target_username@target_ip_address
# Copy only changes between files or directories (super efficient way to sync directories, works either locally or with remote servers using username@ip_address:optionalport, just like ssh):
rsync source target
# Check to see if target is online and responding
ping ip_address
# View network route to target:
traceroute6 ip_address
# Network Monitor
netstat
# View firewall rules
iptables -L
# Scan this machine(localhost) to check for open ports:
nmap localhost
***** wget:
# download a file over http:
wget http://example.com/folder/file
# complete a partially downloaded file:
wget -c http://example.com/folder/file
# start download in background:
wget -b wget -c http://example.com/folder/file
# download a file from ftp server:
wget --ftp-user=USER --ftp-password=PASS ftp://example.com/folder/file
***** netcat:
# Listen for input from network on recieving_port, dump it to a file (insecure, but handy):
netcat -l recieving_port > file_copied
# Pipe the output of a command to a target ip and port over the network:
command | netcat -w number_of_seconds_before_timeout target_ip target_port
# Use tar to compress and output a file as a stream, pipe it to a target ip and port over the network:
sudo tar -czf - filename | netcat -w number_of_seconds_before_timeout target_ip target_port
**** Users and Groups:
# Change owner of a file or directory:
chown user_name:group_name directory_name
# Change privileges over file or directory (see man page for details.)
chmod
# Create a new user:
adduser
# Change user privileges (be very careful with this one):
usermod
# Delete user
deluser
# Print groups:
groups
# Create a new group:
groupadd
# Change group privileges:
groupmod
# Delete group:
delgroup
# Temporarily become a different user:
su username
# Print usernames of logged in users:
users
# Write one line to another user from your terminal:
talk
# Interactive talk program to talk to other users from terminal (must be installed from repositories.):
ytalk
**** Working With Files, Continued:
# View what processes are using what files:
lsof
# View the differences between two files:
diff file_1 file_2
# Output the top number_of_lines of file:
head -n number_of_lines file
# Like head, but it outputs the last -n lines:
tail -n number_of_lines file
# Checksum a file:
md5sum file
# Checksum every file in a directory (install this one from repositories.):
md5deep directory
# Checksum a file (better algorithm with no hash collisions):
sha1sum
# Same operation as md5deep, but using sha1:
sha1deep
# Call command every few number_of_seconds, and highlight difference in output:
watch -d -n number_of_seconds command
# Execute command, print how long it took:
time command
# View files in directory from largest to smallest:
du -a directory | sort -n -r | less
# remove spaces from filenames in current directory:
rename -n 's/[\s]/''/g' *
# change capitals to lowercase in filenames in current directory:
rename 'y/A-Z/a-z/' *
***** Environment and Hardware:
# print motherboard information
dmidecode
# Print full date and time:
date
# Print the hostname of this machine:
echo $HOSTNAME
# Print information about current linux distro:
lsb_release -a
# Or even:
more /etc/issue
# Print linux kernel version:
uname -a
# Print information about kernel modules:
lsmod
# Configure kernel modules (never do this ;p ):
modprobe
# View Installed packages:
dpkg --get-selections
# Print environment variables:
printenv
# List hardware connected via PCI ports:
lspci
# List hardware connected via USB ports:
lsusb
# Print hardware info stored in BIOS:
sudo dmidecode
# Dump captured data off of wireless card:
dumpcap
# Dump info about keyboard drivers:
dumpkeys
***** Ubuntu System Administration, Advanced (Continued):
# Add a Personal Package Archive from Ubuntu Launchpad:
add-apt-repository
# Install a .deb file from command line:
sudo dpkg -i package.deb
**** Python:
# Update pip (Python package manager):
pip install -U pip
# search pip repos for a library:
pip search library_name
# create a virtual python environment to allow install of many different versions of the same Python modules:
virtualenv dirname --no-site-packages
# connect to a virtual python environment
source dirname/bin/activate
# disconnect from a virtual python environment:
deactivate
# install package into virtual python environment from outside:
pip install packagename==version_number -E dirname
# export python virtual environment into a shareable format:
pip freeze -E dirname > requirements.txt
# import python virtual environment from a requirements.txt file:
pip install -E dirname -r requirements.txt
**** git (all commands must be performed in the same directory as .git folder):
# Start a new git project:
git init
git config user.name "user_name"
git config user.email "email"
# Make a copy of a git (target can be specified either locally or remotely, via any number of protocols):
git clone target
# Commit changes to a git:
git commit -m "message"
# Get info on current repository:
git status
# Show change log for current repository:
git log
# Update git directory from another repository:
git pull [target]
# Push branch to other repository:
git push [target]
# Create a new branch:
git branch [branchname]
# Switch to target branch:
git checkout [branchname]
# Delete a branch:
git branch -d [branchname]
# Merge two branches:
git merge [branchname] [branchname]
# Show all branches of a project:
git branch
# Compare a file between two different branches
git diff [current_branch_name] [other_branch_name] -- file_name_and_path
# Restore a file from a different branch
git checkout [other_branch_name] -- file_name_and_path
# List filenames in a diff or show
git show|diff --name-only
# Checkout forked branch of repo without cloning fork
cd my-repo
git remote add forked-version [fork-url-here]
git fetch forked-version
git checkout [branch-name-here]
# Setup forked repo for work and sync with original repo
## watching forked repo to original repo
git remote add FORKED_OWNER git@github.com:FORKED_OWNER/FORKED_REPOSITORY.git
## watching original repo in forked repo
$ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
## pull new updates from original repo into forked repo
git fetch FORKED_OWNER <or> upstream
git fetch --all
git merge upstream/master master
git rebase upstream/master
git reset --hard upstream/master
# List of filenames modified by most recent commit on current branch
git rev-parse HEAD | xargs git diff-tree --no-commit-id --name-only -r
## Reload local .gitignore which also removes untracked files upstream
git rm -r --cached .
git add .
git commit -m [suitable message]
## Git tag & release
git tag
git add .
git commit -m "Tag [new_version] [previous_commit_hash]"
git tag [new_version]
git push origin master
git push origin --tags
*** Virtualization:
#clone a virtual machine (this works, it's been tested):
vboxmanage clonehd virtual_machine_name.vdi --format VDI ~/target_virtual_machine_name.vdi
#mount a shared virtual folder:
#you need to make sure you have the right kernel modules. You can do this with modprobe, but this package works instead in a ubuntu-specific way.
sudo apt-get install virtualbox-ose-guest-utils
sudo mount -t vboxsf name_of_shared_folder_specified_in_Virtualbox path_of_mountpoint
*** mysql:
# Get help:
help
# Show databases:
show databases;
# Choose a database to use:
use database_name_here;
# Show database schema:
show tables;
# Delete database:
DROP DATABASE databasename;
# New database:
CREATE DATABASE databasename;
# Create a new user:
CREATE USER username@localhost IDENTIFIED BY 'password';
# Show users:
select * from mysql.user;
# Delete a user:
delete from mysql.user WHERE User='user_name';
# Give user access to all tables (make them root). the "%" means that they can sign in remotely, from any machine, not just localhost.:
grant all privileges on *.* to someusr@"%" identified by 'password';
# give certain privileges to a user on a certain database:
grant select,insert,update,delete,create,drop on somedb.* to someusr@"%" identified by 'password';
# Tell mysql to use new user priv policies:
flush privileges;
# change user password:
use mysql;
update user set password='password'('newpassword') where User='user_name';
# mysql command line args:
# export text file with commands to rebuild all mysql tables:
mysqldump databasename > dumpfilename.txt
# restore from a dump:
mysql -u username -p < dumpfilename.txt
# dump entire database:
mysqldump -u username -p --opt databasename > dumpfile.sql
# restore from entire database dump:
mysql -u username -p --database=databasename < dumpfile.sql
**** Notes:
# misc:
monit usually restarts pushback unicorn (i.e. to apply changes after vimming database config):
`/usr/sbin/monit stop unicorn` and `/usr/sbin/monit start unicorn`
`launchctl list | grep docker`
telnet towel.blinkenlights.nl # ascii star wars
ridhwaans@ubuntu:~$ skype &
# curl response time:
`curl -s -w "%{time_total}\n" -o /dev/null <http://endpoint>`
# remove all symlinks from directory:
```sudo find {DIR_PATH} -maxdepth 1 -type l | xargs rm```
# find all symlinks in directory:
```find . -maxdepth 1 -type l -ls```
# ports
`sudo netstat -tulpn` or `lsof -i :portNumber` - find pid running on port
example -> `lsof -wni tcp:3000` - find pid running at port 3000
ridhwaans@ubuntu:~$ ps aux | grep skype
1000 3223 0.4 1.5 443828 128148 ? Sl Mar07 4:52 skype
1000 15821 0.0 0.0 8112 932 pts/2 S+ 13:16 0:00 grep --color=auto skype
ridhwaans@ubuntu:~$ kill -9 3223
ridhwaans@ubuntu:~$ ps aux | grep skype
1000 15823 0.0 0.0 8108 932 pts/2 S+ 13:16 0:00 grep --color=auto skype
# bash variables:
```
al$ foo="Hello, World"
al$ echo $foo
Hello, World
al$ bar="Goodbye"
al$ export foo
al$ bash
bash-3.2$ echo $foo
Hello, World
bash-3.2$ echo $bar
bash-3.2$
```
`\` to escape characters, quotes and bash variables
control R search for previous commands (reverse-i-search)
control C sends SIGINT interrupt to abort application
control Z sends SIGTSTP suspends to background, can retrieve to foreground by 'fg'
control D is EOF, steps through code in ruby binding.pry, saves files during cat >> file, exits python and irb
control U clear line command
Control (or Ctrl) ⌃, Option (or Alt) ⌥
mac modifier keys https://support.apple.com/en-ca/HT201236
# virtualenv note:
if changing versions via rbenv or pyenv local/global/shell commands doesnt change the ruby or python -v version, remove ~/.rbenv/shims/.rbenv-shim and try again
# heroku:
heroku restart; heroku pg:reset DATABASE; heroku run rake db:migrate
heroku restart -a timeclock-app; heroku pg:reset DATABASE -a timeclock-app; heroku run rake db:migrate -a timeclock-app
heroku ps:exec -a homehost-demo
heroku logs --tail -a homehost-demo
# git:
git reset --hard origin/master
--soft
Does not touch the index file or the working tree at all (but resets the head to <commit>, just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it.
> git rebase origin/branch
> git rebase local-branch
Squash my last X commits together
> git rebase -i HEAD~x (x is # of commits back)
pick first commit from top
squash (or s) remaining commits from the bottom up
:wq and rename commit message
Remove a specific file from the most recent local commit and commit back
> git reset --soft HEAD^1
> git rm --cached <file-name>
> git commit -m "<your-message>"
Undo a git reset
> git reset 'HEAD@{1}'
# errors:
Possible DNS spoofing detected error-> delete offending line in ~/.ssh/known_hosts and retry
https://www.digitalocean.com/community/tutorials/how-to-use-ps-kill-and-nice-to-manage-processes-in-linux
https://stackoverflow.com/questions/7953806/github-ssh-via-public-wifi-port-22-blocked
http://blog-mazhar.rhcloud.com/reset-the-mysql-root-password-in-osx/
# psql:
As the default configuration, a user called postgres is made and the user postgres has full superadmin access to the entire PostgreSQL instance running on your OS.
The default Postgres user is postgres and a password is not required for authentication. Sqlite3 is its default database.
Thus, to add a password, we must first login and connect as the postgres user
> apt install postgresql postgresql-contrib
> sudo vi /etc/postgresql/12/main/postgresql.conf
(or pg_hba.conf for client authentication)
> psql
psql: error: could not connect to server: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
> psql -V or psql --version
> pg_lsclusters
*shows the list of postgres server instances*
> sudo service postgresql start
* Starting PostgreSQL 12 database server
> psql
psql: FATAL: database "ridhwaans" does not exist
> psql postgres
psql: error: could not connect to server: FATAL: role "ridhwaans" does not exist
> sudo psql postgres
[sudo] password for ridhwaans:
psql: error: could not connect to server: FATAL: role "root" does not exist
> sudo psql -U postgres -p 5432
psql: error: could not connect to server: FATAL: Peer authentication failed for user "postgres"
> sudo -u postgres psql -p 5432
psql (12.2 (Ubuntu 12.2-4))
Type "help" for help.
postgres=#
list roles
postgres=# SELECT rolname FROM pg_roles;
list users & their role
postgres=# \du
list databases
postgres=# \l
list tables
postgres=# \dt
connect/switch to database (by name)
postgres=# \c
quit psql
postgres=# \q or exit
# chmod:
x-xxx-xxx-xxx file
(directory d OR file - OR symlink l)-(user rwx)-(group rwx)-(global rwx)
chmod u=+r-x filename(+ is add permission, - is remove permission)
octal example - chmod 111 7 rwx for 'user' position and so on
# cronjob:
Min Hour Day Mon Weekday
* * * * * command to be executed
┬ ┬ ┬ ┬ ┬
│ │ │ │ └─ Weekday (0=Sun .. 6=Sat)
│ │ │ └────── Month (1..12)
│ │ └─────────── Day (1..31)
│ └──────────────── Hour (0..23)
└───────────────────── Minute (0..59)
0 * * * * every hour
*/15 * * * * every 15 mins
0 */2 * * * every 2 hours
0 0 * * 0 every Sunday midnight
@reboot every reboot
Source: https://devhints.io/cron
# language package managers:
npm install (package.json)
pip install -r (requirements.txt)
bundle install (Gemfile)
ncu -u (package.json)
pur -r (requirements.txt)
bundle update --all (Gemfile)
# Python install dependencies
https://devguide.python.org/getting-started/setup-building/index.html#install-dependencies
# Jira queries (JQL)
## List all issues I am watching
watcher = currentUser() AND resolution = Unresolved ORDER BY priority DESC, updated DESC
## My Current Tickets without Estimates
Sprint in (openSprints()) AND "Story Points" is EMPTY AND assignee = currentUser()
## My Future Tickets without Estimates
Sprint in (futureSprints()) AND "Story Points" is EMPTY AND assignee = currentUser()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment