Mastering the Linux Command Line: Tips and Tricks
Navigating the Directory
cd -
: Switch back to the last working directory.
Running Multiple Commands
- Run commands sequentially:
command_1; command_2; command_3
- Run commands sequentially only if the previous command succeeds:
command_1 && command_2
Previous Commands and Arguments
- Repeat the last command:
!!
- Access specific arguments of the previous command:
!:n
: Replace with the nth argument of the last command.
Example:!:0
for the command itself,!:-1
for the first argument.- Keyboard shortcuts:
- Alt + .: Last argument of any previous command.
- Esc + .: Last argument of the most recent command.
- Common argument patterns:
!^ # First argument !$ # Last argument !* # All arguments !:2 # Second argument !:2-3 # Second to third arguments !:2-$ # Second to last arguments !:2* # Second to all arguments !:2- # Second to second-last arguments !! # Repeat the last command
Spelling Check in Linux
- Use
look
to check word spelling:look docum
Linux Terminal Shortcuts
- Ctrl + a: Move to the start of the line.
- Ctrl + e: Move to the end of the line.
- Ctrl + u: Cut everything before the cursor.
- Ctrl + k: Cut everything after the cursor.
- Ctrl + l: Clear the terminal.
- Ctrl + r: Search command history.
- Ctrl + b: Move back one character.
- Alt + b: Move back one word.
- Ctrl + f: Move forward one character.
- Alt + f: Move forward one word.
- Ctrl + d: Delete the current character.
- Ctrl + w: Cut the last word.
- Alt + d: Cut the word after the cursor.
- Alt + w: Cut the word before the cursor.
- Ctrl + y: Paste the last deleted content.
- Ctrl + _: Undo.
- Ctrl + x, x: Toggle between the first and current cursor position.
- Ctrl + c: Cancel the current command.
- Ctrl + j: End search at the current history entry.
- Ctrl + g: Cancel search and restore the original line.
- Ctrl + n: Next command in history.
- Ctrl + p: Previous command in history.
For more, check StackOverflow.
CSV Files: Pretty Display in Terminal
Add this function to your .bashrc
:
function pretty_csv {
column -t -s, -n "$@" | less -F -S -X -K
}
Use with:
pretty_csv your_file.csv
Pretty Code Printing with enscript
sudo apt install enscript
enscript -2rj --highlight=python --color=1 -o output.ps your_script.py
Check Disk Usage
sudo du -ah --max-depth=1 / | sort -hr
Scheduling Tasks with Crontab
Edit and schedule tasks:
cron -e
Comments