Master Essential Linux Commands: Port Checks, Disk Usage, File Management & More
This guide compiles essential Linux command-line techniques, covering port inspection, disk and CPU monitoring, time‑based file cleanup, secure copying with scp, file searching, disk usage analysis, system information retrieval, firewall management, permission changes, and advanced utilities like watch, crontab and mount.
Practical Commands
1. Check Port Usage
# Check if port 5000 is in use
netstat -lnp | grep 5000
# Alternative method
lsof -i:'5000'
# Kill the process occupying the port
kill -9 28533
# Install missing tools if needed
yum -y install net-tools
# Install firewalld if firewall is required
yum install firewalld2. Disk and CPU Information
# Show disk usage
df -h
# Show directory usage
du -lh --max-depth=1
du -sh *
# Show CPU information
lscpuKey parameters extracted from lscpu output include CPU(s): 4, On-line CPU(s) list: 0‑3, Thread(s) per core: 2, Core(s) per socket: 2, Socket(s): 1, and the model name indicating Intel, AMD, etc.
3. Delete Files by Time
find /home/lifeccp/dicom/studies -mtime +21 -name "*.*" -exec rm -Rf {} \;Brief explanation of the shell command
/home/lifeccp/dicom/studies – target directory
-mtime – modification time selector
+21 – files older than 21 days
"*.*" – file pattern
-exec rm -Rf {} \; – force delete
Note: In practice the command may delete files 1‑2 days older than expected; verify after execution.
4. scp Remote Copy Details
Syntax: scp [options] source destination Key options include:
-1 force SSH1 protocol
-2 force SSH2 protocol
-4 use IPv4 only
-6 use IPv6 only
-B batch mode (no password prompt)
-C enable compression
-p preserve timestamps and permissions
-q suppress progress bar
-r recursive copy
-v verbose output
-c cipher for encryption
-F alternative ssh config file
-i identity file
-l limit bandwidth (Kbit/s)
-o ssh option
-P port (uppercase P)
-S program for data transfer
Examples:
scp -r /root/lk [email protected]:/home/lk/cpfile
scp -r [email protected]:/home/lk /root5. Find Files by Date
# List files in /recordings/ for a specific date
ls --full-time /recordings/ | sed -n '/2018-03-21/p'
# Find files between two dates
find images/ -newermt '2021-01-01' ! -newermt '2021-01-31'
# Copy files within a date range to another server
scp -p <file_path> <user>@<IP>:/target_path6. File and Directory Statistics
6.1 Count Files
# Count files in current directory
ls -l | grep "^-" | wc -l
# Count files recursively
find ./ -type f | wc -l
# Count directories recursively
ls -lR | grep "^d" | wc -l6.2 Disk, Inode and Memory Statistics
Use du, df and free for detailed size and usage information. Example commands:
# du detailed options
du -h *
du -sh *
# df usage
df -h
# free memory
free -m7. System Information
# Kernel version
uname -r
# Distribution info
lsb_release -a
cat /etc/*-release
# Full system info
uname -a
# Real‑time bandwidth
nload7.1 Process Details with top
top
top -d 1 -p <pid>Key fields displayed by top include PID, USER, PR, NI, VIRT, RES, SHR, S, %CPU, %MEM, TIME+, and COMMAND.
7.2 Timezone Settings
timedatectl
timedatectl set-time "YYYY-MM-DD HH:MM:SS"
timedatectl list-timezones
timedatectl set-timezone Asia/Shanghai
timedatectl set-ntp yes
timedatectl set-local-rtc 1
hwclock --systohc --localtime8. Open and View Ports
# List open ports
firewall-cmd --list-ports
# Open a single port
firewall-cmd --zone=public --add-port=8080/tcp --permanent
# Open a range of ports
firewall-cmd --zone=public --add-port=20000-29999/tcp --permanent
# Close a port
firewall-cmd --zone=public --remove-port=8080/tcp --permanent
# Reload firewall
firewall-cmd --reload9. Change Ownership, Group and Permissions
# Change group
chgrp [-R] group file
# Change owner
chown [-R] owner file
chown [-R] owner:group file
# Add execute permission
chmod +x file.sh
# Symbolic permission changes
chmod g+w,o+x filename
chmod g=rwx,o=rx filename10. Extended Commands
10.1 Periodic Execution (watch)
watch -n 1 'ls'10.2 Command History
history
history -c10.3 Routing Information
route
traceroute -T -p [port] [target_ip]10.4 crontab (Scheduled Tasks)
# Edit crontab
crontab -e
# Example: run daily at 02:00
00 2 * * * command
# Start/stop cron service
service crond start
service crond restart
service crond stop
# List crontab
crontab -l
service crond status10.5 Mounting Filesystems
# Mount ISO read‑only
mount -t iso9660 -o ro /dev/cdrom /mnt/cdrom/
# Unmount
umount /mnt/cdrom10.6 Quick Replacement in Large Files
# Replace @ with |
:%s/@/|/g10.7 User Password Expiration
# Show password info
chage -l username
# Set max age
chage -M 99999 username
# Set explicit expiration date
usermod -e 'YYYY-MM-DD' usernameMaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
