Don’t Let Basic Linux Skills Hold You Back! Do You Really Understand These Shell Tricks?

Click the blue “Most Programmer” to follow me!

Add a “star” to get daily tech learning at 18:03

Shell is the core weapon of the Linux system, but many people only know the basic commands likelscdrm and nothing more. The following advanced tricks will help you say goodbye to being a “Shell novice” and boost your efficiency!

Don't Let Basic Linux Skills Hold You Back! Do You Really Understand These Shell Tricks?

head

1. Command Line Shortcuts: Double Your Speed

Cursor Movement
  -Ctrl + a: Jump to the beginning of the command line.
  -Ctrl + e: Jump to the end of the command line.
  -Alt + b / Alt + f: Jump forward/backward by word (10 times faster than arrow keys).

History Operations
  -!!: Repeat the last command (sudo !! to elevate privileges and retry).
  -!$: Reference the last argument of the previous command (e.g., mkdir dir && cd !$).
  -Ctrl + r: Reverse search through command history, input keywords for real-time matching.

Quick Editing
  -Ctrl + w: Delete the previous word.
  -Ctrl + u: Delete all content before the cursor.
  -Ctrl + k: Delete all content after the cursor.

2. Piping and Redirection: Master of Data Manipulation

Combination Skills
  - cmd 2>&1 | tee log.txt: Output to both screen and file (including error stream).
  - curl -s http://xxx | jq .data: Directly parse JSON and extract fields (requires jq tool).

Black Hole and White Hole
  - >/dev/null 2>&1: Discard all output of the command (silent execution).
  - cmd < input.txt: Read input from a file (instead of typing manually).

Process Substitution 
diff <(ls dir1) <(ls dir2) # Compare the file lists of two directories

3. Text Processing: One Line of Code to Replace Excel

awk Magic
  - Extract the last column: awk '{print $NF}' file.txt
  - Count IP access times: awk '{ip[$1]++} END {for (i in ip) print i, ip[i]}' access.log

sed Tricks
  - Replace newline with comma: sed ':a;N;$!ba;s/\n/,/g' file.txt
  - Delete empty lines: sed '/^$/d' file.txt

Advanced grep
  - Show matching lines with surrounding content: grep -A 3 -B 2 "error" log.txt # Show matching line and 3 lines before and 2 lines after
  - Reverse match: grep -v "success" file.txt (exclude lines containing "success")

4. Process Management: The True Control Master

Background and Foreground
  -cmd &: Run command in the background.
  -jobs: View background tasks, fg %1 to bring the first task to the foreground.
  -nohup cmd &: Command continues running after disconnecting SSH (output redirected to nohup.out).

Signal Control
  -Ctrl + z: Pause the current task (suspend to background).
  -kill -9 PID: Forcefully kill the process (-15 for graceful termination).
  -pkill -f "pattern": Kill processes by name pattern.

Resource Monitoring
  -htop: Interactive process manager (more intuitive than top).
  -lsof -i :8080: View the process occupying port 8080.

5. Scripting Techniques: Say Goodbye to Inefficient Repetition

Variable Handling
  - Default value: ${var:-"default"} (use default if var is empty).
  - String slicing: ${str:0:5} (get the first 5 characters).

Functions and Aliases
# Define function for quick compression
zipdir() { zip -r "$1.zip" "$1"; }
# Alias to simplify common commands
alias ll='ls -alh --color=auto'

Script Debugging
  -set -x: Enable debugging mode (print each command).
  -set -e: Exit immediately on error (to avoid cascading failures).

6. File Operations: Batch Renaming is Not a Dream

Universal find
  - Delete logs older than 7 days: find /logs -name "*.log" -mtime +7 -exec rm {} \;
  - Batch modify permissions: find . -type f -name "*.sh" -exec chmod 755 {} \;

rename Tool
  - Change all .txt extensions to .md: rename 's/\.txt$/.md/' *.txt

7. Rare but Powerful Tools

xargs Parallel Acceleration
  cat urls.txt | xargs -P 4 -I {} curl -O {} # 4-thread parallel download

ssh Tunnel and Proxy
  - Local port forwarding: ssh -L 8080:localhost:80 user@remote
# Access remote's port 80 → local 8080

tmux Split Screen
  -tmux new -s mysession: Create a new session.
  -Ctrl + b + ": Horizontal split, Ctrl + b + %: Vertical split.

8. Security and Permissions: Don’t Dig Your Own Pit

sudo No Password
Add to /etc/sudoers: username ALL=(ALL) NOPASSWD: ALL # Use with caution!
File Permission Check
-find / -perm -4000 2>/dev/null
Find all SUID files (potential risk).

Conclusion: The Ultimate Philosophy of Shell

Automate Everything: Never perform manual operations that can be scripted.Combination Skills are King: Use pipes, redirection, and toolchain stacking.Continuous Learning: Master one command a day, and in a year, you will be a terminal god!

Don't Let Basic Linux Skills Hold You Back! Do You Really Understand These Shell Tricks?

Leave a Comment