1. grep — Search Text by Pattern
Searches for lines matching a string or regular expression in files or command output.
Syntax
grep [options] "<pattern>" <file>
Common Usage
grep "error" log.txt
grep -i "error" log.txt # Ignore case
grep -r "ERROR" ./logs/ # Search recursively
grep -v "success" log.txt # Show non-matching lines
grep -n "WARNING" log.txt # Show line numbers
grep -l "main()" *.c # Show matching file names only
Practical Examples
tail -f app.log | grep "ERROR"
ps aux | grep nginx
history | grep ssh
Notes
- Supports regular expressions for advanced pattern matching.
- Frequently combined with pipes (
|) to filter command output.
2. find — Search Files and Directories
Searches for files or directories based on specified conditions.
Syntax
find <path> <conditions> <actions>
Common Usage
find . -name "*.txt"
find . -iname "*.txt"
find /var/log -type d
find . -size +100M
find . -mtime -7
Actions
find . -name "*.tmp" -delete
find ./logs -name "*.log" -exec grep "error" {} \;
Notes
-
-name: Case-sensitive filename search. -
-iname: Case-insensitive filename search. -
-type f: Files only. -
-type d: Directories only.
3. history — Command History
Displays previously executed commands.
Syntax
history
Common Usage
history
history 10
history | grep ssh
!145
!!
History Management
history -c
cat /dev/null > ~/.bash_history
Notes
-
!<number>reruns a command by its history number. -
!!reruns the previous command. - Command history is typically stored in
~/.bash_history.
4. alias — Create Command Shortcuts
Creates custom shortcuts for frequently used commands.
Syntax
alias <name>='<command>'
unalias <name>
Common Usage
alias ll='ls -la'
alias rm='rm -i'
alias c='clear'
alias logwatch='tail -f /var/log/nginx/access.log'
View existing aliases:
alias
Remove an alias:
unalias ll
Make Aliases Persistent
Edit your shell configuration:
nano ~/.bashrc
Add your aliases, then reload the configuration:
source ~/.bashrc
Notes
- Aliases created in the terminal are temporary.
- Add them to
~/.bashrc(or~/.zshrcif using Zsh) to make them permanent.
Note: AI was used to assist in the drafting and formatting of this post.
Top comments (0)