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

πŸ‘‰ Terminals: check this note.

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: file .sh
#!/bin/sh
echo 'some info'
command_1
command_2
# all files / folders containing 'abc'
ls | grep -i abc
# find command lines containing 'abc'
dpkg -l | grep -i abc

Check info

System

# DISK SPACE
df -h
# CPU
cat /proc/cpuinfo | grep 'model name' | uniq # model
cat /proc/cpuinfo | grep 'vendor' | uniq # vendor
cat /proc/cpuinfo | grep processor | wc -l # number of processes
# MEM USAGE
free -m
# ALL ENV
printenv
# NVIDIA
nvidia-smi
lspci -nn | grep '\[03' # another way

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
# PRINT TREE folder structure
tree
tree -d # only folders

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
# DOWNLOAD A FILE
wget https://website.com/filename.ext
# INTERNET SPEED (need python)
curl -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | python -

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