zsh

There are many shells one can use, Mac default is zsh (located at /bin/zsh). I set up PyCharm to use it as well in the Preferences\Tools\Terminal\Shell Path

Bash/zsh comment is the same as python: #

# To open Pycharm/VSCode from terminal:
open -na "PyCharm.app" .
open -na "Visual Studio Code.app" .
# To open a finder from terminal:
open .

To open terminal from folder I added a shortcut: Control + Alt + Shift + T

To see hidden files on Mac in finder: Command + Shift + .

Variable

variable=<some_value>  # make sure there are no spaces around =
variable=solar

To replace chars inside a string:

repo_name="home/solar"
echo $repo_name
repo_name=${repo_name//[\/l]/_}
echo $repo_name

ls command

To count files in a folder from terminal:

ls /etc | wc -l

recursively:

find <directory> -type f | wc -l

for example:

ls '/Users/nenad.bozinovic/work/Frame/elasticity_logs' | head -4

to see file sizes:

ls -l
ls -l --block-size=M

to see hiden files:

ls -a
ls -la

To create/delete directory:

mkdir
rm -r  # delete non empty directory
rmdir # to delete empty directory
rm -d  # to delete empty directory

mv source dst # to move and/or rename directory

tree command

To see tree of directories use tree command:

tree --help

To install on Linux:

!sudo apt install tree

to install on Mac:

brew install tree

Zip/Unzip gz files

To zip/unzip gz:

Install gnu-tar if seeing warning with the tar, then use gtar:

brew install gnu-tar

To zip:

gtar -zcvf myfolder.tar.gz myfolder
gzip filename  # zip it back
gzip -k filename  # to zip it and keep original

to see the content of a zipped file:

gtar -tf myfolder.tar.gz

to unzip:

tar -xf labeled_data.tar.gz
tar -xf labeled_data.tar.gz -C /home/user/destination

Zip/Unzip zip files

zip filename.zip file1.txt file2.txt file3.txt
unzip -vl titanic.zip  # to see content without unzipping
unzip filename.zip
unzip filename.zip -d /home/user/destination

Download

To download a file from URL:

!curl -OL <URL>

Paths

Print path:

echo "${PATH//:/$'\n'}"

To add folder to PATH

Homebrew

Homebrew is a package manager for macOS, it might have some unique packages that pip doesn’t have, to install wget for example:

brew install wget
wget https://your.link.png

pip

To install package:

pip install torch torchvision tensorboard

When using [] with pip it is important to use "" to avoid shell parsing for example:

pip install "mpl_interactions[jupyter]"

To see version of the package installed:

pip show torch

To see all packages installed:

pip list

vim

Command Explanation
i insert mode
Esc exit insert mode
:w Saves the file you are working on
:w [filename] Allows you to save your file with the name you’ve defined
:wq Save your file and close Vim
:q! Quit without first saving the file you were working on

Conda

yes | conda create -n $repo_name python=$python_ver  # notice there is no keyword env
conda env list
conda env remove -n ENV_NAME
conda env export > env.yml
conda env create -n ENVNAME --file ENV.yml
conda list torch

if statement in zsh

if ! [[ -n $repo_name ]] || ! [[ -n $python_ver ]]; then  
  echo "you didn't enter repo name and/or python version"  
  exit 0  
fi

Get IP address

To be able to use ip:

brew install iproute2mac

Before you generate ssh key pair check if you have one:

cat ~/.ssh/id_rsa.pub

Generate ssh key pair

To generate ssh key pair:

ssh-keygen -t rsa

this will ask you to generate a new key in a specified file. Yuo should see id_rsa and id_rsa.pub files that relate to private and public keys.

To copy the key, replace the user and server with your username and the server address you wish to use the key authentication on (use commands hostname and whoami for hostname and username):

ssh-copy-id -i ~/.ssh/id_rsa.pub user@server

List of basic linux commands

Python version

which python

Gives for example: /opt/conda/bin/python

Copy files

Use scp. For example:

scp test.json root@ip_address:/home/solar

For folders add -r:

scp -r test_folder root@ip_address:/home/solar/test_folder

Delete files

rm file1.txt file2.txt

Username

useradd username
passwd username
login username
whoami

IP Address

To get IP address on Linux, use curl this:

apt install curl
curl ifconfig.me
curl checkip.amazonaws.com

Get system, host, and user names

uname -mrs
hostname
whoami

To get a hostname in Jupyter:

hn=!hostname
hn[0]
'Jupiter-2.local'

Concatenate strings

VAR1="Hello, "
VAR2=2
VAR3=" Worlds"
VAR4="$VAR1$VAR2$VAR3"
echo "$VAR4"

Executable file:

chmod +x some_file.ext 

Check if there is x in front of the name with:

ls -l