Master Advanced Shell Tricks to Supercharge Your Linux Workflow
This guide presents a collection of powerful Linux shell shortcuts, piping techniques, text‑processing commands, process‑management tricks, scripting tips, file‑handling utilities, and security best practices that together transform a novice user into an efficient command‑line power user.
1. Command‑Line Shortcuts: Speed Up Navigation
Use Ctrl+a to jump to the start of the line and Ctrl+e to jump to the end. Alt+b and Alt+f move a word backward or forward, roughly ten times faster than arrow keys.
History shortcuts include !! to repeat the previous command, !$ to reuse the last argument, and Ctrl+r for reverse incremental search.
Quick editing commands: Ctrl+w deletes the previous word, Ctrl+u clears everything before the cursor, and Ctrl+k clears everything after the cursor.
2. Pipes and Redirection: Master Data Flow
Combine output and error streams while saving to a file: cmd 2>&1 | tee log.txt Parse JSON directly from a curl request: curl -s http://example.com | jq .data Silence all output: >/dev/null 2>&1 Read input from a file instead of the keyboard: cmd < input.txt Process substitution example for comparing directory listings:
diff <(ls dir1) <(ls dir2)3. Text Processing: One‑Liner Replacements for Excel‑Level Tasks
awk examples:
Print the last column: awk '{print $NF}' file.txt Count occurrences of each IP in an access log:
awk '{ip[$1]++} END {for (i in ip) print i, ip[i]}' access.logsed examples:
Replace newlines with commas: sed ':a;N;$!ba;s/\n/,/g' file.txt Delete empty lines: sed '/^$/d' file.txt grep examples:
Show three lines after and two lines before a match: grep -A 3 -B 2 "error" log.txt Exclude lines containing a pattern:
grep -v "success" file.txt4. Process Management: Take Full Control
Run a command in the background with cmd &. List background jobs with jobs and bring one to the foreground using fg %1. Use nohup cmd & to keep a process alive after SSH disconnects.
Signal handling shortcuts: Ctrl+z pauses a job, kill -9 PID force‑kills it, and pkill -f "pattern" kills by name. Monitor resources with htop and check which process occupies a port: lsof -i :8080.
5. Scripting Tricks: Eliminate Repetitive Work
Default values and substring extraction:
${var:-"default"} # use default if var is empty
${str:0:5} # first five characters of $strDefine reusable functions and aliases:
zipdir() { zip -r "$1.zip" "$1"; }
alias ll='ls -alh --color=auto'Debugging flags: set -x prints each command as it runs, and set -e aborts the script on the first error.
6. File Operations: Bulk Rename and Cleanup
Find and delete logs older than seven days:
find /logs -name "*.log" -mtime +7 -exec rm {} \;Change permissions for all shell scripts in the current directory:
find . -type f -name "*.sh" -exec chmod 755 {} \;Batch rename extensions using the rename utility:
rename 's/\.txt$/.md/' *.txt7. Lesser‑Known Power Tools
xargs parallel download example: cat urls.txt | xargs -P 4 -I {} curl -O {} SSH tunneling:
ssh -L 8080:localhost:80 user@remote # forward remote port 80 to local 8080tmux session management:
tmux new -s mysession # create a new session
Ctrl+b " # horizontal split
Ctrl+b % # vertical split8. Security and Permissions: Avoid Common Pitfalls
Configure password‑less sudo by adding the line username ALL=(ALL) NOPASSWD: ALL to /etc/sudoers (use with caution).
Find all SUID files (potential security risks):
find / -perm -4000 2>/dev/nullConclusion: The Shell Philosophy
Automate everything : Anything you can script should be scripted.
Combine tools : Pipelines, redirections, and tool chaining are the core of Unix power.
Continuous learning : Master one new command each day and become a terminal wizard within a year.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
