baicai

白菜

一个勤奋的代码搬运工!

Common Linux Commands

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 the .tar extension that were 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 Merge#

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' on the current line
:s/old/new/g         # Replace all occurrences of 'old' with 'new' on 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 deletion
:%s/old/new          # Replace the first occurrence of 'old' with 'new' on all lines
:%s/old/new/g        # Replace all occurrences of 'old' with 'new' on 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

Remove Duplicates with awk#

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/
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
# Explanation of TCP connection states
# LISTEN: The server is waiting for a connection request from a remote TCP port
# SYN-SENT: After sending a connection request, waiting for a matching connection request
# SYN-RECV: A connection request has arrived, waiting for confirmation
# SYN-RECEIVED: After receiving and sending a connection request, waiting for confirmation from the other party
# ESTABLISHED: Represents an open connection/normal data transfer state/current concurrent connections
# FIN-WAIT1: Waiting for a remote TCP connection termination request or confirmation of a previous connection termination request/Application says it has finished
# FIN-WAIT2: Waiting for a connection termination request from the remote TCP/other side has agreed to release
# CLOSE-WAIT: Waiting for a connection termination request from the local user
# CLOSING: Waiting for confirmation of a connection termination from the remote TCP/both sides are trying to close at the same time
# LAST-ACK: Waiting for confirmation of the original connection termination request sent to the remote TCP/Waiting for all packets to die
# TIME-WAIT: Waiting for enough time to ensure that the remote TCP has received the connection termination request confirmation/The other side has initialized a release
# ITMED_WAIT: Waiting for all packets to die
# CLOSED: No connection state

CPU/Memory/System Information View#

# 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
# 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" # Record commands to a file
touch /var/log/.myhistory
chmod  /var/log/.myhistory
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.