How do I find a complex command I ran in the past without scrolling through history?

Quick Linux Tip #27:

Try: history | grep -E 'sed.*i.*g' | tail -10

Info: history outputs numbered past commands. Combining it with grep -E (regex) lets you locate specific command patterns fast. Execute entry 892 with !892.

Examples:

  • $ history | grep 'docker run'  # Find docker commands
  • $ history | awk '{print $2}' | sort | uniq -c | sort -rn | head  # Top 10 used commands
  • $ Ctrl+R  # Interactive reverse search

Note: Ctrl+R is the fastest for interactive search. Set HISTSIZE=10000 in ~/.bashrc to retain a larger command history. Use HISTIGNORE='ls:cd:pwd' to skip logging basic commands.

Follow @LinuxTeck for more #LinuxTips

linuxteck@ubuntu:~$ history | grep -E 'sed.*i.*g' | tail -10
  892  sed -i 's/DEBUG=true/DEBUG=false/g' /etc/app.conf
 1234  find . -name '*.py' -exec sed -i 's/foo/bar/g' {} +
 1567  history | grep sed | awk '{print $2}' | sed -i 's/old/new/g'
 2345  cat file | sed -e 's/foo/bar/g' -e 's/baz/qux/g'
 2891  sed -i.bak 's/localhost/127.0.0.1/g' /etc/hosts
linuxteck@ubuntu:~$
PREVIOUS ARTICLE Quick Linux Tip #26: Which processes have connections to my database? NEXT ARTICLE Quick Linux Tip #28: How do I compress old logs but still grep them without decompressing?
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.