Operations 23 min read

Essential Linux Command Cheat Sheet for Developers and Sysadmins

This article compiles a comprehensive set of Linux command‑line shortcuts covering system shutdown, reboot, hardware and CPU info, file and directory management, user and permission handling, networking tools, process monitoring, and performance diagnostics, providing a handy reference for anyone working in a terminal‑heavy environment.

Java Interview Crash Guide
Java Interview Crash Guide
Java Interview Crash Guide
Essential Linux Command Cheat Sheet for Developers and Sysadmins

Basic Operations

Shutdown and Reboot

# Shutdown
shutdown -h now

# Reboot
shutdown -r now

View System and CPU Information

# Kernel info
uname -a

# Kernel version
cat /proc/version

# Environment variables
env

# CPU info
cat /proc/cpuinfo

# Number of logical CPUs and model
cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c

# Physical CPU count and cores per CPU
cat /proc/cpuinfo | grep physical | uniq -c

# 32‑bit or 64‑bit mode
getconf LONG_BIT

# Check for 64‑bit support (lm flag)
cat /proc/cpuinfo | grep flags | grep ' lm ' | wc -l

Create Symbolic Link

ln -s /usr/local/jdk1.8/ jdk

RPM Queries

# Check if a package is installed via rpm
rpm -qa | grep <package_name>

SSH Key Generation

# Generate SSH key
ssh-keygen -t rsa -C [email protected]

# Copy the public key to the target server's authorized_keys (ensure .ssh is 700 and authorized_keys is 600)

Command Aliases

# Add alias to .bash_profile
alias ll='ls -alF'

Synchronize Server Time

sudo ntpdate -u ntp.api.bz

Run Commands in Background

# With nohup output to nohup.out
nohup xxx &

# No output at all
nohup xxx > /dev/null &

# Redirect error to log
nohup xxx > out.log 2>&1 &

Force Logout Active Users

# Force logout of a TTY
pkill -kill -t <TTY>

Find Command Path

which <command>

Check Maximum Open File Descriptors

ulimit -n

Configure DNS

vim /etc/resolv.conf

nslookup – Query DNS Records

nslookup google.com

View Recent Login Information

# Last 5 logins
last -n 5

Set Static IP

ifconfig em1 192.168.5.177 netmask 255.255.255.0

View Process Environment Variables

# Show environment of a process
ps eww -p <PID>

Process Tree

ps auwxf

Process Start Path

cd /proc/<PID>
ls -all   # cwd shows the start directory

Add User and Grant sudo

# Add user
useradd <username>
passwd <username>

# Grant sudo rights
vim /etc/sudoers   # Add line:
# <username> ALL=(ALL) ALL

Kill All Processes Matching Name

ps aux | grep xxx | grep -v grep | awk '{print $2}' | xargs kill -9

Disk, File, and Directory Operations

Vim Editing Shortcuts

# Global replace
:%s/x/y/g

# Move cursor to line start / end
0   # start of line
$   # end of line

# Jump to file start / end
gg   # top of file
Shift+g   # bottom of file

# Show / hide line numbers
:set nu   # show
:set nonu # hide

# Search
/xxx   # forward search, press n for next
?xxx   # backward search

Save Read‑Only File without Switching User

:w !sudo tee %

Disk and Directory Size Information

# Show mounted filesystems
mount

# Show partition usage
df

# Directory size (recursive)
du -H -h

# Non‑recursive size of each entry
du -sh *

wc Command

# Line count
wc -l filename

# Word count
wc -w filename

# Longest line length
wc -L filename

# Byte count
wc -c filename

Compression and Decompression

# Compress
tar czvf xxx.tar <directory>
zip -r xxx.zip <directory>

# Decompress
tar zxvf xxx.tar
# To a specific directory
tar zxvf xxx.tar -C /xxx/yyy/
unzip xxx.zip

Change File Owner/Group

chown eagleye.eagleye xxx.log

Copy, SCP, and mkdir

# Copy file
cp xxx.log

# Force overwrite
cp -f xxx.log

# Copy directory
cp -r src_dir dst_dir

# Remote copy
scp -P <ssh_port> [email protected]:/home/username/xxx /home/xxx

# Create nested directories
mkdir -p /xxx/yyy/zzz
mkdir -p src/{test,main}/{java,resources}

File Comparison

diff -u 1.txt 2.txt

Log Byte Count for Performance Testing

# Output a dot per iteration and monitor byte count
tail -f xxx.log | pv -bt

Remove Special Characters

# Show special characters
cat -v xxx.sh

# Remove ^M characters
sed -i 's/\r//g' env.sh

Convert File Encoding (macOS)

# Convert to UTF‑8
:set fileencodings=utf-8
:w

# Set Unix line endings
:set fileformat=unix

# Batch convert .sh files on macOS
find . -name "*.sh" | xargs dos2unix

tee – Write to Screen and File Simultaneously

awk '{print $0}' xxx.log | tee test.log

Search‑Related Commands

grep

# Invert match (exclude)
grep -v xxx

# Exclude empty lines
grep -v '^$'

# Show line numbers of empty lines
grep -n '^$' file.txt

# Lines starting with abc
grep -n '^abc' file.txt

# Show line numbers where pattern occurs
grep 'xxx' -n xxx.log

# Count occurrences
grep 'xxx' -c xxx.log

# Case‑insensitive search
grep 'xxx' -i xxx.log

awk

# Print lines where 5th field contains "user"
awk -F ':' '{if ($5 ~ /user/) print $0}' /etc/passwd

# Count occurrences of a character (non‑Chinese)
awk -v RS='character' 'END{print --NR}' xxx.txt

find

# Find files with .mysql suffix
find /home/eagleye -name '*.mysql' -print

# Files accessed in last 3 days
find /usr -atime 3 -print

# Files modified in last 5 days
find /usr -ctime 5 -print

# Files owned by jacky, name starts with j
find /doc -user jacky -name 'j*' -print

# Files starting with ja or ma
find /doc \( -name 'ja*' -o -name 'ma*' \) -print

# Delete *.bak files
find /doc -name '*bak' -exec rm {} \;

Network‑Related Commands

Check Which Process Uses a Port

lsof -i:<port>

Get Local IP Address

/sbin/ifconfig -a | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{print $2}' | tr -d "addr:"

iptables

# Show status
service iptables status

# Block an IP
iptables -I INPUT -s <IP> -j DROP

# Unblock an IP
iptables -D INPUT -s <IP> -j DROP

# Open port 9090
/sbin/iptables -I INPUT -p tcp --dport 9090 -j ACCEPT

# Manage firewall service
/etc/init.d/iptables status
/etc/init.d/iptables start
/etc/init.d/iptables stop
/etc/init.d/iptables restart

nc (netcat) – TCP Debugging

# Send data to endpoint
nc 192.168.0.11 8000 < data.txt

# Listen and save incoming data
nc -l 8000 > received_data

# Persistent listening
nc -lk 8000

tcpdump – Capture Packets

# Dump TCP packets on port 12301
tcpdump -i em1 tcp port 12301 -s 1500 -w abc.pcap

traceroute

# ICMP traceroute
traceroute -I www.163.com

# Start from TTL 3
traceroute -M 3 www.163.com

# Trace with specific port
traceroute -p 8080 192.168.10.11

ss – Socket Statistics

# List listening sockets
ss -l

# Show processes owning sockets
ss -pl

# All TCP sockets
ss -t -a

# All UDP sockets
ss -u -a

# Established SMTP connections
ss -o state established '( dport = :smtp or sport = :smtp )'

# Established HTTP connections
ss -o state established '( dport = :http or sport = :http )'

# X server sockets
ss -x src /tmp/.X11-unix/*

# Socket statistics summary
ss -s

netstat

# Count connections per IP and state
netstat -n | awk '/^tcp/ {n=split($(NF-1),a,":"); if(n<=2) ++S[a[1]]; else ++S[a[4]]; ++s[$NF]; ++N} END {for(i in S) printf("%-20s %s
", i, S[i]); printf("%-20s %s
","TOTAL_IP", length(S)); for(i in s) printf("%-20s %s
", i, s[i]); printf("%-20s %s
","TOTAL_LINK", N)}'

# Connection state summary
netstat -n | awk '/^tcp/ {++state[$NF]} END {for(s in state) print s "\t" state[s]}'

# Top TIME_WAIT connections
netstat -n | grep TIME_WAIT | awk '{print $5}' | sort | uniq -c | sort -rn | head -n20

Linux Performance Monitoring

top

# Press uppercase F or O then a‑z to sort by a column, press R to reverse

dmesg – Kernel Messages

dmesg

iostat – Disk I/O

iostat -xz 1

free – Memory Usage

free -m

sar – Network Throughput

# Network device throughput
sar -n DEV 1

# TCP connection statistics
sar -n TCP,ETCP 1

vmstat – CPU, Memory, I/O

# Sample every 2 seconds, once
vmstat 2 1

Source: 一安未来

shellcommand-linesystem-administration
Java Interview Crash Guide
Written by

Java Interview Crash Guide

Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.