DEV Community

yuna song
yuna song

Posted on

Linux Basics: Search, History, and Aliases

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Practical Examples

tail -f app.log | grep "ERROR"
ps aux | grep nginx
history | grep ssh
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Common Usage

find . -name "*.txt"
find . -iname "*.txt"
find /var/log -type d
find . -size +100M
find . -mtime -7
Enter fullscreen mode Exit fullscreen mode

Actions

find . -name "*.tmp" -delete
find ./logs -name "*.log" -exec grep "error" {} \;
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Common Usage

history
history 10
history | grep ssh
!145
!!
Enter fullscreen mode Exit fullscreen mode

History Management

history -c
cat /dev/null > ~/.bash_history
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Common Usage

alias ll='ls -la'
alias rm='rm -i'
alias c='clear'
alias logwatch='tail -f /var/log/nginx/access.log'
Enter fullscreen mode Exit fullscreen mode

View existing aliases:

alias
Enter fullscreen mode Exit fullscreen mode

Remove an alias:

unalias ll
Enter fullscreen mode Exit fullscreen mode

Make Aliases Persistent

Edit your shell configuration:

nano ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Add your aliases, then reload the configuration:

source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Notes

  • Aliases created in the terminal are temporary.
  • Add them to ~/.bashrc (or ~/.zshrc if using Zsh) to make them permanent.

Note: AI was used to assist in the drafting and formatting of this post.

Top comments (0)