Skip to content
Menu

RHSA: Advanced Command Line Techniques

This guide consolidates advanced command-line techniques that are crucial for RHCSA exam success and real-world system administration efficiency.

Part 1: Recalling and Reusing Commands

1.1 The history Command

Your terminal session maintains a list of previously executed commands. Mastering its use is a major efficiency boost.

CommandPurposeExample
historyList all commands in the session’s history.history
!nExecute command number n from the history list.!170 (runs command #170)
!!Execute the last command again.!!
!stringExecute the most recent command starting with string.!touch (runs last touch command)
Ctrl+RReverse-i-search: Interactively search history. Type a keyword.(reverse-i-search)‘grep’: …
history | grep stringSearch entire history for commands containing string.history | grep yum

Part 2: Speed and Automation

2.1 Tab Completion

Press the Tab key to auto-complete commands, file paths, and package names.

  • Use Case: Type a partial name and press Tab to complete it. If multiple options exist, press Tab twice to see all possibilities.
  • Examples:
    • sys + Tab → Suggests systemctlsysctl, etc.
    • cd De + Tab → Completes to cd Desktop/
    • yum install http + Tab → Suggests httpd packages.

2.2 Creating Command Shortcuts with alias

An alias creates a custom shortcut for a longer command.

  • Syntax: alias shortname='long command with options'
  • Temporary Alias: Only lasts for the current terminal session.
alias updateall='sudo dnf update -y'
alias l='ls -la'
  • Permanent Alias: Add the alias command to your ~/.bashrc file.
vim ~/.bashrc
# Add line: alias l='ls -la'
# Save and quit (:wq). Then run: source ~/.bashrc
  • Remove an Alias: unalias aliasname (e.g., unalias updateall)

Part 3: Command Chaining and Control Flow

Execute multiple commands on a single line, controlling their execution based on success or failure.

OperatorNameLogicExample
;SemicolonRuns commands sequentially, regardless of success.mkdir /testdir; cd /testdir; touch test.txt
&&Logical ANDRuns the next command only if the previous one succeeded.sudo dnf update -y && reboot
||Logical ORRuns the next command only if the previous one failed.cd /nonexistent || echo "Directory missing"

Part 4: Power of the Pipe (|)

The pipe (|) takes the output of one command and uses it as the input for the next. This is the foundation of complex text processing.

  • Syntax: command1 | command2 | command3

Common Pipe Examples:

  • Search in a directory listing: ls -l /etc | grep ssh
  • Search for errors in logs: cat /var/log/messages | grep -i error
  • Find a user account: cat /etc/passwd | grep john
  • Monitor logs in real-time: tail -f /var/log/messages | grep error
  • Extract and sort data: cut -d: -f1 /etc/passwd | sort OR awk -F: ‘{print $1}’ /etc/passwd | sort

Part 5: Globbing (Wildcards) for File Operations

Globbing uses wildcard characters to match multiple files/directories, enabling bulk operations.

WildcardPurposeExample
*Matches zero or more of any character.ls *.log (all .log files), rm /var/log/*.log
?Matches exactly one of any character.ls file?.txt (file1.txt, fileA.txt, but not file10.txt)
[ ]Matches one character from a set or range.ls file[1-5].txt (file1.txt, file2.txt, … file5.txt)

Part 6: Data Extraction and Processing Toolkit

These commands are indispensable for filtering and analyzing system data.

CommandPrimary PurposeRHCSA-Relevant Example
cutExtract columns/fields from text.cut -d: -f1,3 /etc/passwd (get usernames & IDs)
awkPowerful pattern scanning & processing.df -h | awk '{print $1, $5}' (get filesystem & use%)
sedStream editor for filtering/transforming text.sed -n '1,5p' /etc/passwd (print first 5 lines)
sortSort lines of text.cut -d: -f1 /etc/passwd | sort (sort usernames)
headDisplay first lines of a file.head -5 /etc/passwd
tailDisplay last lines of a file.tail -5 /etc/passwd
wcWord, line, and character count.wc -l /etc/passwd (count users)
grepSearch for text patterns.grep "root" /etc/passwdgrep "Failed" /var/log/secure
findFind files/dirs based on name, size, etc.find /etc -name "sshd_config"
locateFast file search using a pre-built database.locate sshd
whichShows the full path of a shell command.which systemctl → /usr/bin/systemctl

Summary: The Efficient Administrator’s Workflow

  1. Don’t Retype: Use history and Tab completion to save time and avoid errors.
  2. Automate Repetition: Create aliases for frequently used, complex commands.
  3. Build Pipelines: Use | to chain simple commands (grepcutsort) into powerful data processing workflows.
  4. Operate in Bulk: Use globbing (*?[]) to manage many files at once.
  5. Control Execution: Use ;&&, and || to script multi-step processes directly in the command line.

BasePress comes with three different knowledge base themes to choose from: Default, Modern and Zen. You can change your WordPress Knowledge base theme at any time, navigate to BasePress >Settings >Appearance (Prior to version 2.7.0 this tab was called “Aspect”) and select the theme you want to use from the menu.

In the Basepress Menu, you’ll find a menu item with the name of the theme you’re using. When you click on the name of the theme, a new window opens where you find extra customization tools like color-branding, font or font size, sticky side bar and more. The additional customization tools will depend on the theme you’re using.

Leave a Reply

Your email address will not be published. Required fields are marked *