Learn Linux really quick:
Basic
Control options
Operation
Command Example
Execute command one by one
command1; command2
Execute command1 in the background
command1 & command2
Execute command2 if command1 succeeds
command1 && command2
Execute command2 if command1 fails
command1 || command2
Reverse the command result
!command1
Pipeline options
Operation
Command Example
Redirect the output as input to the next command
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 a 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
File 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
7is111, which isrwx. Read, write, and execute.6is110, which isrw-. Read and write.5is101, which isr-x. Read-only and execute.4is100, which isr--. Read-only.3is011, which is-wx. (Usually, we do not use this.)2is010, which is-w-. (Usually, we do not use this.)1is001, which is--x. (Usually, we do not use this.)0is000, which is---. No permissions.755means:-rwxr-xr-x.644means:-rw-r--r--.
The first three digits are for the owner. The next three digits are for users in the group. The last three digits are for others.
To change the owner or 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 resetting the partition table:
sudo resize2fs /dev/sdb1
Remote mounting
Edit the /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 management
Operation
Command Example
Install Software
sudo apt install something
Uninstall Software
sudo apt remove something
Uninstall 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 shortcuts.
vim
vim config.json
Use :wq to save and :q! to quit.
micro
micro config.json
Use Ctrl+E for commands, Ctrl+Q to exit, and Ctrl+A/S work just like in 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:
Do not follow the documentation on the Steam website! Those documents suggest creating a new user named steam. This is completely unnecessary. This is because some Linux users may not follow best practices for authentication and instead use the root account to run steam. Typically, 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.

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

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.

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.

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.

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

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

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

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

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 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.

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

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

Glances
glances: Glances is a command-line tool used to monitor system resource usage, including CPU usage, memory usage, disk I/O, and network traffic. It provides a dynamic overview of system performance in real-time, featuring 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 a new UUID and convert it to base64
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
Continue playing random videos in 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
Comments (0)
No comments yet. Be the first to leave one!