Common Linux Commands
Date#
$(date -d '1 day ago' '+%Y-%m-%d')
Common methods
Number formatting#
part=`printf "%03d" $i` # Left padding with zeros
Delete old files#
# Find and delete files with .tar extension modified 5 days ago
find /www/backup -mtime +5 -name "*.tar" |xargs rm
For loop#
for ((i=0;i<10;i++))
do
_date=$(date +%Y-%m-%d -d "${i} day")
echo $_date
done
#
for i in {1..10}
do
echo $i
done
File merging#
find ./ -name "item*" | xargs sed 'a\' > all.txt
find ./ -name "item*" | xargs cat > all.txt
:s/old/new # Replace the first occurrence of 'old' with 'new' in the current line
:s/old/new/g # Replace all occurrences of 'old' with 'new' in the current line
:.,$s/old/new # Replace the first occurrence of 'old' with 'new' from the current line to the last line
:.,$s/old/new/g # Replace all occurrences of 'old' with 'new' from the current line to the last line
:N,Ms/old/new # Replace the first occurrence of 'old' with 'new' from line N to line M
:N,Ms/old/new/g # Replace all occurrences of 'old' with 'new' from line N to line M
:N,Ms/old/new/gc # Replace all occurrences of 'old' with 'new' from line N to line M, and prompt for each replacement
:%s/old/new # Replace the first occurrence of 'old' with 'new' in all lines
:%s/old/new/g # Replace all occurrences of 'old' with 'new' in all lines
File sorting, intersection, union, difference#
# Sorting
sort a.txt |uniq -c
# Intersection
sort a.txt b.txt | uniq -d
# Union
sort a.txt b.txt | uniq
# Difference (a.txt - b.txt)
sort a.txt b.txt b.txt | uniq -u
# Difference (b.txt - a.txt)
sort b.txt a.txt a.txt | uniq -u
Delete duplicate lines#
sort -k2n all.txt | uniq > real.out
sort -k2n all.txt | awk '{if ($0!=line) print;line=$0}'
sort -k2n all.txt | sed '$!N; /^\(.*\)\n\1$/!P; D'
Remove spaces#
cat all.txt |sed s/[[:space:]]//g
Awk deduplication#
awk '!($1 in a){a[$1];print $1}'
# or
sort $1 | uniq
# Concatenate awk results with comma as separator
awk -F ',' '{print $1}' | xargs | tr ' ' ','
Common status check#
# Top n processes in descending order of CPU and memory usage
ps -aux --sort -pcpu,+pmem | head -n 5
# View processes by name
ps -f -C java
File synchronization with rsync#
rsync -zvrtopgl --progress --delete /fromDist/ root@s1:/toDist/
Link status statistics#
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
# TCP connection states:
# LISTEN: Server is listening for incoming TCP connections
# SYN-SENT: Initial synchronization, waiting for matching connection request after sending connection request
# SYN-RECV: Waiting for a confirming connection request acknowledgment after having both received and sent a connection request
# SYN-RECEIVED: Waiting for a confirming connection request acknowledgment after having sent a connection request
# ESTABLISHED: Open connection/normal data transfer state/currently active connections
# FIN-WAIT1: Waiting for a connection termination request from the remote TCP, or an acknowledgment of the connection termination request previously sent
# FIN-WAIT2: Waiting for a connection termination request from the remote TCP
# CLOSE-WAIT: Waiting for a connection termination request from the local user
# CLOSING: Waiting for a connection termination request acknowledgment from the remote TCP/both sides simultaneously trying to close
# LAST-ACK: Waiting for an acknowledgment of the connection termination request previously sent to the remote TCP/waiting for all packets to die
# TIME-WAIT: Waiting for enough time to pass to be sure the remote TCP received the acknowledgment of its connection termination request/both sides have initiated a release
# ITMED_WAIT: Waiting for all packets to die
# CLOSED: No connection state
CPU/Memory/System information check#
# CPU
grep "model name" /proc/cpuinfo
cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c
cat /proc/cpuinfo | grep physical | uniq -c
# CPU architecture
echo $HOSTTYPE
# Memory
grep MemTotal /proc/meminfo
# Linux version
cat /etc/redhat-release
cat /etc/os-release
cat /etc/lsb-release
# Linux kernel version
uname -a
uname -r
Common monitoring tools#
# Network monitoring
iftop
# IO monitoring
iotop
# Load monitoring
htop
top
Process monitoring#
pidstat -p 843 1 3 -u -t
# -u: Monitor CPU usage
# Parameters 1 3: Sample once per second, a total of three times
# -t: Refine the monitoring level to threads
SSH related#
# Key generation
ssh-keygen -t rsa -b 4096 -C "your_hostname"
# Passwordless login
cat ~/.ssh/id_rsa.pub | ssh root@ip "cat >> .ssh/authorized_keys"
Using firewalld firewall#
# Disable ping
firewall-cmd --permanent --add-rich-rule='rule protocol value=icmp drop'
# Allow all connections from hosts in the 192.168.1.0/24 network
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.0" accept'
# Block a specific IP address
firewall-cmd --permanent --zone=public --add-rich-rule="rule family=ipv4 source address='123.56.247.76/24' reject"
# Open a port
firewall-cmd --zone=public --permanent --add-port=8080/tcp
firewall-cmd --reload
File statistics#
ls -g |awk 'BEGIN{sum=0}{sum+=$4}END{print sum/(1024*1024*1024)}'
History format and quantity modification#
export HISTSIZE=10000
export HISTTIMEFORMAT=" %Y-%m-%d %H:%M:%S - `who am i 2>/dev/null | awk '{print $NF}'|sed -e 's/[()]//g'` - `who -u am i |awk '{print $1}'` "
export PROMPT_COMMAND="history 1 >> /var/log/.myhistory" # Append command history to a text file
touch /var/log/.myhistory
chmod /var/log/.myhistory