Learn Linux really quick:

Basic

Control options

Operation

Command Example

Execute command one by one

command1; command2

Execute command1 background

command1 & command2

Execute command2 if 1 success

command1 && command2

Execute command2 if 1 fail

command1 || command2

Reverse the command result

!command1

Pipeline options

Operation

Command Example

Redirect the output as input to next

command1 | command2

Redirect the output and error

command1 |& command2

Stream options

Operation

Command Example

Give input to a command

command < in.txt

Readonly mode to a command

command <> in.txt

Redirect the output

command > out.txt

Redirect the output and error

command > out.txt > error.txt

Redirect the output and override

command >| out.txt

Redirect the output and append

command >> out.txt

Redirect the output and error

command &> out.txt

Pipelines

Operation

Pipeline example

Page view

cat NewDocument | less

Search in content

cat NewDocument | grep something

Count lines

cat NewDocument | wc -l

Foreach in lines

cat NewDocument | xargs -L1 echo

Convert to base64

echo MyString | base64

Permissions

Group control

List all groups:

cat /etc/group

Create a new group:

sudo groupadd my-group

Delete a group:

sudo groupdel my-group

User control

List all users:

cat /etc/passwd

Create new user:

sudo adduser user-name

Delete a user:

sudo deluser afkay

User group control

View all groups an user belongs to:

groups user-name

View all users in a group:

grep 'group-name' /etc/group

Add an existing user to a group:

usermod -a -G example-group example-user-name

Remove an existing user from a group:

gpasswd --delete user group

Files Management

Operation

Command Example

Show Files (List View)

ls

Show Files (Detail View)

ls -l

Show Hidden Files

ls -a

Delete File

rm filename

Delete Folder

rm foldername -rf

Move File

mv filename /some/path

Unzip File

tar zxvf ./file.tar.gz

Zip File

tar czvf file.tar.gz ./folder

Run as Administrator

sudo command

Locate command

which ping

View File

cat NewDocument.txt

Search Files

find . -name '*.txt'(Search *.txt in current folder)

View Folder size

du -d 3 -h (3 is depth)

Create File

touch file

File permission

chmod 777 ./public_executable
  • 7 is 111, which is rwx. Read, write and executable.

  • 6 is 110 which is rw-. Read and write.

  • 5 is 101 which is r-x Readonly executable.

  • 4 is 100 which is r-- Readonly.

  • 3 is 011 which is -wx (Usualy we don't use it)

  • 2 is 010 which is -w- (Usualy we don't use it)

  • 1 is 001 which is --x (Usualy we don't use it)

  • 0 is 000 which is --- Nothing.

  • 755 means: -rwxr-xr-x.

  • 644 means: -rw-r--r--.

First 3 is for owner. Second 3 is for users in the group. Last 3 is for others.

To change owner\group:

chown user ./somefile

Quick tip:

sudo find /data -type d -print0 | sudo xargs -0 chmod 0755 # Set all folders as 755.
sudo find /data -type f -print0 | sudo xargs -0 chmod 0644 # Set all files as 644.
sudo chown www-data:www-data ./data/ -R # Change Owner.

Hardware

Copy by bytes

dd if=filesource of=target

Disk management

sudo fdisk /dev/sda

Tips:

  • n – Create partition
  • p – print partition table
  • g - reset as GPT partition table
  • d – delete a partition
  • q – exit without saving the changes
  • w – write the changes and exit.

After creating, we can format it.

mkfs.ext4 /dev/sda1

View mounting status:

lsblk

View disk usage:

df -Th

Refresh disk size after reseting the partition table:

sudo resize2fs /dev/sdb1

Remote mounting

Edit /etc/fstab file.

# SMB
//vault/Shared  /mnt/Shared cifs user=user,pass=pass 0 0

# Disk
UUID=7dbd0682-5dc6-4a70-823c-40e37c86120e /mnt/safe_store       ext4    defaults,noatime,nofail 0       0

To get UUID:

sudo blkid | grep UUID=

System monitoring

Process:

top

Global IO:

dstat

Cores:

htop

IO by process:

sudo htop

Process

View Logged on Users

w

View Process List

top

Kill Process by Name

kill $(pidof windowsphone)

Network

View my IP Address

ifconfig

Download File

wget https://some/file.zip

Ping

ping bing.com

Check DNS Records

dig bing.com

HTTP

Operation

Command Example

GET Url

curl -v https://www.baidu.com

POST Url

curl -v -d 'abc' https://www.baidu.com

POST JSON

curl -v -d '{ }' -H "Content-Type: application/json" https://www.baidu.com

OPTIONS Url

curl -v -X OPTIONS https://www.baidu.com

Programs and servicing

Package managing

Operation

Command Example

Install Software

sudo apt install something

Uninstall Software

sudo apt remove something

Unistall Unused Software

sudo apt autoremove

List Installed Software

sudo apt list someprefix*

View "NuGet" Source

sudo ls /etc/apt/sources.list.d

Remove "NuGet" Source

sudo rm -i /etc/apt/sources.list.d/PPA_Name.list

Service

Operation

Command Example

Check Service Status

sudo systemctl status work996

Start a Service

sudo systemctl start work996

Stop a Service

sudo systemctl stop work996

Register a Service

sudo systemctl enable work996

Remove a Service

sudo systemctl disable work996

Create a Service

sudo nano /etc/systemd/system/work996.service

Example of a custom service:

[Unit]
Description=Work 996

[Service]
WorkingDirectory=/home/pi/some/dir
ExecStart=/home/pi/some/dir/work996
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=work-996
User=pi
Environment=ENV_VAR=SomeValue
Environment=ANOTHER_ENV_VAR=AnotherValue

[Install]
WantedBy=multi-user.target

Updates

Operation

Command Example

App Update

sudo apt update && sudo apt upgrade

Feature Update

sudo apt dist-upgrade

Examples

Auto run commands on start up

sudo vim .profile

Add commands you want to execute, e.g:

# set .NET Core SDK and Runtime path
export DOTNET_ROOT=$HOME/dotnet
export PATH=$PATH:$HOME/dotnet

Text editing

nano

nano config.json

use Ctrl to invoke short cuts

vim

vim config.json

Use :wq to save and :q! to quit.

micro

micro config.json

use Ctrl+E for commands, Ctrl + Q for exit, Ctrl + A,S just work like Windows. https://micro-editor.github.io/index.html

Tmux

To install:

sudo apt install tmux

Create session:

tmux new -s test                                                                            # Create and enter
tmux new -s test -d "sleep 3 && echo I love you! && sleep 30"  # Create and run

Leave session:

exit
# Or
Ctrl+B D

List session:

tmux ls

Attach session:

tmux a               # When only one session.
tmux a -t test    # With session name.

Send command to session:

tmux send-keys -t test "echo Something!" Enter

Screen

To install:

sudo apt install screen

Create session:

echo "sleep 3 && echo I love you! && sleep 30" > ./love.sh
chmod +x ./love.sh
screen -dmS test ./love.sh

Leave session:

exit
# Or
Ctrl+A D

List session:

screen -ls

Attach session:

screen -r test

Send command to session:

screen -S test -X stuff 'echo Something!'`echo -ne '\015'`

Steam CMD

Install steam cmd:

Don't follow Steam website's document! Those documents are suggesting you to create a new user named: steam. That is totally unnecessary. That is because some Linux user may not follow the best practice for authentication and use root account to run steam. Usually for professional engineers, creating their own account is the first step.

 sudo add-apt-repository multiverse
 sudo apt install software-properties-common
 sudo dpkg --add-architecture i386
 sudo apt update
 sudo apt install lib32gcc-s1 steamcmd 

After that you can directly open steamcmd via: steamcmd.

Install an app, for example, project zomboid server (Inside steam cmd):

// Optional: @ShutdownOnFailedCommand 1
// Optional: @NoPromptForPassword 1
// Optional: force_install_dir /opt/pzserver/
login anonymous 
app_update 380870 validate // Project Zomboid as an example.
quit

Run script with steamcmd:

# This is bash!
steamcmd +runscript $HOME/update_zomboid.txt

Monitoring

Top

top: Top is a command-line tool used to monitor the system processes and resource usage in real-time. It provides a dynamic overview of the system's performance, including CPU usage, memory usage, and process information.

file

Free

free: Free is a command-line tool used to monitor the system memory usage. It provides an overview of the total memory available, used, and free, as well as the memory used by buffers and caches

file

NetStat

netstat: Netstat is a command-line tool used to monitor the network connections and their status. It provides an overview of the active network connections, including the source and destination addresses, the protocol used, and the status of the connection.

file

IoTop

iotop: Iotop is a command-line tool used to monitor the I/O usage of the system processes in real-time. It provides a dynamic overview of the I/O operations, including read and write operations, and the processes that are generating them.

file

IOStat

iostat: Iostat is a command-line tool used to monitor the I/O performance of the system disks and devices. It provides an overview of the disk utilization, I/O wait time, and transfer rates.

file

HTop

htop: Htop is an interactive process monitoring tool used to monitor the system processes and resource usage. It provides a dynamic overview of the system's performance, including CPU usage, memory usage, and process information.

file

IFStat

ifstat: Ifstat is a command-line tool used to monitor the network interface traffic and bandwidth usage. It provides an overview of the network traffic, including incoming and outgoing data rates.

file

IFTop

iftop: Iftop is a command-line tool used to monitor the network interface traffic in real-time. It provides a dynamic overview of the network traffic, including the source and destination addresses, and the amount of data being transferred.

file

DStat

dstat: Dstat is a command-line tool used to monitor the system resource usage, including CPU usage, memory usage, disk I/O, and network traffic. It provides a dynamic overview of the system's performance in real-time.

file

SAR

sar: Sar is a command-line tool used to collect and report system resource usage data over time. It provides a historical overview of the system's performance, including CPU usage, memory usage, and disk I/O.

VMStat

vmstat: Vmstat is a command-line tool used to monitor the system resource usage, including CPU usage, memory usage, and disk I/O. It provides a static overview of the system's performance at a specific point in time.

file

NMon

nmon: Nmon is a command-line tool used to monitor the system resource usage, including CPU usage, memory usage, disk I/O, and network traffic. It provides a dynamic overview of the system's performance in real-time.

file

ATop

atop: Atop is a command-line tool used to monitor the system resource usage, including CPU usage, memory usage, disk I/O, and network traffic. It provides a dynamic overview of the system's performance in real-time, with the ability to drill down into specific processes and threads.

file

Glances

glances: Glances is a command-line tool used to monitor the system resource usage, including CPU usage, memory usage, disk I/O, and network traffic. It provides a dynamic overview of the system's performance in real-time, with a user-friendly interface and the ability to monitor multiple systems simultaneously.

Quick Tips

Count total lines of my code

find . -name "*.cs" | xargs -L1 cat | wc -l

Get the total lines of CSharp files

find . -name "*.cs" | grep -v obj | grep -v Migrations |  xargs -L1 wc -l | sort -nr

Get boot time

who -b

Get shutdown events

last

Get which file is the longest C# file

find . -name "\*.cs" | xargs -L1 wc -l | sort -n

Pull all repositories:

find . -maxdepth 2 -mindepth 2 -type d \( ! -name . \) -exec bash -c "cd '{}' && pwd && git pull" \;

Git add only non-white-space changes

git add `git diff -w --ignore-submodules |grep "^[+][+][+]" |cut -c7-`

Test disk write speed

dd if=/dev/zero of=/tmp/tmp count=1 bs=1G

Test transfer speed via SSH

#download  
ssh anduin@host 'dd if=/dev/urandom bs=1024000 count=10' | dd of=/dev/null  
  
#upload  
dd if=/dev/urandom bs=102400 count=10 | ssh anduin@HOST 'cat > /dev/null'

Download file without wget and show downloaded file

curl https://www.baidu.com/ --output - > baidu.html && cat ./baidu.html

Heat the CPU without touching the disk

yes > /dev/null & yes > /dev/null & yes > /dev/null & yes > /dev/null & yes > /dev/null

Get new UUID and convert to base 64

uuid=$(uuidgen) && echo $uuid && mybase64=$(echo $uuid | base64) && echo $mybase64

Get random 4 numbers

cat /dev/urandom | tr -dc '0-9' | fold -w 4 | head -n 1

Keep playing random video under the current folder

while true; do vlc "$(find . -name "\*.mp4" | shuf -n 1)"; done

Sync photos with limited folder depth to another folder (Copy but ignore existing)

find "/source" -maxdepth 12345 -not -type d | xargs -i cp -nv '{}' '/target'

Use DD to burn a USB boot disk (Like Windows)

sudo dd bs=4M if=~/Downloads/Win11_22H2_English_x64v1.iso of=/dev/disk/by-id/usb-USB_SanDisk_3.2Gen1_-0:0 conv=fdatasync status=progress

Go to Prison

sudo rm / -rvf --no-preserve-root