Table of Contents#
1. Navigation Shortcuts#
Tab Completion#
Shortcut: Tab
What it does: Autocompletes commands, filenames, or directory names.
Example:
Type cd Doc and press Tab—Bash will automatically complete it to cd Documents/ (if that directory exists).
Why it’s useful: Saves time and reduces typos. Press Tab twice to see all possible matches if there are multiple options (e.g., cd Doc + Tab + Tab lists directories starting with "Doc").
Quick Directory Jumps#
-
cd ~orcd: Jump to your home directory (e.g.,/home/yourusername).
Example:cd ~→ Moves to/home/alice. -
cd ..: Move up one directory (parent directory).
Example: If you’re in/home/alice/Documents,cd ..takes you to/home/alice. -
cd -: Jump back to the previous directory.
Example: After moving from/home/aliceto/var/log,cd -returns to/home/alice.
Pushd and Popd for Directory Stacking#
-
pushd <directory>: Navigate to a directory and add the current directory to a "stack."
Example:pushd /var/log→ Moves to/var/logand saves your previous directory. -
popd: Remove the top directory from the stack and navigate back to it.
Example: Afterpushd /var/log,popdreturns you to the directory you were in before.
Why it’s useful: Ideal for switching between multiple directories without retyping paths.
2. Command Editing Shortcuts#
Move Cursor to Start/End of Line#
-
Ctrl + A: Move cursor to the start of the command line.
Example: If you typedls -l /long/path/to/file,Ctrl + Ajumps to the beginning (ls). -
Ctrl + E: Move cursor to the end of the command line.
Example: From the start ofls -l /long/path/to/file,Ctrl + Ejumps to the end (file).
Delete Text Efficiently#
-
Ctrl + U: Delete all text from the cursor to the start of the line.
Example: If you typedsudo apt install wrongpackage, place the cursor afterinstalland pressCtrl + Uto deletesudo apt install. -
Ctrl + K: Delete all text from the cursor to the end of the line.
Example: Inecho "Hello, world!", place the cursor afterHello,and pressCtrl + Kto deleteworld!. -
Ctrl + W: Delete the previous word (from the cursor backward).
Example: Incd /home/alice/Documents, place the cursor at the end and pressCtrl + Wrepeatedly to deleteDocuments, thenalice/, etc.
Jump Between Words#
Alt + B: Move cursor backward one word.Alt + F: Move cursor forward one word.
Example: Ingit commit -m "Fix bug",Alt + Bfrom the end jumps to"Fix, then to-m, etc.
Undo Mistakes#
Shortcut: Ctrl + _ (or Ctrl + Shift + -)
What it does: Undo the last edit (works like "Ctrl + Z" in GUI apps).
Example: If you accidentally deleted text with Ctrl + U, Ctrl + _ restores it.
3. Command History Shortcuts#
Bash keeps a history of your commands (stored in ~/.bash_history). Use these shortcuts to reuse or search through past commands.
Search History with Reverse Lookup#
Shortcut: Ctrl + R
What it does: Open a reverse search prompt to find past commands.
Example: Press Ctrl + R, then type apt—Bash will show the most recent command containing "apt" (e.g., sudo apt update). Press Enter to run it, or Ctrl + R again to see older matches.
Repeat Recent Commands#
-
!!: Repeat the last command (pronounced "bang bang").
Example: If you forgotsudoforapt update, runsudo !!to executesudo apt update. -
!n: Repeat then-th command in history (findnwithhistory).
Example:historyshows500 ls -la, so!500runsls -la. -
!-n: Repeat the command fromnsteps ago.
Example:!-2repeats the command before the last one.
Reuse Arguments from History#
-
!$: Reuse the last argument of the previous command.
Example: Afterls /var/log/syslog,cat !$runscat /var/log/syslog. -
!*: Reuse all arguments of the previous command.
Example: Aftercp file1.txt file2.txt /backup/,mv !* /new_backup/runsmv file1.txt file2.txt /backup/ /new_backup/.
4. Process Control Shortcuts#
These shortcuts help manage running processes directly from the command line.
Terminate a Running Process#
Shortcut: Ctrl + C
What it does: Send an interrupt signal to the foreground process, stopping it.
Example: If ping google.com is running, Ctrl + C stops the ping.
Suspend and Resume Processes#
-
Ctrl + Z: Suspend the foreground process (sends it to the background).
Example: Pause a long-runningwgetdownload withCtrl + Z. -
bg: Resume a suspended process in the background.
Example: AfterCtrl + Zonwget, runbgto let it continue downloading in the background. -
fg: Bring a background process to the foreground.
Example:fg %1(replace1with the job number) brings the first background job back to the front.
List Background Jobs#
Shortcut: jobs
What it does: List all suspended or background processes.
Example:
$ jobs
[1]+ Stopped wget https://example.com/largefile.isoHere, [1] is the job number, and you can resume it with fg %1.
5. File Management Shortcuts#
These shortcuts simplify working with files and directories using pattern matching and expansion.
Wildcards for Pattern Matching#
-
*: Matches any sequence of characters (including none).
Example:ls *.txtlists all.txtfiles in the current directory. -
?: Matches exactly one character.
Example:ls file?.txtmatchesfile1.txtorfileA.txtbut notfile12.txt. -
[]: Matches one character from a set.
Example:ls file[1-3].txtmatchesfile1.txt,file2.txt,file3.txt.
Brace Expansion for Bulk Operations#
Syntax: {item1,item2,...} or {start..end}
What it does: Generates multiple arguments from a list or range.
Examples:
touch file{1..5}.txtcreatesfile1.txt,file2.txt, ...,file5.txt.cp report.{pdf,docx}copiesreport.pdfandreport.docx(if they exist).
Tilde Expansion for Home Directories#
-
~: Expands to your home directory (e.g.,/home/alice).
Example:cd ~/Documentsis shorter thancd /home/alice/Documents. -
~username: Expands to another user’s home directory (if you have permission).
Example:ls ~boblists files in Bob’s home directory.
6. Advanced Shortcuts#
Clear the Screen#
Shortcut: Ctrl + L (or clear command)
What it does: Clears the terminal screen, similar to the clear command.
Why it’s useful: Keeps your terminal uncluttered without typing clear.
Exit the Shell#
Shortcut: Ctrl + D (or exit command)
What it does: Closes the current terminal session.
Example: Press Ctrl + D to log out of an SSH session or exit a terminal window.
Create Custom Aliases#
What it does: Define shortcuts for long or frequently used commands.
Example: Add this to your ~/.bashrc file to create a shortcut for ls -la:
alias ll='ls -la'Reload the file with source ~/.bashrc, then run ll to list all files (including hidden ones) in long format.
Conclusion#
Mastering these Bash shortcuts will transform your command line experience from tedious to efficient. Start with the ones that align with your daily tasks (e.g., tab completion, Ctrl + R for history, or !! for repeating commands) and gradually incorporate more. With practice, these shortcuts will become muscle memory, saving you time and reducing frustration.