Last modified on 21 Sep 2020.

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 about terminals.
πŸ‘‰ Note about Screen.

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

.sh file

# using script: file.sh
#!/bin/sh
echo 'some info'
command_1
command_2

# and then sh file.sh
# with arguments
$file1 = $1
wc $file1 # word count

# multiple input args
for FILE1 in "[email protected]"; do
    wc $FILE1
done
NAME="defaut" # default value! DON'T HAVE SPACE!!!
# with flags
while getopts n:f: option; do
    case "${option}"
        in
            n) NAME=${OPTARG};;
            f) FILE=${OPTARG};;
    esac
done

echo $NAME
wc $FILE

# how to use?
sh test.sh -n "ThiD" -f test.md
# 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
# like monitor
top
# MEM USAGE
free -m
# ALL ENV
printenv
# NVIDIA
nvidia-smi
lspci -nn | grep '\[03' # another way
# list of devices
lsusb

Folders / Files

# CHANGE ACTIVE DIR
cd <dir>
cd # to the startup dir
cd / # to root
cd .. # to father dir
cd - # back to previous dir
# CREATE NEW FOLDER
mkdir <dir>
# LIST
ls
ls -a # including hidden
ls | grep 'ubuntu' # files containing 'ubuntu' in name
# 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
tree -d -I 'abc' # except folder "abc"
tree -I 'abc|xyz' # except folder "abc" and "xyz"
tree -I 'test_*|__pycache__|__init__.py' # use wildcat
tree -L 2 # level 2
tree -P 'test_' # list only files starting with "test_"

Permission

# list of groups
groups
# which groups a user belongs to
group <user_name>
id -nG # or
# check info of a current user
id <user_name>
# list all members of a group
grep <group_name> /etc/group
# 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
# open ports
sudo apt install nmap
nmap localhost
# very simple server
python3 -m http.server # localhost:8000
python3 -m http.server 1337 # localhost:1337
# current running servers
netstat -lepunt

# kill a process, e.g. 29231/ssh
kill <pid> # eg. kill 29231
# 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

Images

# open an image
eog image_file.jpg

Alias

Create your own β€œalias” command for short,

# CREATE
alias yourAlias='cd /usr/'
alias yourAlias=cd /usr/ # windows
# CALL
yourAlias
# LIST OF ALIASES
alias
alias abc # "abs" stands for what?
# remove an alias
unalias abc
# group of commands
my_alias() {
    screen -S dat -dm bash -c "cd /dinhanhthi.com; iserve; exec sh"
}
  • 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.

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

Display

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

References