Bash commands are mainly supported in MacOS, Linux but also support in Windows. You can use integrated tools for using bash on these platforms.

Tools

Hotkeys

  • Ctrl + C : interrupt current tasks.
  • Ctrl + L : clear the screen.
  • Tab : autocomplete the commands / directories / file names / ….
  • Ctrl + Shift + V : paste from clipboard.
  • For a long list: Enter to continue to read, q to quit.

Multiple commands

Run at once,

command_1 && command_2

Using script: create a script, e.g. multiple_commands.sh, and run sh multiple_commands.sh

#!/bin/sh
echo 'some info'
command_1
command_2

Check info

System

# DISK SPACE
df -h
# CPU & RAM
cat /proc/cpuinfo
cat /proc/meminfo
# MEM USAGE
free -m
# ALL ENV
printenv

Folders / Files

# CHANGE ACTIVE DIR
cd <dir>
cd # to the startup dir
cd / # to root
cd .. # to father dir
# CREATE NEW FOLDER
mkdir <dir>
# LIST
ls 
ls -a # including hidden
# CURRENT PATH
pwd
# FOLDER/FILE SIZE
du -hs <directory / file>
# `h` : human readable (6.7G)
# `s` : only this directory
# REMOVING
rm <file>
rm -f <file> # force to remove
rm -rf <dir> # remove folder
rmdir <empty-dir> # remove empty
# COMPRESS
zip file.zip file/folder
unzip file.zip # decompress

Permission

# CHECK PERMISSION
ls -l 
ls -l <file>
# ADD existing USER to existing GROUP
sudo usermod -a -G groupName userName
# CHANGE PERMISSION
chown <user>:<group> file
chown -R thi:root folder # folder & children

Network

# CHECK IP
ifconfig
ipconfig # windows
# INTERNET SPEED (need python)
curl -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | python -
# DOWNLOAD A FILE
wget https://website.com/filename.ext

Text file

# QUICK LOOK CONTENT
more file.txt
cat file.txt
# JUST CREATE
touch file.txt
# CREATE + MODIFY
nano file.txt # Ctrl+X to quit
vim file.txt # ESC, :q to quit
# SEARCH STRING
grep "string" file.txt
# ADD A LINE TO A FILE WITHOUT OPENNING IT
echo "hello 'thi' world" >> my_file.txt

Alias

Create your own “alias” command for short,

# CREATE
alias yourAlias='cd /usr/'
alias yourAlias=cd /usr/ # windows
# CALL
yourAlias

# LIST OF ALIASES
alias
  • Linux / MacOS: Add your alias to .bash_aliases (in home dir, printenv HOME) if you wanna store your alias permanently.
  • Windows: Using cmder (its setting file), add more aliases to <cmder-install>/config/user_aliases.cmd. You can also add (automatically) on the cmder UI, it adds them for you to the .cmd file.

    raimon=ssh -p 15424 [email protected]
    sshnotebook=ssh -N -L localhost:8765:192.168.0.155:9889 [email protected] -p 15424
    dat=cd dinhanhthi.com
    nt2=cd notetheme2
    serve=bundle exec jekyll serve -I
    build=bundle exec jekyll build
    condafix=set CONDA_DLL_SEARCH_MODIFICATION_ENABLE=1
    playground=cd C:\Users\dinha\Documents\GitHub\dataswati\git\playground
    aquassay=cd C:\Users\dinha\Documents\GitHub\dataswati\git\aquassay
    bronze-alu=cd C:\Users\dinha\Documents\GitHub\dataswati\git\bronze-alu
    soufflet=cd C:\Users\dinha\Documents\GitHub\dataswati\git\soufflet
    popai=cd C:\Users\dinha\Documents\GitHub\dataswati\python-dataswati\popai
    

Copy / Cut / Paste

# MOVE
mv <old-dir> <new-dir>
move <old-dir> <new-dir> # windows
# RENAME a file/folder
mv olname.txt newname.txt
# COPY
cp file file
cp -r file&dir file&dir

Vim

SSH

Commands

ssh [email protected]_host
ssh [email protected]_host -p remote_port
# CHECK VERSION
ssh -V

# DISCONNECT
exit
# COPY FILE: LOCAL -> REMOTE
scp local_file [email protected]:/var/tmp/

# REMOTE -> LOCAL
scp [email protected]:/usr/local/bin/add.sh .

How it works?

  1. Local creates public_key (id_rsa.pub) & private_key (id_rsa).
  2. Only private_key can understand public_key.
  3. Remote sends messages encrypted based on public_key.
  4. Local has to use private_key to understand (decrypt) remote’s messages.

SSH command lines

# FOR EXAMPLE
ssh -C # use data compression

Usage: Access jupyter notebooks from remote server on local machine.

Below are some popular commands[ref] :

  • -C: use data compression.
  • -p <port>: port to connect.
  • -q: quiet mode.
  • -v: verbose mode.
  • -X: running GUI remote app locally.
  • -L: local port forwarding[ref] .