Master Linux Command Line: Essential Tips, Shortcuts, and Powerful Tricks
This guide covers practical Linux shell techniques such as safe file deletion with aliases, sudo shortcuts, command‑line editing, history reuse, real‑time log viewing, disk and memory checks, process management, multi‑command execution, compressed log handling, terminal copy‑paste, ELF inspection, and useful aliases for faster workflows.
Safe Deleting Files
Set an alias for rm to request confirmation before deleting, e.g., rm -i. To disable the alias temporarily, use unalias rm or place unalias rm in ~/.bashrc if you prefer the original behavior.
Using sudo
When you forget to prepend sudo, you can repeat the last command with sudo !! or create an alias that includes sudo, for example alias update='sudo apt update'.
Command Editing and Cursor Movement
Useful readline shortcuts include Ctrl+U to delete from the start of the line to the cursor, Ctrl+K to delete from the cursor to the end, Ctrl+A and Ctrl+E to move to the beginning or end of the line, Alt+F / Alt+B to move a word forward or backward, and Ctrl+W to delete the previous word.
History Command Quick Execution
Use !n where n is the command number from history, or !! to repeat the last command.
Real‑time Log Viewing
Watch a log file as it grows with tail -f filename.log. The same effect can be achieved with less +F filename.log.
Disk or Memory Usage
Check mounted filesystem usage with df -h. Inspect memory statistics with free -h, which shows total, used, free, and available memory.
Find Process ID by Name
Retrieve a PID using pgrep hello or pidof hello, where hello is the process name.
Kill Process by Name
Terminate a process directly by name with killall hello or pkill hello.
View Process Runtime
Show how long a process has been running with ps -p 24525 -o lstart,etime, where 24525 is the PID.
Quick Directory Switching
Use cd - to return to the previous directory, cd to go to the home directory, and cd /path && command (or cd /path && rm -rf *) to ensure the second command runs only if the first succeeds.
Multiple Commands Execution
Separate commands with a semicolon ( ;) to run them sequentially regardless of success, or use && to run the second command only if the first succeeds.
View Compressed Log Files
Read a gzipped log without extracting using zcat test.gz or zless test.gz.
Clear File Content
Truncate a file instantly with >filename.
Log to File and Console
Redirect script output to both a log file and the console with ./test.sh | tee test.log.
Terminate and Resume Process
Pause a running process with Ctrl+Z and resume it with fg (foreground) or bg (background).
Measure Program Runtime
Use the time command to measure execution time, e.g., time ./fibo 30, which reports real, user, and sys times.
Top Memory Processes
List the ten processes consuming the most memory with ps -aux | sort -k4nr | head -n 10.
Search Commands
Find commands related to a task with man -k "copy files", which lists matching manual entries.
Copy‑Paste in Terminal
Use Ctrl+Insert to copy and Shift+Insert to paste in many terminal emulators.
Search Files for a String
Locate files containing a specific string with grep -rn "test", which prints the filename and line number.
Screen Freeze
Pause terminal output with Ctrl+S and resume with Ctrl+Q.
Edit Text Without an Editor
When no editor is available, create a file using cat >file.txt, type the content, then press Ctrl+D to save.
View ELF File
Inspect an ELF binary’s header with readelf -h filename to see architecture, type, and endianness.
Search Symbols in a Library
Check if a library contains a specific symbol using nm filename | grep interface.
Command Editing
Move to the start or end of a command line with Ctrl+A or Ctrl+E. Replace a word in the previous command using ^old^new^.
Alias for Remote Login
Create a shortcut for SSH login, e.g., alias butterfly='ssh -v -l jdoe 192.168.0.11'. Add such aliases to ~/.bashrc for persistence.
Freeze/Unfreeze Terminal
Use Ctrl+S to freeze output and Ctrl+Q to unfreeze.
Reuse Commands
Recall previous commands with !!, !ec (last command starting with “ec”), or !76 (command number 76).
View Log Dynamically
Continuously monitor a log file with tail -f /var/log/syslog.
Help
Most commands provide usage information via the --help option, complementing the manual pages accessed with man.
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.
