Bash (Unix Shell)

Anh-Thi Dinh
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

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.
  • Ctrl + A: move cursor to the beginning of the line.

Multiple commands

1# run at once
2command_1 && command_2

.sh file

☝️
#!/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.sh
1# with arguments
2$file1 = $1
3wc $file1 # word count
4
5# multiple input args
6for FILE1 in "$@"; do
7	wc $FILE1
8done
1NAME="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.md

Search / grep / sed

1# all files / folders containing 'abc'
2ls | grep -i abc
1# find command lines containing 'abc'
2dpkg -l | grep -i abc
1# 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-packages

Check info

System

1# Change password
2passwd
1# DISK SPACE
2df -h
1# MEM USAGE
2free -m
1# like monitor
2top
1# ALL ENV
2printenv
3
4# add new
5export ABC=/xyz/thi/
1# NVIDIA
2nvidia-smi
3lspci -nn | grep '\[03' # another way
1# list of devices
2lsusb
1# list display screen
2xrandr --listactivemonitors
1# 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 processes

Folders / Files

1# CHANGE ACTIVE DIR
2cd <dir>
3cd # to the startup dir
4cd / # to root
5cd .. # to father dir
6cd - # back to previous dir
1# CREATE NEW FOLDER
2mkdir <dir>
1# LIST
2ls
3ls -a # including hidden
4ls | grep 'ubuntu' # files containing 'ubuntu' in name
1# 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 -5
1# REMOVING
2rm <file>
3rm -f <file> # force to remove
4rm -rf <dir> # remove folder
5rmdir <empty-dir> # remove empty
1# COMPRESS
2zip file.zip file/folder
3unzip file.zip # decompress
1# 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_"

Permission

1# list of groups
2groups
1# which groups a user belongs to
2group <user_name>
3id -nG # or
1# check info of a current user
2id <user_name>
1# list all members of a group
2grep <group_name> /etc/group
1# CHECK PERMISSION
2ls -l
3ls -l <file>
1# ADD existing USER to existing GROUP
2sudo usermod -a -G groupName userName
1# CHANGE PERMISSION
2chown <user>:<group> file
3chown -R thi:root folder # folder & children

Network

1# CHECK IP
2ifconfig
3ipconfig # windows
1# DOWNLOAD A FILE
2wget <https://website.com/filename.ext>
1# open ports
2sudo apt install nmap
3nmap localhost
1# very simple server
2python3 -m http.server # localhost:8000
3python3 -m http.server 1337 # localhost:1337
1# current running servers
2sudo apt install net-tools
3netstat -lepunt
4
5# kill a process, e.g. 29231/ssh
6kill <pid> # eg. kill 29231
1# Kill a port
2kill $(lsof -t -i:4000)
1# mb data used
2sudo apt install vnstat
3vnstat -d
1# 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"

Text file

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

Images

1# open an image
2eog image_file.jpg
1# Create
2ln -s original_folder sym_folder
3
4# Remove
5rm sym_folder

Alias

Create your own "alias" command for short,
1# CREATE
2alias yourAlias='cd /usr/'
3alias yourAlias=cd /usr/ # windows
1# CALL
2yourAlias
1# LIST OF ALIASES
2alias
3alias abc # "abs" stands for what?
1# remove an alias
2unalias abc
1# 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}

Create / Copy / Cut / Paste

1# Create a new folder
2mkdir <folder>
3mkdir -p <folder> # already exist accepted
1# MOVE
2mv <old-dir> <new-dir>
3move <old-dir> <new-dir> # windows
1# RENAME a file/folder
2mv olname.txt newname.txt
1# COPY
2cp file file
3cp -r file&dir file&dir

Display

1# only display 3 last directory names
2PROMPT_DIRTRIM=3
1# display only user:current_folder#
2PS1='\u:\W\$ '

References

Loading comments...