All posts

My Ubuntu/Mac Command Cheatsheet

August 25, 2022
tutorial

This is a simple Ubuntu command cheatsheet for myself.

GPU

$ nvidia-smi
Thu Jun 22 11:28:07 2023       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.116.04   Driver Version: 525.116.04   CUDA Version: 12.0     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ...  On   | 00000000:67:00.0 Off |                  Off |
|  0%   48C    P8    29W / 450W |   3997MiB / 24564MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A     43171      C   python3                          3994MiB |
+-----------------------------------------------------------------------------+
  • Check GPU usage using nvtop: install the package using sudo apt install nvtop and then run nvtop

File System

  • ps means process status:

    • -u: list all processes by user name:

    • -e: view all the running processes

    • -f: view full-format listing

      ps -u hjwang
      PID TTY          TIME CMD
      3558 ?        00:00:02 sshd
      3559 pts/0    00:00:00 bash
      ...
      22112 ?        01:17:52 python

    |: pipe a command that lets you use two or more commands such that output of one command serves as input to the next. grep is a command-line utility for searching plain-text data sets for lines that match a regular expression.

    ps -ef | grep python can be use to show all running processes with python.

  • kill PID: terminate a process using its ID: kill 22112 (gracefully) or kill -9 22112 (forcefully and immediately)

  • du -sh *: list the sizes of all files and sub-folders.

  • tree: print the directory tree in terminal: install using sudo apt-get install tree

OS

  • Show OS version: lsb_release -a

Python

Change pip source:

pip config set global.index-url https://mirrors.aliyun.com/pypi/simple
Writing to /home/sedy/.config/pip/pip.conf

VPN

Get link from https://airtcp6.com/

mkdir clash
cd clash
wget https://github.com/Dreamacro/clash/releases/download/v1.18.0/clash-linux-amd64-v1.18.0.gz
gzip -d clash-linux-amd64-v1.18.0.gz
mv clash-linux-amd64-v1.18.0 clash
chmod +x clash
wget -O config.yaml https://airchats.xyz/link/xxlFHIZsFzxkBaBHlapx?clash=1
./clash -d .

SFTP

https://linuxhint.com/setup-sftp-server-ubuntu/

sudo apt install ssh
sudo vim /etc/ssh/sshd_config

add the following to the end of the file:

Match group sftp
ChrootDirectory /home
X11Forwarding no
AllowTcpForwarding no

ForceCommand internal-sftp will disable SSH, which should not be included.

restart, add group, add/modify user, change permission

sudo systemctl restart ssh
sudo addgroup sftp
sudo useradd -m new_user_john -g sftp
sudo passwd new_user_john
sudo usermod -a -G sftp existing_user_tom
sudo chmod 700 /home/new_user_john
sudo chmod 700 /home/existing_user_tom/

Use FileZilla to connect.

PS. The featured image for this post is generated using Stable Diffusion, whose full parameters with model link can be found at Takin.AI.

Mac

add environment variables: `vim ~/.bash_profile``

add to file: `export OPENAI_API_KEY=sk-xxx``

View environment variables: printenv

Process

  • Control + Z to suspend a process - it is still running in the background

  • fg to continue the process

  • jobs to see all suspended jobs

    [1]  + suspended  scrapy crawl ssrn
    [2]  + suspended  scrapy crawl ssrn
  • kill %1 to terminate a suspended job. %1 is the index.

Git

Git Reset and Force Push

To revert to a previous commit and force the remote repository to sync with your local repository:

  1. First, find the commit hash you want to revert to:

    git log
  2. Reset to that commit (replace COMMIT_HASH with the actual hash):

    git reset --hard COMMIT_HASH
  3. Force push to remote repository:

    git push -f origin main

    ⚠️ Warning: Force push rewrites the remote history. Only use it when you're sure and have communicated with your team.