artimys
4/14/2017 - 5:46 PM

Terminal Cheatsheet

Useful commands to navigrate Mac/Linux terminal.

preview file content

cat path_to_file    # use for short files
less path_to_file   # use for larger files, page up/down
tail -f production.log  # follow file in real time

edit file

nano path_to_file

# or

vi path_to_file     # to insert text, enter mode (press i)
                    # to save (press ESC, :, type wq or x)
                    # to exit w/o save (press ESC, :, type q!)
                    # to select lines, enter mode (press v) 
                    #   -> press 'd' to delete line
                    #   -> press 'y' to copy

search

# display a file's content and pass it on to grep for searching
cat file-path | grep some-text

sed command

# Note: works on linux but might not on Mac
# -i option: inline replacement
# a after reg exp means it will place new line after match

sed -i '/^RegexToMatch/a newLineTextHere' fileToModify.txt

host file

sudo vi /private/etc/hosts
sudo vi /etc/hosts # (ln to /private/etc/hosts)

symbolic links

ln -s /Users/arturo/Documents/rails-app railsapp
unlink summit2016   # to remove symlink
ls -la              # to view files, hidden and symlinks

shell

cat /etc/shells         # view available shells
chsh -s $(which zsh)    # set default shell to zsh
echo $SHELL             # check current shell

shell - BASH

bash --version          # version
vi ~/.bash_profile      # customize shell
source ~/.bash_profile  # reload bash without restarting terminal

shell - ZSH

zsh --version           # version

ssh

ssh user@website.com
ssh -i 'private-key-file' user@website.com    # specificy which ssh key to use

scp

download file to local computer

# . means local current directory - linux
# ~ means local computer's home directory - windows 7
scp user@website.com:~/path_to_file_on_remote_server .
scp user@website.com:/path_to_file_on_remote_server .

view TCP ports

# Works on MacOS Sierra
sudo lsof -i -n -P | grep TCP

# To kill process, insert pid from above command
# sudo kill {pid}

or

lsof -n -i4TCP:{port}
ex. lsof -n -i4TCP:9292
ex. lsof -n -i4TCP:5434 -- psql

kill -9 pid

view history of commands in Terminal

$ history

# To view command history with timestamps, 
# add line to .bash_profile and reload Terminal.
# Then run the `history` command again
export HISTTIMEFORMAT="%m/%d/%y %T "

mount network folder to local filesystem

First create a new folder path that will be used link the networkfolder/shared drive.

Note: be sure to disconnect from the network folder first

# mount_smbfs //user@server/folder-path ~/new-path-locally

# example (def works on MacOS Mojave)
$ mount_smbfs //arty@10.0.0.37/Junk ~/mounty/junky

# to view what is currently mounted
$ df

un-mount network folder from local filesystem

Note: make sure iTerm is not on local path that will be unmounted

umount ~/mounty/junky
# or
diskutil unmount ~/mounty/junky