Bash commands are mainly supported in MacOS, Linux but also support in Windows. You can use integrated tools for using bash on these platforms.
👉 Note: Terminal + ZSH
- Apps: cmder (Windows), iTerm2 (MacOS), Guake Terminal (Linux), Hyper (multi-platforms).
- Online: repl.it
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:
Enterto continue to read,qto quit.
Ctrl+A: move cursor to the beginning of the line.
1# run at once
2command_1 && command_2#!/bin/bash tells your terminal to run the script with bash. There are also zsh, sh, fish,...1# using script: file.sh
2#!/bin/sh
3echo 'some info'
4command_1
5command_2
6
7# and then sh file.sh1# with arguments
2$file1 = $1
3wc $file1 # word count
4
5# multiple input args
6for FILE1 in "$@"; do
7 wc $FILE1
8done1NAME="defaut" # default value! DON'T HAVE SPACE!!!
2# with flags
3while getopts n:f: option; do
4 case "${option}"
5 in
6 n) NAME=${OPTARG};;
7 f) FILE=${OPTARG};;
8 esac
9done
10
11echo $NAME
12wc $FILE
13
14# how to use?
15sh test.sh -n "ThiD" -f test.md1# all files / folders containing 'abc'
2ls | grep -i abc1# find command lines containing 'abc'
2dpkg -l | grep -i abc1# search and extract a part of result
2pip show numpy
3# Location: /usr/lib/python3/dist-packages
4pip show numpy | sed -n 's/Location: //p'
5# /usr/lib/python3/dist-packages1# Change password
2passwd1# DISK SPACE
2df -h1# MEM USAGE
2free -m1# like monitor
2top1# ALL ENV
2printenv
3
4# add new
5export ABC=/xyz/thi/1# NVIDIA
2nvidia-smi
3lspci -nn | grep '\[03' # another way1# list of devices
2lsusb1# list display screen
2xrandr --listactivemonitors1# CPU
2cat /proc/cpuinfo | grep 'model name' | uniq # model
3cat /proc/cpuinfo | grep 'vendor' | uniq # vendor
4cat /proc/cpuinfo | grep processor | wc -l # number of processes1# CHANGE ACTIVE DIR
2cd <dir>
3cd # to the startup dir
4cd / # to root
5cd .. # to father dir
6cd - # back to previous dir1# CREATE NEW FOLDER
2mkdir <dir>1# LIST
2ls
3ls -a # including hidden
4ls | grep 'ubuntu' # files containing 'ubuntu' in name1# CURRENT PATH
2pwd # or echo ${pwd}
3
4# PARENT OF CURRENT PATH
5echo $(dirname `pwd`)1# FOLDER/FILE SIZE
2du -hs <directory / file>
3# `h` : human readable (6.7G)
4# `s` : display size
5
6# all folders/files of current folder
7du -hs * | sort -rh
8
9# only folders
10du -sh ./*/
11
12# only first 5 retrieves
13du -h /home/thi/ | sort -rh | head -51# REMOVING
2rm <file>
3rm -f <file> # force to remove
4rm -rf <dir> # remove folder
5rmdir <empty-dir> # remove empty1# COMPRESS
2zip file.zip file/folder
3unzip file.zip # decompress1# PRINT TREE folder structure
2tree
3tree -d # only folders
4tree -d -I 'abc' # except folder "abc"
5tree -I 'abc|xyz' # except folder "abc" and "xyz"
6tree -I 'test_*|__pycache__|__init__.py' # use wildcat
7tree -L 2 # level 2
8tree -P 'test_' # list only files starting with "test_"1# list of groups
2groups1# which groups a user belongs to
2group <user_name>
3id -nG # or1# check info of a current user
2id <user_name>1# list all members of a group
2grep <group_name> /etc/group1# CHECK PERMISSION
2ls -l
3ls -l <file>1# ADD existing USER to existing GROUP
2sudo usermod -a -G groupName userName1# CHANGE PERMISSION
2chown <user>:<group> file
3chown -R thi:root folder # folder & children1# CHECK IP
2ifconfig
3ipconfig # windows1# DOWNLOAD A FILE
2wget <https://website.com/filename.ext>1# open ports
2sudo apt install nmap
3nmap localhost1# very simple server
2python3 -m http.server # localhost:8000
3python3 -m http.server 1337 # localhost:13371# current running servers
2sudo apt install net-tools
3netstat -lepunt
4
5# kill a process, e.g. 29231/ssh
6kill <pid> # eg. kill 292311# Kill a port
2kill $(lsof -t -i:4000)1# mb data used
2sudo apt install vnstat
3vnstat -d1# INTERNET SPEED (need python)
2curl -s <https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py> | python -1# (1st test on mac)
2# Look for which ports are running
3# Find their PID
4# Kill them
5brew install nmap
6nmap localhost
7lsof -i:8080
8kill <PID>
9
10# in case above command cannot solve the problem
11sudo lsof -i -P | grep "8080"1# QUICK LOOK CONTENT
2more file.txt
3cat file.txt1# JUST CREATE
2touch file.txt1# CREATE + MODIFY
2nano file.txt # Ctrl+X to quit
3vim file.txt # ESC, :q to quit1# SEARCH STRING
2grep "string" file.txt1# ADD A LINE TO A FILE WITHOUT OPENNING IT
2echo "hello 'thi' world" >> my_file.txt1# open an image
2eog image_file.jpg1# Create
2ln -s original_folder sym_folder
3
4# Remove
5rm sym_folderCreate your own "alias" command for short,
1# CREATE
2alias yourAlias='cd /usr/'
3alias yourAlias=cd /usr/ # windows1# CALL
2yourAlias1# LIST OF ALIASES
2alias
3alias abc # "abs" stands for what?1# remove an alias
2unalias abc1# group of commands
2my_alias() {
3 screen -S dat -dm bash -c "cd /dinhanhthi.com; iserve; exec sh"
4}1# list of commands
2my_alias(){
3 cd /home/user/git/abc/
4 git add .
5 git commit -m "abc"
6 git push
7}1# With parameter
2alias testing="echo $1"
3# Run as `testing abc`
4
5my_alias() {
6 cd thi/
7 echo $1
8}1# Used as `check_samples "api"` or just `check_samples`
2check_samples() {
3 if [ ! -z $1 ]; then # If there is parameter
4 python scripts/check_samples.py | grep $1
5 else # If there is no parameter
6 python scripts/check_samples.py
7 fi
8}1# Create a new folder
2mkdir <folder>
3mkdir -p <folder> # already exist accepted1# MOVE
2mv <old-dir> <new-dir>
3move <old-dir> <new-dir> # windows1# RENAME a file/folder
2mv olname.txt newname.txt1# COPY
2cp file file
3cp -r file&dir file&dir1# only display 3 last directory names
2PROMPT_DIRTRIM=31# display only user:current_folder#
2PS1='\u:\W\$ '